UIGestureRecognizer手势使用


  手势识别器

 .能够为系统提供的视图对象添加触摸事件的响应方法(UIView,UILabel,UIImageView);

 .内部封装了手势识别的过程,我们只需要把中心放在手势识别之后的对用操作上.


UIGestureRecognizer是手势识别器的一个抽象的基类,值提供手势识别器基本的功能.使用时使用具体的子类.

首先创建一个视图

UIView *redView = [[UIViewalloc] initWithFrame:[[UIScreenmainScreen]bounds]];

    redView.backgroundColor = [UIColororangeColor];

    [self.view addSubview:redView];

    [redView release];


1.轻拍手势

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

    //设置轻拍的次数

    tapGesture.numberOfTapsRequired = 2;

    //设值轻拍是需要的手指个数

    tapGesture.numberOfTouchesRequired =2;

    //给视图添加轻拍手势

    [redView addGestureRecognizer:tapGesture];

    //切记释放

    [tapGesture release];

2.长按手势

UILongPressGestureRecognizer

长按手势是连续的。手势开始(UIGestureRecognizerStateBegan)时,允许手指(numberOfTouchesRequired)的数量已按指定期间内(minimumPressDuration)和触摸不超越运动(allowableMovement)的允许范围内。该手势识别过渡到状态改变时手指移动,并结束(UIGestureRecognizerStateEnded)当任一手指被取消。

属性

  •    minimumPressDuration  property     触摸的时间长短
  •    numberOfTouchesRequired  property  允许手指的触摸的次数
  •    numberOfTapsRequired  property    允许手指的数量
  •    allowableMovement  property   触摸期间是否移动UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(handeleLongPressGesture:)];    
  •     longPressGesture.minimumPressDuration =0.5;

        longPressGesture.numberOfTapsRequired =2;

        [redView addGestureRecognizer:longPressGesture];

        [longPressGesture release];


- (void)handeleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture{

    //判断它的长按时间内触发

    if (UIGestureRecognizerStateBegan == longPressGesture.state) {

        NSLog(@"呵呵呵");

    }

}

3.轻扫手势(轻划手势)

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

    [redView addGestureRecognizer:swipGesture];

    //设置轻扫方向,让左右方向都存在

    //只要枚举值中有左移运算符,代表枚举值可以通过按位或,让多个枚举值同事存在.

    //警告:上下和左右在一个手势识别器中不能同时存在

    //如果想让上下左右四个方法都有,那只能创建两个轻扫手势对象,一个支持上下,一个支持左右.

    swipGesture.direction =UISwipeGestureRecognizerDirectionLeftUISwipeGestureRecognizerDirectionRight;

    swipGesture.direction =UISwipeGestureRecognizerDirectionDownUISwipeGestureRecognizerDirectionUp;

    //创建手势后释放内存

    [swipGesture release];


4.平移手势

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

    [redView addGestureRecognizer:panGesture];//添加到视图上

    [panGesture release];

//处理平移手势

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture{

    //获取平移之前后的改变量

    CGPoint point = [panGesture translationInView:panGesture.view];

    panGesture.view.transform =CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);

    [panGesture setTranslation:CGPointZeroinView:panGesture.view];

}


5.捏合手势

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(handlePinch:)];

    [redView addGestureRecognizer:pinchGesture];

    [pinchGesture release];

处理捏合手势

- (void)handlePinch:(UIPinchGestureRecognizer *)pinch{

    pinch.view.transform =CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

    //将之前的变化比例置1

    pinch.scale =1;

}

6.旋转手势

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

    [redView addGestureRecognizer:rotationGesture];

    [rotationGesture release];

处理旋转手势

- (void)handleRotation:(UIRotationGestureRecognizer *)roration{

    roration.view.transform =CGAffineTransformRotate(roration.view.transform, roration.rotation);//roration.roration获取改变的角度

    //将之前改变的角度rotation0

    roration.rotation =0;

}

7.屏幕边缘手势

UIScreenEdgePanGestureRecognizer *screenPanGesture = [[UIScreenEdgePanGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleScreen:)];

    [redView addGestureRecognizer:screenPanGesture];

    //设屏幕边缘

    screenPanGesture.edges = UIRectEdgeLeft;

//几个方向枚举值

typedefNS_OPTIONS(NSUInteger, UIRectEdge) {

    UIRectEdgeNone   =0,

    UIRectEdgeTop    =1 << 0,

    UIRectEdgeLeft   =1 << 1,

    UIRectEdgeBottom =1 << 2,

    UIRectEdgeRight  =1 << 3,

    UIRectEdgeAll    =UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight

}NS_ENUM_AVAILABLE_IOS(7_0);

    [screenPanGesture release];

处理屏幕边缘手势

- (void)handleScreen:(UIScreenEdgePanGestureRecognizer *)screen{

    CGPoint point = [screen translationInView:screen.view];

    screen.view.transform =CGAffineTransformTranslate(screen.view.transform, point.x,0);

    [screensetTranslation:CGPointZero inView:screen.view];

    NSLog(@"梦戳");

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值