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 = @"";
}

 

 
 
posted @ 2019-01-03 17:54  小、  阅读(552)  评论(0编辑  收藏  举报