UI手势的基本用法

--------------轻拍手势

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

    // 将手势监听器加入view中

    [_aView addGestureRecognizer:tapGesture];

    

    //要求点击的次数是两次

    tapGesture.numberOfTapsRequired = 2;

    //要求必须两个手指点击

    tapGesture.numberOfTouchesRequired = 2;


手势监听:监听轻拍手势然后改变背景

-(void)changeBackgroundColor:(UITapGestureRecognizer *)gesture

{

    UIView *aView = gesture.view;

    aView.backgroundColor =           [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];

}

   

--------------长按手势

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

    [_aView addGestureRecognizer:longPressGesture];

手势监听:监听轻拍手势然后改变背景

-(void)changeBackgroundColor:(UILongPressGestureRecognizer *)gesture

{

  //这个手势比较奇葩,得设置响应的是长按的开始结束还是取消的状态,否则它会响应好几次        

if (gesture.state == UIGestureRecognizerStateBegan) {

        UIView *aView = gesture.view;

        aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];

}

    


--------------轻扫手势

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

    [_aView addGestureRecognizer:swipGesture];

    swipGesture.numberOfTouchesRequired = 1;

    // 改变响应轻扫响应方向,下面的设置是响应由上往下的手势

    swipGesture.direction = UISwipeGestureRecognizerDirectionDown;

手势监听:监听轻拍手势然后改变背景

-(void)changeBackgroundColor:(UISwipeGestureRecognizer *)gesture

{

    UIView *aView = gesture.view;

    aView.backgroundColor =           [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];

}


--------------拖动手势

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];

    [_aView addGestureRecognizer:panGesture];


      手势监听:拖动手指实现view跟着移动

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)pan

{

    CGPoint offsetPoint = [pan translationInView:pan.view];

    pan.view.transform = CGAffineTransformMakeTranslation(offsetPoint.x, offsetPoint.y);

}

    

--------------捏合手势

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

    [_aView addGestureRecognizer:pinch];


手势监听:捏合手指实现view的跟随缩放

-(void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinch

{

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

}

--------------旋转手势

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

    [_aView addGestureRecognizer:rotationGesture];

    [rotationGesture release];


手势监听:旋转手指实现view的跟随实现旋转

-(void)rotationGestureRecognizer:(UIRotationGestureRecognizer*)rotationGesture

{

    rotationGesture.view.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);

}





tips:

IOS模拟器中可以通过安alt(option)键来实现模拟两个手指按的情况,如果是3个手指捏,呵呵,目前木有那个手机支持


转载于:https://my.oschina.net/u/1392520/blog/282600

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值