iOS-个人整理07 - touch触摸事件和手势识别器

一、touch触摸事件

在给定的触摸阶段,如果发生触摸事件,应用程序就会发送下列消息
//触屏开始-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//触屏结束,手指离开屏幕-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//滑动-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//触屏取消,触摸被打断,来电话,短信等-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

下面的TouchView实现了创建一块View,点击变成红色,触摸离开变成黑色,按住拖拽会变成随机色,同时View的图块也会随着拖拽而移动

#import "TouchView.h"  
  
@implementation TouchView  
  
//每一个UIView或者其子类,都可以实现触摸事件,重写系统提供的方法  
  
//触摸开始,触摸对象触摸屏幕  
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    NSLog(@"%s,开始",__func__);  
    self.backgroundColor = [UIColor redColor];  
}  
  
//触摸对象在屏幕上移动  
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    NSLog(@"%s,移动",__func__);  
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];  
      
    //得到某个触摸对象,如果支持多点触摸,可得到多个对象  
    UITouch *touch = touches.anyObject;  
    //得到触摸对象当前点  
    CGPoint locationPoint = [touch locationInView:self.superview];  
    //得到移动之前所在的点  
    CGPoint prePoint = [touch previousLocationInView:self.superview];  
    //计算delX delY,就是x,y所移动的距离  
      
    float delX = locationPoint.x - prePoint.x;  
    float delY = locationPoint.y - prePoint.y;  
      
    CGRect frame = self.frame;  
      
    //获取整个屏幕宽度  
    float screenWidth = [UIScreen mainScreen].bounds.size.width;  
    float screenHeight = [UIScreen mainScreen].bounds.size.height;  
      
    float x = frame.origin.x;  
    float y = frame.origin.y;  
  
    //让视图无法拖出屏幕的范围  
    if (x + delX < 0 || y + delY < 0 || x + frame.size.width + delX > screenWidth || y +frame.size.height + delY > screenHeight) {  
        NSLog(@"超出");  
    }  
    else  
    {  
        self.center = CGPointMake(self.center.x + delX, self.center.y + delY);  
    }  
    //移动中改变视图大小  
    //self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width + delX, self.frame.size.height + delY);  
}  
  
//手指离开屏幕  
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    NSLog(@"%s,结束",__func__);  
    self.backgroundColor = [UIColor blackColor];  
}  
  
//触摸被打断,来电话,短信等  
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    NSLog(@"%s,打断",__func__);  
}  
@end  

二、手势识别器

手势识别器是对触摸事件进行了封装,本身起到了识别作用
传说中IOS有七种手势
轻拍 UITapGestureRecognizer
轻扫 UISwipeGestureRecognizer
沿屏幕边缘轻扫 UIScreenEdgePanGestureRecognizer
捏合 UIPinchGestureRecognizer
旋转 UIRotationGestureRecognizer
平移 UIPanGestureRecognizer
长按 UILongPressGestureRecognizer

下面先使用了轻拍,轻扫和沿屏幕边缘轻扫。
基本过程
1.先初始化手势

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

2.设置手势属性

//需要的触摸次数  
   tagGesture.numberOfTapsRequired = 1;  
   //需要的触摸对象的个数  
   tagGesture.numberOfTouchesRequired = 2;  

3.给View添加手势

[self.view addGestureRecognizer:tagGesture];  

4.实现手势方法

-(void)tapAction:(UITapGestureRecognizer *)sender  
{  
    NSLog(@"轻拍");  
}  

#import "RootViewController.h"  
#import "TapView.h"  
  
@interface RootViewController ()  
  
@end  
@implementation RootViewController  
  
  
-(void)viewDidLoad  
{  
    [super viewDidLoad];       
  
     //1.创建轻拍手势    
    UITapGestureRecognizer *tagGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];  
    [headImageView addGestureRecognizer:tagGesture];  
    //需要的触摸次数  
    tagGesture.numberOfTapsRequired = 1;  
    //需要的触摸对象的个数  
    tagGesture.numberOfTouchesRequired = 2;  
    [self.view addGestureRecognizer:tagGesture];  
  
    //2.创建轻扫手势  
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; 
    //扫动方向 
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeGesture]; 
    //3.创建屏幕边缘轻扫手势 
UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [self.view addGestureRecognizer:pan];  
    //边缘设置  
    pan.edges = UIRectEdgeLeft; 
    //指定手势代理 
    swipeGesture.delegate = self;}  
#pragma mark 手势代理
//如果视图添加了多个手势,此代理方法如果返回值为YES:运行识别多个手势势 
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:  
          (UIGestureRecognizer *)otherGestureRecognizer{ return YES;}  
  
 //手势开关  
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer  
{   
    //判断一个视图的类型,如果是button类型,就不响应手势,这样点击button和手势就不会同时触发   
    if ([[gestureRecognizer view] isKindOfClass:[UIButton class]])   
    {  
         return NO;   
    }   
    return YES;  
}  
  
//轻拍手势触发的方法  
-(void)tapAction:(UITapGestureRecognizer *)sender  
{  
    //得到轻拍的点  
    //获取点  
    CGPoint point=[sender locationInView:sender.view];  
    NSLog(@"轻拍");  
}  
  

下面代码实现了捏合,选择,长按,平移手势
这里还涉及了View的图像变化
UIview有一个transform属性,可以对View进行旋转,缩放平移等操作,按住option键可以实现在模拟器上进行两指操作

#import "SecondViewController.h"  
//需要遵守代理协议  
@interface SecondViewController ()  
  
@end  
  
@implementation SecondViewController  
  
-(void)viewDidLoad  
{  
    [super viewDidLoad];  
      
    //初始化一个UIImageView  
    UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];  
    [myImageView setImage:[UIImage imageNamed:@"background.jpg"]];  
    myImageView.center = self.view.center;  
    [self.view addSubview:myImageView];  
    myImageView.userInteractionEnabled = YES;  
      
#pragma mark 手势2  
      
    //旋转手势  
    UIGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];  
    //添加手势  
    [myImageView addGestureRecognizer:rotation];  
    //设置代理  
    rotation.delegate = self;  
      
    //捏合手势  
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];  
    [myImageView addGestureRecognizer:pinch];  
    //设置代理  
    pinch.delegate = self;  
      
    //平移手势  
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];  
    [myImageView addGestureRecognizer:pan];  
      
    //长按手势  
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];  
    //长按时间,默认0.5  
    longPress.minimumPressDuration = 1;  
    //长按时可移动范围  
    longPress.allowableMovement = 100;  
    [myImageView addGestureRecognizer:longPress];  
    //添加代理  
    longPress.delegate = self;  
      
}  
  
//长按方法  
-(void)longPressAction:(UILongPressGestureRecognizer*)sender  
{  
    //进行判断,长按开始的时候,不设置则长按离开也会触发  
    if (sender.state == UIGestureRecognizerStateBegan) {  
       NSLog(@"长按");  
    }  
    else if (sender.state == UIGestureRecognizerStateEnded)  
    {  
        NSLog(@"长按结束");  
    }  
    else if (sender.state == UIGestureRecognizerStateChanged)  
    {  
        NSLog(@"长按变化");  
    }  
    
}  
  
#pragma mark 实现手势方法  
//平移方法  
-(void)panAction:(UIPanGestureRecognizer*)sender  
{  
    UIView *myView = [sender view];  
    //得到偏移量  
    CGPoint translation = [sender translationInView:myView];  
    //通过2D仿射函数中与位移相关的函数来改变视图位置  
    //tx,ty X,Y方向的位移  
    myView.transform = CGAffineTransformTranslate(myView.transform, translation.x, translation.y);  
    //设置偏移归位CGPointZero = CGPointMake(0,0)  
    [sender setTranslation:CGPointZero inView:myView];  
      
}  
  
//捏合方法  
-(void)pinchAction:(UIPinchGestureRecognizer*)sender  
{  
    UIView *myView = [sender view];  
    //缩放大小  
    float myScale = sender.scale;  
    //通过2D仿射变换函数与缩放有关的函数改变视图大小  
    myView.transform = CGAffineTransformScale(myView.transform, myScale, myScale);  
    //复原  
    sender.scale = 1;  
}  
  
-(void)rotationAction:(UIRotationGestureRecognizer*)sender  
{  
    //手势作用的视图  
    UIView *myView = [sender view];  
      
    //通过2D仿射变换函数与旋转相关的函数对视图旋转  
    //第一个参数:作用视图的transfrom  
    //第二个参数:旋转的角度  
    float rotation = sender.rotation;  
    myView.transform = CGAffineTransformRotate(myView.transform, rotation);  
      
    //myView.transform = CGAffineTransformMakeRotation(M_PI_2);  
    //每进行一次旋转,都需要将旋转角度复原,让其在原有基础上再次累加  
    sender.rotation = 0;  
}  
  
//实现代理方法,可以同时响应多个手势  
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  
{  
    return YES;  
}  
  
@end  

你可能感兴趣的:(iOS-个人整理07 - touch触摸事件和手势识别器)