IOS--手势控制的使用

     手势识别是具有互斥的原则的,比如单击和双击,如果它识别出一种手势,其后的手势将不被识别

    // 添加单击的手势UITapGestureRecognize

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];

    [tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法

    [self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View上

    

 // 添加双击的手势UITapGestureRecognize

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];

    tapGestureRecognizer.numberOfTapsRequired = 2; // 设置单击几次才触发方法

    [tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法

    [self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View上

    

      添加长按的手势UILongPressGestureRecognizer

     注意:会调用两次方法,开始长按调用一次  松开后再调用一次  当长按并且滑动的时候,会多次调用长按的方法

    UILongPressGestureRecognizer *pressLongGestureRecognizer = [[UILongPressGestureRecognizeralloc] init];

    [pressLongGestureRecognizer addTarget:self action:@selector(pressLongGestureAction:)]; // 给长按手势添加方法

    [self.view addGestureRecognizer:pressLongGestureRecognizer]; // 添加到当前的View上

  

     添加捏合的手势UIPinchGestureRecognizer

    注意:捏合手势不是捏合一次调用一次方法,而是在捏合的过程中不停的调用方法

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];

    [pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)]; // 添加捏合手势的方法

    [self.view addGestureRecognizer:pinchGestureRecognizer]; // 添加到当前的View上

     添加旋转的手势UIRotationGestureRecognizer

     注意:旋转手势是两指同时进行旋转

    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizeralloc] init];

    [rotationGestureRecognizer addTarget:self action:@selector(rotationGestureAction:)]; // 给旋转手势添加方法

    [self.view addGestureRecognizer:rotationGestureRecognizer]; // 添加到当前的View上

    

     添加滑动的手势(轻扫手势) UISwipeGestureRecognizer

    注意: 快速移动,是用于监测滑动的方向的

    UISwipeGestureRecognizer *swipGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];

    swipGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; // 添加手势的方法

    // 以下是设置滑动的方向

    // typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {

    //    UISwipeGestureRecognizerDirectionRight = 1 << 0, // 从左向右滑动

    //    UISwipeGestureRecognizerDirectionLeft  = 1 << 1, // 从右向左滑动

    //    UISwipeGestureRecognizerDirectionUp    = 1 << 2, // 从下向上滑动

    //    UISwipeGestureRecognizerDirectionDown  = 1 << 3  // 从上向下滑动

    // };

    [swipGestureRecognizer addTarget:self action:@selector(swipGestureAction:)]; // 给滑动手势添加方法

    [self.view addGestureRecognizer:swipGestureRecognizer]; // 添加到当前的View上

    

    

    

    添加拖移手势(平移手势) UIPanGestureRecognizer

    注意:慢速移动,是用于监测偏移的量的

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];

    [panGestureRecognizer addTarget:self action:@selector(panGestureAction:)]; // 添加托移收拾的方法

    [self.view addGestureRecognizer:panGestureRecognizer]; // 添加到当前的View

    [panGestureRecognizer release], panGestureRecognizer = nil; // 释放内存

 

#pragma mark - 实现单击手势的方法

- (void)tapGestureAction:(UITapGestureRecognizer *) sender {

    NSLog(@"您  轻拍   了屏幕");

}

#pragma mark - 实现长按手势的方法

- (void)pressLongGestureAction:(UILongPressGestureRecognizer *) sender {

    NSLog(@"您   长按   了屏幕");

}

#pragma mark - 实现了捏合手势的方法

- (void)pinchGestureAction:(UIPinchGestureRecognizer *) sender {

    NSLog(@"您   捏合   了屏幕");

}

#pragma mark - 实现旋转手势的方法

- (void)rotationGestureAction:(UIRotationGestureRecognizer *) sender {

    NSLog(@"您使用了   旋转   手势");

}

#pragma mark - 实现滑动手势的方法

- (void)swipGestureAction:(UISwipeGestureRecognizer *) sender {   

    NSLog(@"您   滑动    了屏幕");

}

#pragma mark - 实现了托移手势的方法

- (void)panGestureAction:(UIPanGestureRecognizer *) sender {

    NSLog(@"您   托移   了。。。。");

}

晃动手势

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

{

    application.applicationSupportsShakeToEdit = YES;

    return YES;

}

-(BOOL)canBecomeFirstResponder {

    return YES;

}

-(void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    [self becomeFirstResponder];

}

- (void)viewWillDisappear:(BOOL)animated {

    [self resignFirstResponder];

    [super viewWillDisappear:animated];

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

    if (motion == UIEventSubtypeMotionShake) {

        NSLog(@"检测到晃动");

    }

}

只要有梦想,人生就有意义。。。
原文地址:https://www.cnblogs.com/jiafuyang/p/4829319.html