//
// ViewController.m
// 05-手势
//
// Created by wanghy on 15/9/21.
// Copyright (c) 2015年 wanghy. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView* imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建一个手势的对象
// 2.把手势的对象添加到需要手势的view当中
// 3.实现手势的方法
//UITapGestureRecognizer(敲击)-------------
// // 1.创建手势的对象
// UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// // 几根手指
// tap.numberOfTouchesRequired = 2;
// // 点几次
// tap.numberOfTapsRequired = 2;
// // 2.对imageView添加手势
// [self.imageView addGestureRecognizer:tap];
// // 3.实现方法
//UISwipeGestureRecognizer(轻扫)-------------
// 1.
UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 往左滑
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 2.
[self.imageView addGestureRecognizer:swipe];
[self.imageView addGestureRecognizer:swipe1];
//UILongPressGestureRecognizer(长按)-------------
// 1.
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 长按多长时间执行方法
longPress.minimumPressDuration = 2;
// 误差
longPress.allowableMovement = 10;
// 2.
[self.imageView addGestureRecognizer:longPress];
//UIRotationGestureRecognizer(旋转)-------------
// 1
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
// 2.
[self.imageView addGestureRecognizer:rotation];
//UIPinchGestureRecognizer(捏合,用于缩放)-------------
//1.
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 2.
[self.imageView addGestureRecognizer:pinch];
//UIPanGestureRecognizer(拖拽)-------------
// 1.
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
// 2.
[self.imageView addGestureRecognizer:pan];
}
// 拖拽
- (void)pan:(UIPanGestureRecognizer*)sender
{
CGPoint p = [sender translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, p.x, p.y);
[sender setTranslation:CGPointZero inView:self.imageView];
}
// 捏合
- (void)pinch:(UIPinchGestureRecognizer*)sender
{
// self.imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, sender.scale, sender.scale);
sender.scale = 1;
}
// 旋转
- (void)rotation:(UIRotationGestureRecognizer*)sender
{
NSLog(@"%f", sender.rotation);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, sender.rotation);
sender.rotation = 0;
// self.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
}
// 长按
- (void)longPress:(UILongPressGestureRecognizer*)sender
{
// 只是想让开始的时候执行某个代码 需要判断 手势的状态
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"longPress");
}
}
// 轻扫
- (void)swipe:(UISwipeGestureRecognizer*)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"left");
}
else {
NSLog(@"right");
}
}
// 敲击
- (void)tap:(UITapGestureRecognizer*)sender
{
NSLog(@"tap");
}
@end
原文:http://www.cnblogs.com/wahy/p/4827662.html