赞
踩
- // 1. 点按手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
- // 点按次数,例如双击2
- // 注意:在iOS中最好少用双击,如果一定要用,就一定要有一个图形化的界面告知用户可以双击
- [tap setNumberOfTapsRequired:2];
- // 用几根手指点按
- [tap setNumberOfTouchesRequired:1];
- [imageView addGestureRecognizer:tap];
-
- // 2. 长按手势 Long Press
- UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
- [imageView addGestureRecognizer:longTap];
-
- // 3. 拖动手势 Pan
- UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
- [imageView addGestureRecognizer:pan];
-
- // 4. 旋转手势 Rotation
- UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
- [imageView addGestureRecognizer:rotation];
-
- // 5. 缩放(捏合)手势 Pinch
- UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
- [imageView addGestureRecognizer:pinch];
-
- // 6. 轻扫手势 Swipe
- /*
- 手指在屏幕上扫动,和拖动手势的区别在于,手指离开屏幕才会触发监听方法
-
- 1> 手指可以上、下、左、右四个方向轻轻扫动,如果没有指定方向,默认都是向右扫动
- 2> 在设置轻扫手势时,通常需要将手势识别添加到父视图上监听
- 3> 在监听方法中,注意不要使用recognizaer.view,因为手势监听的是父视图,而要处理的视图通常是其他的视图
-
- 如果要使用多个方向的轻扫手势,需要指定多个轻扫手势,通常只需指定左右两个方向即可。
-
- 因为iOS用户大多不习惯上下的轻扫动作
- */
- UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe1 setDirection:UISwipeGestureRecognizerDirectionUp];
- [self.view addGestureRecognizer:swipe1];
-
- UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe2 setDirection:UISwipeGestureRecognizerDirectionDown];
- [self.view addGestureRecognizer:swipe2];
-
- UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe3 setDirection:UISwipeGestureRecognizerDirectionLeft];
- [self.view addGestureRecognizer:swipe3];
-
- UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe4 setDirection:UISwipeGestureRecognizerDirectionRight];
- [self.view addGestureRecognizer:swipe4];
- #pragma mark - 手势监听方法
- #pragma mark 轻扫手势
- // 尽管轻扫手势也是连续手势,但是该手势是在手指离开屏幕才会被触发的
- // 因此,在编写代码时,不需要处理手势的状态
- - (void)swipe:(UISwipeGestureRecognizer *)recognizer
- {
- // 让图片朝手指不同的方向飞出屏幕,然后再复位
- CGRect frame = kImageInitFrame;
- if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {
- frame.origin.y -= 400;
- } else if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) {
- frame.origin.y += 400;
- } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {
- frame.origin.x -= 300;
- } else {
- frame.origin.x += 300;
- }
-
- [UIView animateWithDuration:1.0 animations:^{
- [self.imageView setFrame:frame];
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:1.0f animations:^{
- [self.imageView setFrame:kImageInitFrame];
- }];
- }];
- }
-
- #pragma mark 捏合手势
- - (void)pinch:(UIPinchGestureRecognizer *)recognizer
- {
- /**
- 变化过程中,放大缩小
- 结束后,恢复
- */
- if (UIGestureRecognizerStateChanged == recognizer.state) {
- [recognizer.view setTransform:CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)];
-
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 恢复初始位置,清除所有的形变效果
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
-
- }
-
- #pragma mark 旋转手势,至少两根手指
- - (void)rotation:(UIRotationGestureRecognizer *)recognizer
- {
- // recognizer中的rotation属性是基于图片的初始旋转弧度的
- /**
- 变化过程中,旋转
- 结束后,恢复
- */
- if (UIGestureRecognizerStateChanged == recognizer.state) {
- [recognizer.view setTransform:CGAffineTransformMakeRotation(recognizer.rotation)];
- /**
- 累加的形变
- */
- // [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation)];
- // // 把手势识别的rotation设置为0,在下一次触发时,以当前的旋转角度为基准继续旋转
- // recognizer.rotation = 0;
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 恢复初始位置,清除所有的形变效果
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
- }
-
- #pragma mark 拖动手势
- - (void)pan:(UIPanGestureRecognizer *)recognizer
- {
- NSLog(@"拖动");
-
- /*
- 拖动手势结束后,以动画的方式回到初始位置
- */
- // 在手势状态处于“变化”时处理图片的移动
- // UIGestureRecognizerStateChanged类似于touchesMoved方法,会不断地被调用
- if (UIGestureRecognizerStateChanged == recognizer.state) {
-
- // translationInView,判断在父视图中平移的位置,平移的偏移量始终以视图的初始位置为基础
- CGPoint deltaPoint = [recognizer translationInView:self.view];
-
- // 用形变参数来改变图像的位置
- [recognizer.view setTransform:CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y)];
-
- // CGRect targetRect = kImageInitFrame;
- // targetRect.origin.x += deltaPoint.x;
- // targetRect.origin.y += deltaPoint.y;
- //
- // [recognizer.view setFrame:targetRect];
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 将图片复位
- // [recognizer.view setFrame:kImageInitFrame];
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
- }
-
- #pragma mark 长按手势
- - (void)longTap:(UILongPressGestureRecognizer *)recognizer
- {
- NSLog(@"长按");
- /*
- 旋转半圈的动画,动画完成后恢复
-
- 长按手势属于连续手势,是需要处理状态
-
- 因为长按通常只有一根手指,因此在Began状态下,长按手势就已经被识别了
-
- 针对长按的处理,最好放在UIGestureRecognizerStateBegan状态中实现
- */
- if (UIGestureRecognizerStateBegan == recognizer.state) {
- NSLog(@"长按");
-
- [UIView animateWithDuration:1.0f animations:^{
- [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
- } completion:^(BOOL finished) {
- // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- // [UIView animateWithDuration:1.0f animations:^{
- // [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
- // } completion:^(BOOL finished) {
- // // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
- // [recognizer.view setTransform:CGAffineTransformIdentity];
- // }];
- }
- }
-
- #pragma mark 点按手势
- - (void)tap:(UITapGestureRecognizer *)recognizer
- {
- NSLog(@"点我了");
- // 做一个动画效果
- // 向下移出屏幕,完成后再重新返回初始位置
- // recognizer.view 就是识别到手势的视图,也就是手势绑定到得视图
-
- CGRect initFrame = recognizer.view.frame;
- CGRect targetFrame = recognizer.view.frame;
- targetFrame.origin.y += 360;
-
- [UIView animateWithDuration:1.0f animations:^{
- // 自动反向重复动画
- [UIView setAnimationRepeatAutoreverses:YES];
- // 设置动画重复次数
- [UIView setAnimationRepeatCount:2];
-
- [recognizer.view setFrame:targetFrame];
- } completion:^(BOOL finished) {
- [recognizer.view setFrame:initFrame];
- }];
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。