2015-03-01 20:31:56 Avecle_soleil 阅读数 731

手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。

iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。

UITapGestureRecognizer 

UIPinchGestureRecognizer 

UIRotationGestureRecognizer 

UISwipeGestureRecognizer 

UIPanGestureRecognizer 

UILongPressGestureRecognizer



      上面的手势对应的操作是:
  • Tap(点一下)
  • Pinch(二指往內或往外拨动,平时经常用到的缩放)
  • Rotation(旋转)
  • Swipe(滑动,快速移动)
  • Pan (拖移,慢速移动)
  • LongPress(长按) 

2、使用手势的步骤

使用手势很简单,分为两步:

  • 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
  • 添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。

ps:一个手势只能对应一个View,但是一个View可以有多个手势。


手势具体操作 如下:

新建一个mainViewController 继承于 UIViewcontroller ,

mainViewController.m

#import "mainViewController.h"


@interface mainViewController ()


@property (retain, nonatomic)UIImageView *imageView;




@end


@implementation mainViewController


- (void)dealloc{

    [self.imageView release];

    [super dealloc];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }

    return self;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    [self createSubViews];

    

}


- (void)createSubViews{

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 130, 220, 350)];

    [_imageView setImage:[UIImage imageNamed:@"1.jpg"]];

    _imageView.userInteractionEnabled = YES;

    [self.view addSubview:_imageView];

    [_imageView release];

    

    

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"点击", @"长按", @"轻扫", @"捏合", @"旋转", @"拖拽", nil]];

    segmentedControl.frame = CGRectMake(30, 600, 315, 50);

    segmentedControl.momentary = YES;

    segmentedControl.tintColor = [UIColor blackColor];

    [segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedControl];

    [segmentedControl release]; 

}

#pragma mari - UISegmentedControl的点击事件

- (void)action:(id)sender{

    // 分段点击按钮

    UISegmentedControl *seg = (UISegmentedControl *)sender;

    // 移除imageView的所有手势,重新添加新的手势

    // 第一步:获得一个视图的所有手势

    NSArray *gestures = self.imageView.gestureRecognizers;

    // 第二步:移除

    for (UIGestureRecognizer *ges in gestures) {

        [self.imageView removeGestureRecognizer:ges];

    }

    switch (seg.selectedSegmentIndex) {

        case 0:

        {

            // 创建一个点击手势

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

            [self.imageView addGestureRecognizer:tap];

            [tap release];

        }

            break;

        case 1:

        {

            // 长按手势

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

            [self.imageView addGestureRecognizer:longPress];

            [longPress release];

        }

            break;

        case 2:

        {

            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

            // 一个轻扫手势只能有一个轻扫方向

            // 设置轻扫的方向

            swipe.direction = UISwipeGestureRecognizerDirectionDown;

            [self.imageView addGestureRecognizer:swipe];

            [swipe release];

        }

            break;

        case 3:

        {

            // 捏合手势

            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAciton:)];

            [self.imageView addGestureRecognizer:pinch];

            [pinch release];

        }

            break;

        case 4:

        {

            // 旋转手势

            UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];

            [self.imageView addGestureRecognizer:rotate];

            [rotate release];

        }

            break;

        case 5:

        {

            // 拖拽手势

            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

            [self.imageView addGestureRecognizer:pan];

            [pan release];

        }

            break;

        default:

            break;

    }

    

}

#pragma mark - 内部的点击手势事件

#pragma mark 点击

- (void)tapAction:(UITapGestureRecognizer *)tap{

    // 通过手势获得手势所在的视图

//    UIImageView *aImage = (UIImageView *)tap.view;

    // 通过不同的状态做不同的事

    if (tap.state == UIGestureRecognizerStateBegan) {

        NSLog(@"开始点击");

    }

    

    NSLog(@"%s", __func__);

}

#pragma mark 长按

- (void)longPressAction:(UILongPressGestureRecognizer *)press{

    if (press.state == UIGestureRecognizerStateBegan) {

        NSLog(@"begin");

    }

}

#pragma mark 轻扫

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

        NSLog(@"横扫千军");

}

#pragma mark 捏合

- (void)pinchAciton:(UIPinchGestureRecognizer *)pinch{

    NSLog(@"捏合");

    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1.0f;

}

#pragma mark 旋转

- (void)rotateAction:(UIRotationGestureRecognizer *)ges{

    // 旋转的基础角度为0, 每次旋转的角度都是以当前中轴线为轴来判定的

    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, ges.rotation);

    ges.rotation = 0;

    NSLog(@"Rotation = %f", ges.rotation);

}

#pragma mark 拖拽

- (void)panAction:(UIPanGestureRecognizer *)pan{

    CGPoint point = [pan translationInView:self.imageView];

    // 偏移量会在每次偏移之后,在偏移后的基础上增加

    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

    // 重新把偏移量归零

    [pan setTranslation:CGPointZero inView:self.imageView];

    

}


AppDelegate.m

#import "AppDelegate.h"

#import "mainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc{

    [super dealloc];

}




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _window.backgroundColor = [UIColor whiteColor];

    [_window makeKeyAndVisible];

    [_window release];

    

    mainViewController *myView = [[mainViewController alloc] init];

    [self.window setRootViewController:myView];

    [myView release];

    

    return YES;

}









2016-03-23 20:41:16 wangwei1138568289 阅读数 170

手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。

一、介绍

1、iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

  • UIPanGestureRecognizer(拖动)
  • UIPinchGestureRecognizer(捏合)
  • UIRotationGestureRecognizer(旋转)
  • UITapGestureRecognizer(点按)
  • UILongPressGestureRecognizer(长按)
  • UISwipeGestureRecognizer(轻扫)

在六种手势识别中,只有一种手势是离散型手势,他就是 UITapGestureRecognizer。

离散型手势的特点就是:一旦识别就无法取消,而且只会调用一次手势操作事件(初始化手势时指定的回调方法)。

换句话说其他五种手势是连续型手势,而连续型手势的特点就是:会多次调用手势操作事件,而且在连续手势识别后可以取消手势。

2、手势的枚举:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
    UIGestureRecognizerStateChanged,   // 手势状态发生转变
     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
    UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
    UIGestureRecognizerStateFailed,    // 手势识别失败,恢复到默认状态
     UIGestureRecognizerStateRecognized= UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded


3、使用手势的步骤

使用手势很简单,分为三步:

(1)创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。

(2)设置手势识别器对象实例的相关属性(可选部分)

(3)添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。


二、代码示例


#import "ViewController.h"

#import "NextViewController.h"


@interface ViewController ()

{

    UIImageView *imageView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    

    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 100, 200)];

    

    [self.view addSubview:imageView];

    

    //    点击手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tick:)];

    //    手指点击屏幕的次数

    tap.numberOfTapsRequired = 1;

    //    几个手指点击

    tap.numberOfTouchesRequired = 1;

    [self.view addGestureRecognizer:tap];

    

    //    长按

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(loagPress:)];

    //    最少按多少秒

    longPress.minimumPressDuration = 3;

    [self.view addGestureRecognizer:longPress];

    

    //    轻扫

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

    //    轻扫的方向

    swipe.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:swipe];

    

    

    //    拖动

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

    [pan requireGestureRecognizerToFail:swipe];

    [self.view addGestureRecognizer:pan];

    

    //    捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

    

    [self.view addGestureRecognizer:pinch];

    

    //    旋转

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

    rotation.rotation = 1;

    [self.view addGestureRecognizer:rotation];

}


- (void)tick:(UITapGestureRecognizer *)tap

{

//  6、再出始化  这个对象的  地方  挂上代理

//    NextViewController *next = [[NextViewController alloc]init];

//    next.delegate = self;

//    

//    [self presentViewController:next animated:YES completion:nil];

    

    

    imageView.transform = CGAffineTransformIdentity;

    //    获取点击屏幕的位置

    NSLog(@"tap%f   %f",[tap locationInView:self.view].x,[tap locationInView:self.view].y);

    

    imageView.image = [UIImage imageNamed:@"yu.jpg"];

    imageView.alpha = 1;

    imageView.center = [tap locationInView:self.view];

    [UIView animateWithDuration:0.5 animations:^{

        imageView.alpha = 0.01;

    }];

}


- (void)mmmmmm

{

    imageView.image = [UIImage imageNamed:@"yu.jpg"];

    imageView.alpha = 1;

}


- (void)loagPress:(UILongPressGestureRecognizer *)longPress

{

    NSLog(@"longPress%f   %f",[longPress locationInView:self.view].x,[longPress locationInView:self.view].y);

}


- (void)swipe:(UISwipeGestureRecognizer *)swipe

{

    NSLog(@"swipe%f   %f",[swipe locationInView:self.view].x,[swipe locationInView:self.view].y);

    

    self.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);

    [UIView animateWithDuration:0.8 animations:^{

        self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);

    }];

}


- (void)pan:(UIPanGestureRecognizer *)pan

{

    //    获取拖动的位置

    imageView.image = [UIImage imageNamed:@"yu.jpg"];

    imageView.alpha = 1;

    imageView.center = [pan locationInView:self.view];

    

}


- (void)pinch:(UIPinchGestureRecognizer *)pinch

{

    imageView.image = [UIImage imageNamed:@"yu.jpg"];

    imageView.alpha = 1;

    

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    //    捏合的变化规模

    pinch.scale = 1;

    

}


- (void)rotation:(UIRotationGestureRecognizer *)rotation

{

    

    imageView.image = [UIImage imageNamed:@"yu.jpg"];

    imageView.alpha = 1;

    

    //    使旋转手势上的视图旋转变化

    imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);

    

}

2014-07-22 17:27:12 qq_18127543 阅读数 210
/*六大手势:1.轻拍,单击,双击,多指点击 2.长按 3.轻扫,分上下左右四个方向 4.旋转
     5.捏合,放大缩小  6.拖拽,移动位置   */
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 280, 280)];
    aView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:aView];
    [aView release];
    //轻拍手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeViewBackgroundColor:)];
    tapGesture.numberOfTapsRequired = 2;
    tapGesture.numberOfTouchesRequired = 2;
    [aView addGestureRecognizer:tapGesture];
    [tapGesture release];
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeViewBackgroundColor:)];
    longPress.minimumPressDuration = 2.0;
    [aView addGestureRecognizer:longPress];
    [longPress release];
    //轻扫手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeViewBackgroundColor:)];
    [aView addGestureRecognizer:swipeGesture];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    [swipeGesture release];
    //拖动手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [aView addGestureRecognizer:panGesture];
    [panGesture release];
    //捏合手势
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [aView addGestureRecognizer:pinchGesture];
    [pinchGesture release];
    //旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [aView addGestureRecognizer:rotationGesture];
    [rotationGesture release];
2017-07-27 17:25:16 codeteenager 阅读数 195

点击手势

    //创建点击手势
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapMe)];
    //控制点击此时
    tap.numberOfTapsRequired=3;
    //创建图片控件
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
    //设置图片是可交互的
    imageView.userInteractionEnabled=YES;
    [self.view addSubview:imageView];
    //为图片控件添加手势
    [imageView addGestureRecognizer:tap];

长按手势

    //创建长按手势
    UILongPressGestureRecognizer *longP=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressMe)];
    //控制长按事件(按多少秒触发)
    longP.minimumPressDuration=3;
    //控制长按过程中手指允许移动的范围
    longP.allowableMovement=50;
    [imageView addGestureRecognizer:longP];

轻扫手势

    //创建轻扫手势
    UISwipeGestureRecognizer *swip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipMe)];
    [imageView addGestureRecognizer:swip];

缩放手势

    //缩放手势
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchMe)];
    [imageView addGestureRecognizer:pinch];

拖拽手势

    //拖拽手势
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMe)];
    [imageView addGestureRecognizer:pan];

旋转手势

    //旋转手势
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationMe)];
    [imageView addGestureRecognizer:rotation];
2017-11-23 21:42:56 u012078168 阅读数 1452

版本

Xcode 9.1

1. 六种常用手势:

  • UITapGestureRecognizer:点击手势
  • UIPinchGestureRecognizer:捏合手势(放大缩小)
  • UIRotationGestureRecognizer:旋转手势
  • UISwipeGestureRecognizer:滑动手势
  • UIPanGestureRecognizer:拖动手势
  • UILongPressGestureRecognizer:长按手势

2. 解决手势冲突

手势识别实际上是调用触摸事件来实现的。如果一个手势A的识别部分是另一个手势B的子部分时,默认情况下A就会先识别,B就无法识别了,造成手势冲突。例如拖动手势(UIPanGestureRecognizer)的操作事件是在手势的开始状态(UIGestureRecognizerStateBegan)开始执行的,而滑动手势(UISwipeGestureRecognizer)的操作事件只有在手势结束状态(UIGestureRecognizerStateEnded)才能执行,因此能识别拖动手势而不能识别滑动手势。
解决手势冲突办法:

使用requireGestureRecognizerToFail:方法,指定某个手势执行的前提是另一个手势失败后才会执行。

例如:

    /* 解决手势冲突 */
    // 解决 单击 与 双击 之间的冲突
    [singleTap requireGestureRecognizerToFail:doubleTap];
    // 解决 拖动 与 滑动 之间的冲突
    [pan requireGestureRecognizerToFail:swipeToLeft];
    [pan requireGestureRecognizerToFail:swipeToRight];
    // 解决 拖动 和 长按 之间的冲突
    [longPress requireGestureRecognizerToFail:pan];

3. 手势在视图控件之间的传递(响应链)

和触摸事件一样,默认情况下,子视图(上层视图)触摸事件执行后就不再向父视图(下层视图)传递。如果想继续往下传递手势,可利用代理方法gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer:来实现。此代理方法默认返回NO,会阻断继续向下识别手势,如果返回YES则可以继续向下传递手势。

示例
注意UIImageView默认userInteractionEnabled为NO,这就无法识别手势。本例使用storyboard创建UIImageView并设置userInteractionEnabled为YES。

GestureVC.m

#import "GestureVC.h"

@interface GestureVC () <UIGestureRecognizerDelegate> {

    NSInteger _currentIndex;
}

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation GestureVC

- (void)viewDidLoad {
    [super viewDidLoad];

    /* ----- 点击手势 ----- */
    // 默认单击
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture:)];
    [self.imageView addGestureRecognizer:singleTap];
    // 双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGesture:)];
    doubleTap.numberOfTapsRequired = 2;
    // 需要2根手指一起敲击
//    tap.numberOfTouchesRequired = 2;
    // 把点击手势添加到imageView上
    [self.imageView addGestureRecognizer:doubleTap];

    /* ----- 捏合手势 ----- */
    // 模拟器触发:按住option键,再点鼠标左键
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
    [self.imageView addGestureRecognizer:pinch];

    /* ----- 旋转手势 ----- */
    // 模拟器触发:也是按住option键
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    [self.imageView addGestureRecognizer:rotation];

    /* ----- 滑动手势 ----- */
    // 默认往右滑
    UISwipeGestureRecognizer *swipeToRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToRightGesture:)];
    [self.imageView addGestureRecognizer:swipeToRight];
    // 改成往左滑
    UISwipeGestureRecognizer *swipeToLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToLeftGesture:)];
    swipeToLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipeToLeft];
    // 支持两个方向(左+右/上+下),不同时支持横向和竖向,优先支持横向
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;

    /* ----- 拖动手势 ----- */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [self.imageView addGestureRecognizer:pan];

    /* ----- 长按手势 ----- */
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
    // 设置长按时间
    longPress.minimumPressDuration = 1.0f;
    [self.imageView addGestureRecognizer:longPress];

    /* 解决手势冲突 */
    // 解决 单击 与 双击 之间的冲突
    [singleTap requireGestureRecognizerToFail:doubleTap];
    // 解决 拖动 与 滑动 之间的冲突
    [pan requireGestureRecognizerToFail:swipeToLeft];
    [pan requireGestureRecognizerToFail:swipeToRight];
    // 解决 拖动 和 长按 之间的冲突
    [longPress requireGestureRecognizerToFail:pan];
}


#pragma mark - 手势识别响应方法

// 单击手势
- (void)singleTapGesture:(UITapGestureRecognizer *)gesture {

    NSLog(@"singleTapGesture");

    self.label.hidden = !self.label.hidden;
}


// 双击手势
- (void)doubleTapGesture:(UITapGestureRecognizer *)gesture {

    NSLog(@"doubleTapGesture");

    // 取消一切形变
    self.imageView.transform = CGAffineTransformIdentity;
}


// 捏合手势
- (void)pinchGesture:(UIPinchGestureRecognizer *)gesture {

    NSLog(@"pinchGesture");

    if (gesture.state == UIGestureRecognizerStateChanged) {         // 手势进行中
        // 捏合手势中scale属性记录了缩放比例
        self.imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }else if(gesture.state == UIGestureRecognizerStateEnded) {      // 手势结束
    }
}


// 旋转手势
- (void)rotationGesture:(UIRotationGestureRecognizer *)gesture {

    NSLog(@"rotationGesture");

    if (gesture.state == UIGestureRecognizerStateChanged) {         // 手势进行中
        // 旋转手势中rotation属性记录了旋转弧度
        self.imageView.transform = CGAffineTransformMakeRotation(gesture.rotation);
    }else if(gesture.state == UIGestureRecognizerStateEnded) {      // 手势结束
    }
}


// 向左滑动手势
- (void)swipeToLeftGesture:(UISwipeGestureRecognizer *)gesture {

    NSLog(@"swipeToLeftGesture");

    // 上一张图片
    _currentIndex--;
    if (_currentIndex < 0) {
        _currentIndex = 2;
    }else if (_currentIndex >2) {
        _currentIndex = 0;
    }
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",_currentIndex]];
}


// 向右滑动手势
- (void)swipeToRightGesture:(UISwipeGestureRecognizer *)gesture {

    NSLog(@"swipeToRightGesture");

    // 下一张图片
    _currentIndex++;
    if (_currentIndex < 0) {
        _currentIndex = 2;
    }else if (_currentIndex >2) {
        _currentIndex = 0;
    }
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",_currentIndex]];
}


// 拖动手势
- (void)panGesture:(UIPanGestureRecognizer *)gesture {

    NSLog(@"panGesture");

    if (gesture.state == UIGestureRecognizerStateChanged) {     // 手势进行中
        // 取得在相对根视图(self.view)的移动
        CGPoint translation = [gesture translationInView:self.view];
        // 改变imageView坐标
        self.imageView.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);
    }else if(gesture.state == UIGestureRecognizerStateEnded){
        // 0.5s后取消一切形变
        [UIView animateWithDuration:0.5 animations:^{
            self.imageView.transform = CGAffineTransformIdentity;
        }];
    }
}


// 长按手势
- (void)longPressGesture:(UILongPressGestureRecognizer *)gesture {

    NSLog(@"longPressGesture");

    // 此方法会调用两次(Began & Ended),需判断其手势状态
    if (gesture.state == UIGestureRecognizerStateBegan) {

        // 创建alertController
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除这张图片吗?" preferredStyle:UIAlertControllerStyleActionSheet];
        // 添加确认按钮
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
            // 这里啥也不做
        }]];
        // 添加取消按钮
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            // 这里啥也不做
        }]];
        // 显示alertController
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


@end


效果图
效果图

没有更多推荐了,返回首页