Swift:How 让 SegmentedControl 通过滑动通知值变化?

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

我在 UITableViewCell 自定义 class 中进行了分段控制。我添加了滑动手势,也可以通过滑动来修改值。在 cellForRowAtIndexPath 方法中。 在 awakeFromNib() 方法中,我添加了这一行:

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

观察分段控件值的变化。 这是上述方法的实现

    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()

    }

这是我处理滑动的函数

 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

        }

    }

问题是:当我滑动 segmentValueChanged 时没有注意到 selectedIndex 值有任何变化? 我试着玩弄目标和发件人,但不幸的是没有任何好结果

提前致谢

编辑:

我在 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)

编辑: 在custom segment control中我找到了这个方法,也许编辑它可以解决问题,但我不知道该怎么做

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
}

很遗憾,您无法触发通过更改所选索引更改的分段控件值

你可以打电话给你的

segmentValueChanged(segmentedControl)

结束时

handleswipes()

更新了 sampleTableReuse

中的代码

请仅以此为灵感,并尝试相应地修改您的代码

知道了

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)

解决问题