Xcode/Swift 中的多个 UIGestureRecognizers

Multiple UIGestureRecognizers in Xcode/Swift

现在,我有两个不同的 UILabel,每个都有自己的长按和平移 UIGestureRecognizers(通过故事板设置)。我的最终目标是让每个 UILabel 在长按时改变颜色,并且无需抬起手指来结束长按,从而在用户上下或左右平移时更改 UILabel 本身的值。

现在,每个UILabel都有自己的平移手势方法和长按手势方法。有什么方法可以让两个 UILabel 都使用一个长的 press/pan 方法,同时又能为一个标签做一些事情,为另一个标签做一些事情?

此外,是否有更好的方法来执行此操作?最终,我也想在改变标签的值时实现视觉反馈,比如以动画的形式。

我是 iOS 编程和一般编程的新手,非常感谢详细的回答。谢谢

您可以有一个 function.No 需要单独的手势来实现单独的 Label.Example

//First add tag value to ur labels
label_one.tag=1;
label_two.tag=2;


UIPanGestureRecognizer * _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(handlePanGesture:)];
_panGestureRecognizer.delegate = self;
[label_one addGestureRecognizer:_panGestureRecognizer];


 UIPanGestureRecognizer * _panGestureRecognizer_two = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(handlePanGesture:)];
_panGestureRecognizer_two.delegate = self;
[label_two addGestureRecognizer:_panGestureRecognizer_two];


-(void)handlePanGesture:(UIPanGestureRecognizer*)sender{
     if(sender.tag==1){

     }
     else if(sender.tag==2){
     }
 }

其他手势也一样