- (void )viewDidLoad {
[super viewDidLoad];
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(10 , 70 , 350 , 550 )];
gestureView.backgroundColor = [UIColor yellowColor];
[self .view addSubview:gestureView];
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector (tap1Action:)];
tap1.numberOfTapsRequired = 1 ;
tap1.numberOfTouchesRequired = 1 ;
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector (tap2Action:)];
tap2.numberOfTapsRequired = 2 ;
[gestureView addGestureRecognizer:tap2];
[tap1 requireGestureRecognizerToFail:tap2];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector (swipeAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[gestureView addGestureRecognizer:swipe];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector (panAction:)];
[gestureView addGestureRecognizer:pan];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector (longPress:)];
longPress.minimumPressDuration = 3 ;
[gestureView addGestureRecognizer:longPress];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector (rotation:)];
[gestureView addGestureRecognizer:rotation];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector (pinchAction:)];
[gestureView addGestureRecognizer:pinch];
}
- (void )tap1Action:(UITapGestureRecognizer *)tap1
{
NSLog (@"单击" );
}
- (void )tap2Action:(UITapGestureRecognizer *)tap2
{
NSLog (@"双击" );
}
- (void )swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog (@"轻扫" );
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog (@"向上轻扫" );
}
}
- (void )panAction:(UIPanGestureRecognizer *)pan{
}
- (void )longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog (@"开始长按" );
}else if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog (@"结束长按" );
}
}
- (void )rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog (@"旋转" );
CGFloat r = rotation.rotation ;
NSLog (@"r is %.2f" , 180 / M_PI * r);
}
- (void )pinchAction:(UIPinchGestureRecognizer *)pinch{
NSLog (@"捏合" );
CGFloat scale = pinch.scale ;
pinch.view .transform = CGAffineTransformMakeScale(scale, scale);
}