UI手势

   UI手势就是给UI增加手势,大致可分为6种:

    UITapGestureRecognizer   //Tap(点击)

    UIPinchGestureRecognizer    //pin(捏合)

    UIRotationGestureRecognizer   //rotation(旋转)

    UISwipeGestureRecognizer  //swipe(滑动,快速的移动,用于监测滑动的方向)

    UIPanGestureRecognizer   //pan(拖动,慢速移动,用于监测偏移的量)

    UILongPressGestureRecognizer   //longPress(长按)

    //可以把它看作是一块操作的区域

    UIView *tapView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 300, 500)];

    tapView.backgroundColor = [UIColor redColor];

    [self.view addSubview:tapView];

    //单击,必须写冒号

    UITapGestureRecognizer *singTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];

    //点击的次数

    singTap.numberOfTapsRequired = 1;//单击

    singTap.numberOfTouchesRequired = 1;//手指个数

    [tapView addGestureRecognizer:singTap];

    //双击

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

    doubleTap.numberOfTapsRequired = 2;

    [tapView addGestureRecognizer:doubleTap];

      //手势冲突

    //这句代码非常关键,双击手势确定监测失败才会触发单击手势的操作(意思就是说:单击手势和双击手势同时存在优先执行双击手势)

    [singTap requireGestureRecognizerToFail:doubleTap];

    //捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

    //一般用比例,不用速度

    [tapView addGestureRecognizer:pinch];

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

    

    NSLog(@"捏合:%f",pinch.scale);

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

//由于缩放的时候会在上次的基础上缩放,所以要设置在原来的基础上缩放

    pinch.scale = 1;

}

//旋转

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

    [tapView addGestureRecognizer:rotation];

- (void)rotation:(UIRotationGestureRecognizer*)rotation {

    NSLog(@"旋转:%f",rotation.rotation);

    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);

    //如果不设置为0,就会转得非常快

    rotation.rotation = 0;

}

 //长按

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

    [tapView addGestureRecognizer:longPress];


    //滑动

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

    swipe.direction = UISwipeGestureRecognizerDirectionRight;

    [tapView addGestureRecognizer:swipe];

- (void)swipe:(UISwipeGestureRecognizer*)swipe {

    //移动

    //获取当前viewlocation

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

    swipe.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值