手势识别:UIGestureRecongnizer


1、UIGestureRecognizer    

    UIGestureRecognizer基类是一个抽象类,它的子类:

       UITapGestureRecognizer

       UIPinchGestureRecognizer

       UIRotationGestureRecognizer

       UISwipeGestureRecognizer

       UIPanGestureRecognizer

       UILongPressGestureRecognizer

         

    代码实现:

    创建一个单视图工程,在viewcontroller.h 中声明一个属性

    @property(retain,nonatomic)UIView *pView;(不要忘了释放它喔)

     在实现文件里
  

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    //==============================创建单击手势识别器============================//

    //创建点击手势识别器

   UITapGestureRecognizer *pSingleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(singleGesture:)];

    //将手势加入到当前视图

    [self.viewaddGestureRecognizer:pSingleTap];

    //==============================创建双击手势识别器============================//

    //创建双击手势识别器

    UITapGestureRecognizer *pDoubleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(doubleTap:)];

    //设置敲击次数

    pDoubleTap.numberOfTapsRequired =2;

    //添加到视图

    [self.viewaddGestureRecognizer:pDoubleTap];

    //双击时让单击放弃响应

    [pSingleTap requireGestureRecognizerToFail:pDoubleTap];

    //释放对象

    [pSingleTaprelease];


    [pDoubleTaprelease];

    

    //========================滑动手势========================================//

    

    UISwipeGestureRecognizer *pSwip = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipGesture:)];

    //设置滑动方向为向右滑动

    pSwip.direction =UISwipeGestureRecognizerDirectionRight;

    

    [self.viewaddGestureRecognizer:pSwip];

    

    [pSwiprelease];

    //=================================长按手势============================//

    

    UILongPressGestureRecognizer *pLongPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressed:)];

    //设置最小长按时间

    pLongPress.minimumPressDuration =2;

    

    [self.viewaddGestureRecognizer:pLongPress];

    

    [pLongPressrelease];

    

    //===================================创建旋转手势识别器=====================//

    

    UIRotationGestureRecognizer *pRotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationGesture:)];

    

    [self.viewaddGestureRecognizer:pRotation];

    

    [pRotationrelease];

    

    //======================创建UIPanGestureRecognizer对=========================================//

    

    self.pView = [[UIViewalloc]initWithFrame:CGRectMake(100,100, 50, 50)];

    

    self.pView.backgroundColor = [UIColorredColor];

    

    [self.viewaddSubview:self.pView];

    

    UIPanGestureRecognizer *pPan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panGesture:)];

    

    [self.viewaddGestureRecognizer:pPan];

    

    [pPanrelease];

    

}

//单击对应的方法

- (void)singleGesture:(UITapGestureRecognizer *)tap

{

   NSLog(@"单击");

}

//双击对应的方法

- (void)doubleTap:(UITapGestureRecognizer *)tap

{

   NSLog(@"双击");

}

//滑动对应的方法

- (void)swipGesture:(id)sender

{

   NSLog(@"向右滑动");

}

//长按对应的方法

- (void)longPressed:(id)sender

{

   NSLog(@"长按");

}

//旋转对应的方法

- (void)rotationGesture:(UIRotationGestureRecognizer *)sender

{

   NSLog(@"旋转手势");

    

   float degree = sender.rotation * (180/M_PI);

    

   NSLog(@"旋转角度为:%.2f",degree);

}

//拖拽对应的方法

- (void)panGesture:(UIPanGestureRecognizer *)tap

{

   CGPoint point = [tap locationInView:self.view];

    

   self.pView.center = point;

    

    NSLog(@"%@",NSStringFromCGPoint(point));

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值