#import "TouchViewController.h" @interface TouchViewController () @end @implementation TouchViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor grayColor]; NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare",@"MagentaSquare",@"YellowSquare", nil]; for (int i = 0; i < 3; i ++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+90*i, 50+110*i, 100, 100)];; imageView.image = [UIImage imageNamed:imageArr[i]]; imageView.userInteractionEnabled = YES; [self.view addSubview:imageView]; // 添加点击手势 [self addGesture:imageView]; [imageView release]; } } // 添加手势识别器 - (void)addGesture:(UIImageView *)imageView { // 1、添加单击事件,点击手势识别器 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; // 设置点击的操作 tap.numberOfTapsRequired = 2; // 将手势添加到view 上 [imageView addGestureRecognizer:tap]; [tap release]; // // 2、创建一个移动手势 // UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; // [imageView addGestureRecognizer:pan]; // [pan release]; // 3、 实现缩放,捏合手势 UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinClick:)]; [imageView addGestureRecognizer:pin]; [pin release]; // 4、旋转 UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rota:)]; [imageView addGestureRecognizer:rota]; [rota release]; // 5、长按 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpress:)]; [imageView addGestureRecognizer:longPress]; [longPress release]; // 6-1、轻扫手势,一个手势只能支持一个手势,要想支持多个,就得重新创建多个方向的 // 会和移动的手势冲突 UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; swip1.direction = UISwipeGestureRecognizerDirectionUp ; [imageView addGestureRecognizer:swip1]; [swip1 release]; // 6-2、轻扫手势 UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; swip2.direction = UISwipeGestureRecognizerDirectionDown ; [imageView addGestureRecognizer:swip2]; [swip2 release]; // 6-3、轻扫手势 UISwipeGestureRecognizer *swip3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; swip3.direction = UISwipeGestureRecognizerDirectionLeft ; [imageView addGestureRecognizer:swip3]; [swip3 release]; // 6-4、轻扫手势 UISwipeGestureRecognizer *swip4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; swip4.direction = UISwipeGestureRecognizerDirectionRight ; [imageView addGestureRecognizer:swip4]; [swip4 release]; } // 实现手势的事件,只有点击图片的时候才会触发 - (void)tapClick:(UITapGestureRecognizer *)sender { [self.view bringSubviewToFront:sender.view]; } // 实现移动手势,类似于touchMove - (void)move:(UIPanGestureRecognizer *)sender { // 固定的写法 CGPoint point = [sender translationInView:self.view]; sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y); [sender setTranslation:CGPointZero inView:self.view]; } // 实现捏合手势的功能 - (void)pinClick:(UIPinchGestureRecognizer *)sender { sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale); sender.scale = 1; } // 实现旋转 - (void)rota:(UIRotationGestureRecognizer *)sender { sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation); sender.rotation = 1; }