iOS开发中的几个手势操作实例分享

手势操作---识别单击还是双击
在视图上同时识别单击手势和双击手势的问题在于,当检测到一个单击操作时,无法确定是确实是一个单击操作或者只是双击操作中的第一次点击。解决这个问题的方法就是:在检测到单击时,需要等一段时间等待第二次点击,如果没有第二次点击,则为单击操作;如果有第二次点击,则为双击操作。
检测手势有两种方法,一种是定制子视图,重写视图从UIResponder类中继承来的事件处理方法,即touchesBegan:withEvent:等一系列方法来检测手势;另一个方法是使用手势识别器,即UIGestureRecognizer的各种具体子类。
一.重写事件处理方法

代码如下:

- (id)init { 
    if ((self = [super init])) { 
        self.userInteractionEnabled = YES; 
        self.multipleTouchEnabled = YES; 
        // ... 
    } 
    return self; 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self]; 
 
    if (touch.tapCount == 1) { 
        [self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3]; 
    }else if(touch.tapCount == 2) 
    { 
        [self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]]; 
    } 

 
-(void)handleSingleTap:(NSValue*)pointValue 

    CGPoint touchPoint = [pointValue CGPointValue]; 
    //... 

 
-(void)handleDoubleTap:(NSValue*)pointValue 

    CGPoint touchPoint = [pointValue CGPointValue]; 
    //... 
}

首先确认定制视图的userInteractionEnabled和multipleTouchEnabled属性都为YES.
在touchesEnded:withEvent:方法中,如果是第一次触摸结束,则cancelPreviousPerformRequestsWithTarget:方法不会起作用,因为self未调度任何方法,此时tapCount为1,使用performSelector:withObject:afterDelay:调用单击事件处理方法,在0.3s钟后执行。

代码如下:

[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];

如果这是一个单击操作,则后面0.3钟内不会再有触摸事件,则handleSingleTap:方法执行,这样识别出了单击操作。
如果这是一个双击操作,则第二次点击在0.3s内触发,在第二次触摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先会取消之前对handleSingleTap:方法的调度,使之不会执行,然后在调用handleDoubleTap:方法处理双击操作。
二.使用Gesture Recognizer
使用Gesture Recognizer识别就会简单许多,只需添加两个手势识别器,分别检测单击和双击事件,设置必要的属性即可。

代码如下:

- (id)init { 
    if ((self = [super init])) { 
    self.userInteractionEnabled = YES; 
        UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)]; 
        singleTapGesture.numberOfTapsRequired = 1; 
        singleTapGesture.numberOfTouchesRequired  = 1; 
        [self addGestureRecognizer:singleTapGesture]; 
 
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)]; 
        doubleTapGesture.numberOfTapsRequired = 2; 
        doubleTapGesture.numberOfTouchesRequired = 1; 
        [self addGestureRecognizer:doubleTapGesture]; 
 
        [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture]; 
    } 
    return self; 

-(void)handleSingleTap:(UIGestureRecognizer *)sender{ 
    CGPoint touchPoint = [sender locationInView:self]; 
    //... 

-(void)handleDoubleTap:(UIGestureRecognizer *)sender{ 
    CGPoint touchPoint = [sender locationInView:self]; 
    //... 
}

唯一需要注意的是

代码如下:

[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];

这句话的意思时,只有当doubleTapGesture识别失败的时候(即识别出这不是双击操作),singleTapGesture才能开始识别,同我们一开始讲的是同一个问题。

UIGestureRecognizer小应用
1、轻拍手势:双指、单击,修改imageView的frame为(0,0,320,200)
2、长按手指:单指,修改imageView的alpha=0.5
3、实现平移、旋转、捏合
4、轻扫:竖向轻扫实现图:像随机切换显示;横向轻扫实现:图像消失,随机修改imageview的背景颜色
5、imageview每次只能添加一种手势识别器。

代码如下:

#define _originalRect CGRectMake(10, 50, 300, 450) 
#define _originalImageName  @"h4.jpeg" 
 
#import "HMTRootViewController.h" 
 
@interface HMTRootViewController (){ 
 
    UITapGestureRecognizer       * _tapGesture; 
    UILongPressGestureRecognizer * _longGesture; 
    UIPanGestureRecognizer       * _panGesture; 
    UIRotationGestureRecognizer  * _rotateGesture; 
    UIPinchGestureRecognizer     * _pinchGesture; 
    UISwipeGestureRecognizer     * _verticalSwipeGesture; 
    UISwipeGestureRecognizer     * _horizontanlSwipeGesture; 
    BOOL isTopDownOfRightLeft;    // 垂直滑动是YES,水平滑动是NO 
     

 
@property (nonatomic,retain) UIButton * button; 
@property (nonatomic,retain) UIImageView * imageView; 
 
@end 
 
@implementation HMTRootViewController 
 
- (void)dealloc{ 
     
    RELEASE_SAFELY(_imageView); 
    RELEASE_SAFELY(_button); 
    [super dealloc]; 
 

 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
        // Custom initialization 
        isTopDownOfRightLeft = YES; 
    } 
    return self; 

 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
     
    [self createButtonView]; 
    [self createImageView]; 
 

 
#pragma mark - 设置图像 
- (void)createImageView{ 
     
    self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_originalImageName]]; 
    _imageView.frame = CGRectMake(10, 50, 300, 450); 
    _imageView.userInteractionEnabled = YES; 
    [self.view addSubview:_imageView]; 
    [_imageView release]; 

 
 
 
#pragma mark - 设置手势 
 
#pragma mark  点击手势 
- (void)createTapGestureRecognizer{ 
    
 
    _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapGestureRecognizer:)]; 
    _tapGesture.numberOfTapsRequired = 1; 
    _tapGesture.numberOfTouchesRequired = 2; 
    [self.imageView addGestureRecognizer:_tapGesture]; 
    [_tapGesture release]; 
 

 
- (void)TapGestureRecognizer:(UITapGestureRecognizer *)tapGesture{ 
 
    self.imageView.frame = CGRectMake(0, 0, 320, 200); 
    NSLog(@"%@",NSStringFromCGRect(self.imageView.frame)); 
 
}  
 
#pragma mark  长按手势 
- (void)createLongGestureRecognizer{ 
     
    _longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGestureRecognizer:)]; 
    _longGesture.numberOfTouchesRequired = 1; 
    _longGesture.minimumPressDuration = 1.0; 
    [self.imageView addGestureRecognizer:_longGesture]; 
    [_longGesture release]; 
     

 
- (void)longGestureRecognizer:(UILongPressGestureRecognizer *)longGesture{ 
 
    self.imageView.alpha = 0.5; 
    NSLog(@"%s",__FUNCTION__); 
 

 
#pragma mark 平移拖拽手势 
- (void)createPanGestureRecognizer{ 
     
    _panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_panGesture]; 
    [_panGesture release]; 
     

 
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
  
    CGPoint txty = [panGesture translationInView:self.view]; 
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, txty.x, txty.y); 
     
    [panGesture setTranslation:CGPointMake(0, 0) inView:self.view]; 
     

 
#pragma mark 旋转手势 
- (void)createRotationGestureRecognizer{ 
     
     
    _rotateGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_rotateGesture]; 
    [_rotateGesture release]; 
     

 
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)rotateGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotateGesture.rotation); 
    rotateGesture.rotation = 0; 
     

 
#pragma mark 捏合缩放手势 
- (void)createPinchGestureRecognizer{ 
     
    _pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_pinchGesture]; 
    [_pinchGesture release]; 
     

 
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinchGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale); 
    pinchGesture.scale = 1; 
     

 
#pragma mark - 轻扫手势 
#pragma mark 上下 竖 垂直轻扫 
- (void)createVerticalSwipeGestureRecognizer{ 
    
    _verticalSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)]; 
    _verticalSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; 
    [self.imageView addGestureRecognizer:_verticalSwipeGesture]; 
    [_verticalSwipeGesture release]; 

 
#pragma mark 水平 左右轻扫 
- (void)createHorizontanlSwipeGesture{ 
     
    _horizontanlSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)]; 
    _horizontanlSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight; 
    [self.imageView addGestureRecognizer:_horizontanlSwipeGesture]; 
     

 
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    
    if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown)) { 
        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%i.jpeg",arc4random()%7+1]]; 
        ; 
 
    }else if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)){ 
     
        self.imageView.image = nil; 
        self.imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]; 
    } 

 
 
#pragma mark - 设置按钮 
- (void)createButtonView{ 
     
    NSArray * buttonArray = @[@"轻点",@"长按",@"平移",@"旋转",@"捏合",@"轻扫"]; 
     
    for (int i = 0; i < [buttonArray count]; i++) { 
         
        self.button = [UIButton buttonWithType:UIButtonTypeSystem]; 
        _button.frame = CGRectMake(10+50*i, 500, 50, 48); 
        [_button setTitle:[buttonArray objectAtIndex:i] forState:UIControlStateNormal]; 
        [_button addTarget:self action:@selector(onClikButton:) forControlEvents:UIControlEventTouchUpInside]; 
        _button.tag = i; 
        [self.view addSubview:_button]; 
    } 
   

 
- (void)onClikButton:(UIButton *)button{ 
     
    [self resetImageView]; 
    switch (button.tag) { 
        case 0: 
   
            [self createTapGestureRecognizer]; 
            break; 
        case 1: 
  
            [self createLongGestureRecognizer]; 
            break; 
        case 2: 
         
            [self createPanGestureRecognizer]; 
            break; 
        case 3: 
            
            [self createRotationGestureRecognizer]; 
            break; 
        case 4: 
             
            [self createPinchGestureRecognizer]; 
            break; 
        case 5: 
            if (isTopDownOfRightLeft == YES) { 
                [self createVerticalSwipeGestureRecognizer]; 
                isTopDownOfRightLeft = NO; 
            } else { 
                [self createHorizontanlSwipeGesture]; 
                isTopDownOfRightLeft = YES; 
            } 
            break; 
        default: 
            break; 
    } 
     

 
#pragma mark - 重置imageView 
- (void)resetImageView 

    for (int i = 0; i < [self.imageView.gestureRecognizers count]; i++) { 
        [self.imageView removeGestureRecognizer:[self.imageView.gestureRecognizers objectAtIndex:i]]; 
    } 
    self.imageView.alpha = 1.0; 
    self.imageView.transform = CGAffineTransformIdentity; 
    self.imageView.frame = _originalRect; 
    self.imageView.image = [UIImage imageNamed:_originalImageName]; 

 
 
- (void)didReceiveMemoryWarning 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 

 
@end

(0)

相关推荐

  • iOS手势密码的实现方法

    本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上效果图 其实就是对画图功能的一个实现,再加上手势操作结合起来. 屏幕宽度高度,方便下面操作,不做解释 #define ScreenHeight [[UIScreen mainScreen] bounds].size.height #define ScreenWidth [[UIScreen mainScreen] bounds].size.width 控制器.m文件 这里的imageView是用来装手势画图之后的

  • iOS实现手势解锁操作

    本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格页面 2.实现按钮被点击及滑动过程中按钮状态的改变 3.实现滑动过程中的连线 4.绘制完毕后判定密码是否正确, 5.密码判定后实现跳转. 下面我们就来用代码实现上述五个过程. 1.加载九宫格界面 1.1九宫格内控件的分布 3*3 ,我们可以自定义view(包含3*3个按钮),添加到viewContr

  • 使用Swift代码实现iOS手势解锁、指纹解锁实例详解

    一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeedsDisplay,这样就会自动调用drawRect方法). 1.3.当手指在屏幕上滑动时,调用重写的touchesEnded:withEvent方法. 这两个方法执行的操作是一样的:通过locationInView获取 触摸的坐标,然后用 CGRectContainsPoint 判断手指是否经过UIB

  • iOS手势的实现方法

    本文实例为大家分享了iOS手势的具体实现代码,供大家参考,具体内容如下 效果 细节 1.UITouch #import "ViewController_0.h" @interface ViewController_0 () @property (nonatomic, strong)UILabel *label; @end @implementation ViewController_0 - (void)viewDidLoad { [super viewDidLoad]; self.la

  • IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)

    下面通过图文并茂的方式给大家分享下IOS手势操作(拖动.捏合.旋转.点按.长按.轻扫.自定义)的相关内容. 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotatio

  • 基于JS实现Android,iOS一个手势动画效果

    废话不多说了,先给大家展示下效果图: 这是iOS下的效果,android下完全一致.通过do_GestureView组件和do_Animation组件,deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过手势控制图片上下动画滑动实现开合效果,还支持声音效果. 下面是主要的代码 //index.ui.js var do_Animator1 = mm("do_Animator"); do_Animator1.append(500, { y: -1334, curve:

  • iOS轻点、触摸和手势代码开发

    一.响应者链 以UIResponder作为超类的任何类都是响应者.UIView和UIControl是UIReponder的子类,因此所有视图和所有控件都是响应者. 1.初始相应器 事件首先会传递给UIApplication对象,接下来会传递给应用程序的UIWindow,UIWindow会选择一个初始相应器来处理事件.初始响应器会选择下面的方式选择1.对于触摸事件,UIWindow会确定用户触摸的视图,然后将事件交给注册了这个视图的手势识别器或则注册视图层级更高的手势识别器.只要存在能处理事件的识

  • iOS仿邮箱大师的九宫格手势密码解锁

    本文实例为大家分享了iOS手势密码解锁的相关代码,供大家参考,具体内容如下 // // LockView.m // 手势解锁 // // Created by Daniel on 16/4/4. // Copyright © 2016年 Daniel. All rights reserved. // #import "LockView.h" @interface LockView () /** 保存已选中的按钮 */ @property(nonatomic, strong) NSMut

  • iOS开发之触摸事件以及手势

    iOS中的事件分为三类:触摸事件.加速计事件.远程控制事件.只有继承了UIResponder的对象才能接收并处理事件,称之为"响应者对象".UIApplication.UIViewController.UIView都继承自UIResponder.UIResponder内部提供的方法来处理事件: 触摸事件:touchesBegan.touchesMoved.touchesEnded.touchesCancelled 加速计事件:motionBegan.motionEnded.motion

  • iOS开发之手势识别

    一.UIGestureRecognizer简单介绍 我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸事件处理都需要实现3个touches方法,比较繁琐,实际上我们可以使用更加简单的触摸事件处理操作,那就是 手势识别UIGestureRecognizer . 手势识别操作基类UIGestureRecognizer不能直接使用,我们常使用的是它的子类,这些手势操作类都继承自UIGestureRecognizer类 常用手势识别子类: 点按手势 UITapGestureRecognize

随机推荐