UITouch觸摸與手勢
1. //延遲調用單擊事件,以便輕按兩下時可取消單擊調用
[selfperformSelector:@selector(singleTap) withObject:nilafterDelay:0.2];
2. //類方法取消單擊調用方法,及時調用輕按兩下方法
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap) object:nil];
3.self.clipsToBounds= YES//當子視圖越界時,剪切子視圖
幾種常用的手勢
//建立手勢視圖
UIView *gestureView = [[UIViewalloc]initWithFrame:CGRectMake(10, 20, 300, 300)];
gestureView.backgroundColor = [UIColororangeColor];
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100, 400, 100, 50)];
label.text = @"0";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColorblueColor];
label.tag = 100;
[self.viewaddSubview:label];
[self.viewaddSubview:gestureView];
/******************點選手勢*******************/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap1Action:)];
//點選的數量---點選的次數
tap1.numberOfTapsRequired = 1;
//點選的個數-----手指的個數
tap1.numberOfTouchesRequired = 1;
[gestureView addGestureRecognizer:tap1];
//單手輕按兩下
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap2Action:)];
tap2.numberOfTouchesRequired = 2;//雙手輕按兩下
tap2.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap2];
//如果tap2觸發了.則tap1則失效-----輕按兩下時,單擊失效
[tap1 requireGestureRecognizerToFail:tap2];
/******************輕掃手勢***********************/
UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip1Action:)];
[gestureView addGestureRecognizer:swip1];
/******************平移手勢***********************/
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan1Action:)];
[gestureView addGestureRecognizer:pan1];
/******************長按手勢***********************/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressAction:)];
[gestureView addGestureRecognizer:longPress];
/******************旋轉手勢***********************/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];
[gestureView addGestureRecognizer:rotation];
/******************捏合手勢***********************/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
[gestureView addGestureRecognizer:pinch];
}
/******************點選手勢*******************/
//單擊
- (void)tap1Action:(UITapGestureRecognizer *)tap1
{
NSLog(@"單擊");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
label.text = @"單擊";
//輕按兩下
- (void)tap2Action:(UITapGestureRecognizer *)tap2
NSLog(@"輕按兩下");
label.text = @"輕按兩下";
/******************輕掃手勢***********************/
- (void)swip1Action:(UISwipeGestureRecognizer *)swip1
// NSLog(@"輕掃");
if (swip1.direction == UISwipeGestureRecognizerDirectionRight) {
label.text = @"向右輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionLeft){
label.text = @"向左輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionUp){
label.text = @"向上輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionDown){
label.text = @"向下輕掃";
}
/******************平移手勢***********************/
- (void)pan1Action:(UIPanGestureRecognizer *)pan1
// UILabel *label = (UILabel *)[self.viewviewWithTag:100];
CGPoint p1 = [pan1 locationInView:pan1.view];
NSLog(@"平移的坐标是:%@",NSStringFromCGPoint(p1));
/******************長按手勢***********************/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
if (longPress.state == UIGestureRecognizerStateBegan) {
label.text = @"開始長按";
}elseif (longPress.state == UIGestureRecognizerStateEnded){
label.text = @"長按結束";
/******************旋轉手勢***********************/
- (void)rotationAction:(UIRotationGestureRecognizer *)rota
//旋轉的弧度
CGFloat r = rota.rotation;
NSLog(@"旋轉的弧度是:%.2f",r);
NSLog(@"旋轉的角度是:%.2f",180 * r / M_PI);
/******************捏合手勢***********************/
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
//獲得捏合的比例
CGFloat scale = pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);