【问题标题】:Change label color with UITapGestureRecognizer使用 UITapGestureRecognizer 更改标签颜色
【发布时间】:2016-01-07 13:14:23
【问题描述】:

我有一个带有 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 这样带有触摸事件的标签。

【问题讨论】:

  • 你的代码有什么问题?
  • 如果这是您的目标,为什么不使用按钮来代替?
  • 因为我有这个问题:stackoverflow.com/questions/34652981/…
  • 所以我需要将按钮替换为标签:(或者如果您找到更好的解决方案...
  • 我认为即使您使用点击手势识别器,它也会与桌面点击手势混淆。使用 UIButtons 总是更好

标签: ios swift uibutton


【解决方案1】:

标签的 UserInteractionEnabled 默认为 false。所以如果你使用它的标签出口,那么从(XIB/故事板)启用它

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

longgestureRecognizer.minimumPressDuration = 0.001

使用recognizer.state == .Began 会快速调用。

【讨论】:

【解决方案2】:

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

Objective-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];
}

斯威夫特 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
}

别忘了设置:

Objective-C

label.userInteractionEnabled = YES;

斯威夫特 4

label.isUserInteractionEnabled = true

将自定义类添加到您的 xib 中的标签并享受 :)

参考:UILabel + touchDown

【讨论】:

    【解决方案3】:

    就像提到的 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)
    

    【讨论】:

      【解决方案4】:

      两件事:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 2017-03-28
        相关资源
        最近更新 更多