【iOS进阶】【触摸事件】手势识别功能

一、监听触摸事件的做法                  

1>如果想监听一个view上面的触摸事件,之前的做法是

  1.自定义一个view

  2.实现view的touches方法,在方法内部实现具体处理代码

2>通过touches方法监听view触摸事件,有很明显的几个缺点

  1.必须得自定义view

  2.由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件

  3.不容易区分用户的具体手势行为

3>iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度

二、UIGestureRecognizer               

1>为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer

  利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势

2>UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势

    UITapGestureRecognizer(敲击)

    UIPinchGestureRecognizer(捏合,用于缩放)

    UIPanGestureRecognizer(拖拽)

    UISwipeGestureRecognizer(轻扫)

    UIRotationGestureRecognizer(旋转)

    UILongPressGestureRecognizer(长按)

3>手势识别的状态

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    // 没有触摸事件发生,所有手势识别的默认状态
    UIGestureRecognizerStatePossible,
    // 一个手势已经开始但尚未改变或者完成时
    UIGestureRecognizerStateBegan,
    // 手势状态改变
    UIGestureRecognizerStateChanged,
    // 手势完成
    UIGestureRecognizerStateEnded,
    // 手势取消,恢复至Possible状态
    UIGestureRecognizerStateCancelled, 
    // 手势失败,恢复至Possible状态
    UIGestureRecognizerStateFailed,
    // 识别到手势识别
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};

三、UIGestureRecognizer的具体用法            

1>敲击(UITapGestureRecognizer)

#import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //可交互
    self.imageView.userInteractionEnabled = YES;
    //可多点触控
    self.imageView.multipleTouchEnabled = YES;
    [self textTap];

}

#pragma mark - UIGestureRecognizer代理方法 - 主要用于拦截触摸点
/**
 *  当点击view的时候,会先调用这个方法
 */
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    CGPoint pos = [touch locationInView:touch.view];
    NSLog(@"%@",NSStringFromCGPoint(pos));
}

#pragma mark - 敲击手势
/**
 *  常用的做法
 */
-(void)textTap{
#warning 手势识别器-敲击
    //1.创建手势识别器对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    //连续敲击指定次数,手势才能识别成功
    tap.numberOfTapsRequired = 2;
    //两根手指一起敲
    tap.numberOfTouchesRequired = 2;
    
    //2.添加手势识别器到对应的view
    [self.imageView addGestureRecognizer:tap];
    //3.添加监听方法(识别到了对应的手势,就会调用监听方法)
    [tap addTarget:self action:@selector(tapView)];
}

-(void)tapView{
    NSLog(@"hehe");
}

/**
 *  简化的做法
 */
-(void)textTap2{
    //默认一根手指敲击一次
    UIGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
    tap.delegate = self;
    [self.imageView addGestureRecognizer:tap];
}

@end

2>清扫(UISwipeGestureRecognizer)、长按(UILongPressGestureRecognizer)

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] 
                    initWithTarget:self action:@selector(swpieView)]; UISwipeGestureRecognizer
*swipeDown = [[UISwipeGestureRecognizer alloc]
                    initWithTarget:self action:@selector(swpieView)]; UISwipeGestureRecognizer
*swipeLeft = [[UISwipeGestureRecognizer alloc]
                    initWithTarget:self action:@selector(swpieView)]; UISwipeGestureRecognizer
*swipeRight = [[UISwipeGestureRecognizer alloc]
                    initWithTarget:self action:@selector(swpieView)];
//设置清扫方向清扫方向 // swipeUp.direction = UISwipeGestureRecognizerDirectionUp; // swipeDown.direction = UISwipeGestureRecognizerDirectionDown; // swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; // swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self.redView addGestureRecognizer:swipeUp]; [self.redView addGestureRecognizer:swipeDown]; [self.redView addGestureRecognizer:swipeLeft]; [self.redView addGestureRecognizer:swipeRight]; } #pragma mark - 清扫 -(void)swpieView{ NSLog(@"清扫了view"); } #pragma mark - 长按 -(void)textLongPress{ UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                     initWithTarget:self action:@selector(longPressView)];
//至少长按多少秒 longPress.minimumPressDuration = 2.0; //在触发手势之前,指定像素内有效 longPress.allowableMovement =50; [self.redView addGestureRecognizer:longPress]; } -(void)longPressView{ NSLog(@"长按了view"); } @end

1>旋转(UIRotationGestureRecognizer)、缩放(UIPinchGestureRecognizer)

#import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self testPinch];
    [self testRotate];
    
}
#pragma mark - 手势识别器的代理方法
/**
 *  是否允许多个手势识别器同时有效
 *  Simultaneously : 同时地
 */
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } #pragma mark  - 缩放 -(void)testPinch{ UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)]; pinch.delegate =self; [self.imageView addGestureRecognizer:pinch]; } -(void)pinchView:(UIPinchGestureRecognizer *)pinch{ //scale-缩放比例 pinch.view.transform = CGAffineTransformScale(pinch.view.transform,pinch.scale,pinch.scale); pinch.scale = 1; NSLog(@"缩放-----%f",pinch.scale); } #pragma mark  - 旋转 -(void)testRotate{ UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]
                    initWithTarget:self action:@selector(rotateView:)]; rotate.
delegate = self; [self.imageView addGestureRecognizer:rotate]; } //触发了手势就会把手势对象传给你 -(void)rotateView:(UIRotationGestureRecognizer *)rotate{ rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation); #warning 这个设置很重要,每转一次就要清0一次 rotate.rotation = 0; NSLog(@"旋转"); } @end

1>缩放(UIPanGestureRecognizer)

#import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *yellowView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self.yellowView addGestureRecognizer:pan];
}

-(void)panView:(UIPanGestureRecognizer *)pan{
    switch (pan.state) {
        case UIGestureRecognizerStateBegan://开始出发手势
            break;
        case UIGestureRecognizerStateEnded://手势结束
            break;
        default:
            break;
    }
    
    //获取在view上面挪动的距离
    CGPoint pos = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += pos.x;
    center.y += pos.y;
    pan.view.center = center;
    //清空移动的距离
    [pan setTranslation:CGPointZero inView:pan.view];
    
    NSLog(@"拖拽--------%@", NSStringFromCGPoint(pos));
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"View最后的位置------%@",NSStringFromCGPoint(self.yellowView.center));
}

@end

 

posted @ 2015-04-15 20:52  锟斤拷Dy  阅读(203)  评论(0)    收藏  举报