iOS 轻击触摸和手势的检测
Posted 小、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 轻击触摸和手势的检测相关的知识,希望对你有一定的参考价值。
一、检测捏合手势( UIPinchGestureRecognizer):
//设定一个实例变量存储手指之间的其起始距离 @property (assign, nonatomic) CGFloat initialFontSize; //调用: UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(doPinch:)]; [self.viewaddGestureRecognizer:pinch]; //实现方法: - (void)doPinch:(UIPinchGestureRecognizer *)pinch { if (pinch.state == UIGestureRecognizerStateBegan) { initialFontSize = label.font.pointSize;//开始时的 } else { label.font = [label.fontfontWithSize:initialFontSize * pinch.scale]; } }
二、 检测多次轻击(UITapGestureRecognizer):
//调用: UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(tap1)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; [self.viewaddGestureRecognizer:singleTap]; UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(tap2)]; doubleTap.numberOfTapsRequired = 2; doubleTap.numberOfTouchesRequired = 1; [self.viewaddGestureRecognizer:doubleTap]; [singleTap requireGestureRecognizerToFail:doubleTap]; //这句是最主要的 UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(tap3)]; tripleTap.numberOfTapsRequired = 3; tripleTap.numberOfTouchesRequired = 1; [self.viewaddGestureRecognizer:tripleTap]; [doubleTap requireGestureRecognizerToFail:tripleTap]; UITapGestureRecognizer *quadrupleTap = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(tap4)]; quadrupleTap.numberOfTapsRequired = 4; quadrupleTap.numberOfTouchesRequired = 1; [self.viewaddGestureRecognizer:quadrupleTap]; [tripleTap requireGestureRecognizerToFail:quadrupleTap]; //不同点击触发的事件: - (void)tap1 { singleLabel.text = @"Single Tap Detected"; [selfperformSelector:@selector(eraseMe:) withObject:singleLabelafterDelay:1.6f]; } - (void)tap2 { doubleLabel.text = @"Double Tap Detected"; [selfperformSelector:@selector(eraseMe:) withObject:doubleLabelafterDelay:1.6f]; } - (void)tap3 { tripleLabel.text = @"Triple Tap Detected"; [selfperformSelector:@selector(eraseMe:) withObject:tripleLabelafterDelay:1.6f]; } - (void)tap4 { quadrupleLabel.text = @"Quadruple Tap Detected"; [selfperformSelector:@selector(eraseMe:) withObject:quadrupleLabelafterDelay:1.6f]; } //清除内容 - (void)eraseMe:(UILabel *)label { label.text = @""; }
以上是关于iOS 轻击触摸和手势的检测的主要内容,如果未能解决你的问题,请参考以下文章
向 UIImageView 添加轻击手势以更改 UILabel?