Swift:如何通过滑动使SegmentedControl通知值更改?

时间:2015-10-27 18:37:14

标签: ios swift uigesturerecognizer uisegmentedcontrol

我在UITableViewCell自定义类中有分段控件。我添加了滑动手势以启用通过滑动修改值。在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)

编辑: 在自定义段控件中我找到了这个方法,也许编辑它可以解决问题,但我不知道如何做到这一点

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
}

2 个答案:

答案 0 :(得分:0)

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

你可以打电话给你

segmentValueChanged(segmentedControl)

结束时的

handleswipes()

更新了sampleTableReuse

中的代码

请仅将此用作灵感,并尝试相应地修改您的代码

答案 1 :(得分:0)

搞定了

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)

解决问题