theDC
theDC

Reputation: 6484

Swift:How to make SegmentedControl notice value change done by swiping?

I have segmented control inside the UITableViewCell custom class. I added swipe gesture to enable modifying the value also by swipe. Inside the cellForRowAtIndexPath method. In awakeFromNib() method in I add this line:

segmentControl.addTarget(self, action: "segmentValueChanged:", forControlEvents: .ValueChanged)

to observe changes to the value of segmented control. Here is the implementation of mentioned method

    func segmentValueChanged(sender: AnyObject){

    if segmentControl.selectedIndex == 0 {

        segmentControl.thumbColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
        segmentControl.selectedLabelColor = UIColor.whiteColor()
        segmentControl.unselectedLabelColor = UIColor.grayColor()


    }else if segmentControl.selectedIndex == 1{

        segmentControl.thumbColor = UIColor(red: 46.0/255.0, green: 204.0/255.0, blue: 113.0/255.0, alpha: 1.0)
        segmentControl.selectedLabelColor = UIColor.grayColor()
        segmentControl.unselectedLabelColor = UIColor.whiteColor()

    }

Here is my function that handles swipes

 func handleSwipes(sender:UISwipeGestureRecognizer)

    {
        if segmentControl.selectedIndex == 1 && sender.direction == .Left

        {

            self.segmentControl.selectedIndex = 0

        }

        if segmentControl.selectedIndex == 0 && sender.direction == .Right

        {

            self.segmentControl.selectedIndex = 1

        }

    }

The problem is: When I swipe segmentValueChanged does not notice any change is selectedIndex value? I tried playing around with target and senders, but without any good result unfortunately

Thanks in advance

EDIT:

I add gesture inside cellForRowAtIndexPath

let leftSwipe = UISwipeGestureRecognizer(target: cell, action: Selector("handleSwipes:"))

    let rightSwipe = UISwipeGestureRecognizer(target: cell, action: Selector("handleSwipes:"))

 leftSwipe.direction = .Left

 rightSwipe.direction = .Right

 cell.segmentControl.thumbView.addGestureRecognizer(leftSwipe)

 cell.segmentControl.thumbView.addGestureRecognizer(rightSwipe)

EDIT: In custom segment control I found this method, maybe editing it could solve the issue, but I do not have idea how to do that

override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {

    let location = touch.locationInView(self)

    var calculatedIndex : Int?
    for (index, item) in labels.enumerate() {
        if item.frame.contains(location) {
            calculatedIndex = index
        }
    }


    if calculatedIndex != nil {
        selectedIndex = calculatedIndex!
        sendActionsForControlEvents(.ValueChanged)
    }

    return false
}

Upvotes: 1

Views: 2602

Answers (2)

theDC
theDC

Reputation: 6484

Got it

func handleSwipes(sender:UISwipeGestureRecognizer)

    {
        if segmentControl.selectedIndex == 1 && sender.direction == .Left

        {

            self.segmentControl.selectedIndex = 0

            segmentControl.sendActionsForControlEvents(.ValueChanged)

        }

        if segmentControl.selectedIndex == 0 && sender.direction == .Right

        {

            self.segmentControl.selectedIndex = 1
            segmentControl.sendActionsForControlEvents(.ValueChanged)

        }

    }

segmentControl.sendActionsForControlEvents(.ValueChanged)

solves the problem

Upvotes: 0

Vineeth Vijayan
Vineeth Vijayan

Reputation: 1329

Unfortunately you cannot trigger segmented control value changed by changing the selected index

You can call your

segmentValueChanged(segmentedControl)

at the end of

handleswipes()

Updated the code in sampleTableReuse

Please use this only for inspiration and try to modify your code accordingly

Upvotes: 0

Related Questions