使用UITapGestureRecognizer更改标签颜色

时间:2016-01-07 13:14:24

标签: ios swift uibutton

我有一个带有UITapGestureRecognizer和longgestureRecognizer的标签:

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed:"))
let longgestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longLabelPressed:"))
label.addGestureRecognizer(gestureRecognizer)
label.addGestureRecognizer(longgestureRecognizer)

当我按下时,我想改变UIButton中的颜色:

func longLabelPressed(recognizer:UILongPressGestureRecognizer){
        if let label = recognizer.view as? UILabel {
            if recognizer.state == .Began {
                label.textColor = UIColor.redColor()
            }

            if recognizer.state == .Ended {
                label.textColor = UIColor.blackColor()
            }



        }
    }

但是如何检测点击结束事件?

func labelPressed(recognizer:UITapGestureRecognizer) {
        if let label = recognizer.view as? UILabel {

        }

    }

我的目标是创建带有触摸事件的UIButton标签。

4 个答案:

答案 0 :(得分:4)

标签的UserInteractionEnabled默认为false 。因此,如果您正在使用其标签插座,请从(XIB / storyboard)

启用它

现在使用UILongPressGestureRecognizer使标签与按钮相同 然后你成功调用了事件,但你在UILongPressGestureRecognizer中编写了标签颜色更改代码,因此需要一些时间(默认时间)来检测触摸事件。 recognizer.state == .Began所以改变long pressGesture的最短时间

longgestureRecognizer.minimumPressDuration = 0.001

使用recognizer.state == .Began将快速拨打电话。

答案 1 :(得分:3)

创建一个新的UILabel子类并实现它:

<强>目标C

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor blueColor];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor whiteColor];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor blackColor];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor redColor];
}

Swift 4

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.blue
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.white
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.black
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.red
}

不要忘记设置:

<强>目标C

label.userInteractionEnabled = YES;

Swift 4

label.isUserInteractionEnabled = true

将自定义类添加到xib中的标签上,然后享受:)

参考:UILabel + touchDown

答案 2 :(得分:1)

docs内容相似,你应该启用不同的互动。

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed:"))
let longgestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longLabelPressed:"))

label.userInteractionEnabled = YES;

label.addGestureRecognizer(gestureRecognizer)
label.addGestureRecognizer(longgestureRecognizer)

答案 3 :(得分:0)

两件事:

  1. 您应该将标签上的userInteractionEnabled属性设置为true(如前所述)
  2. 使用UITapGestureRecognizer只会在手势完成时为您提供信息。如果您想知道它何时开始,我建议您使用UILongPressGestureRecognizer并将其minimumPressDuration属性设置为0.