手势

//
//  ViewController.m
//  Ts
//
//  Created by liyang on 16/6/23.
//  Copyright © 2016年 liyang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *redView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
    [self.view addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2:)];
    tap2.delegate = self;
    [self.redView addGestureRecognizer:tap2];
    
    
    // tap2的state = failed,另一个tap1对手势进行响应。
    [tap2 requireGestureRecognizerToFail:tap1];
    
}
- (void)tap1:(UITapGestureRecognizer *)sender
{
    NSLog(@"手势1");
}
- (void)tap2:(UITapGestureRecognizer *)sender
{
    NSLog(@"手势2");
}
#pragma mark - 手势的代理方法
// 此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        return YES;
    }else{
        return NO;
    }
    
}
  • 当一个界面有多个手势时候,通过代理方法处理

你可能感兴趣的:(手势)