UI控件 - 手势识别

为了完成手势识别,必须借助于手势识别器—  UIGestureRecognizer

1.  Tap点按手势
1. 创建一个点按事件
UITapGestureRecognizer  *tap = [[ UITapGestureRecognizer   alloc initWithTarget : self   action : @selector (tap:)];

2. 设置点击的次数
tap. numberOfTapsRequired
点击的手指的个数
tap.
numberOfTouchesRequired

3. 向要点击的view中添加手势
[ self . view   addGestureRecognizer :tap]

4. 代理方法  - ( BOOL )gestureRecognizer:( UIGestureRecognizer  *)gestureRecognizer shouldReceiveTouch:( UITouch  *)touch;


2. LongPress 长按手势
- ( void )addLongPress
{
    
UILongPressGestureRecognizer  *longPress = [[ UILongPressGestureRecognizer   alloc initWithTarget : self   action : @selector (longPress:)];
    longPress.
minimumPressDuration  =  0.01 ;
    [
_imageView   addGestureRecognizer :longPress];
}

- (
void )longPress:( UILongPressGestureRecognizer  *)longPress
{
    // 长按包含多个状态,通过state标记
    
if  (longPress. state  ==  UIGestureRecognizerStateBegan  ) {
        
NSLog ( @"%s" ,__func__);
    }
}

3. Swipe 清扫手势
- ( void )addSwipe
{
    
UISwipeGestureRecognizer  *swipe = [[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
    swipe.
direction  =  UISwipeGestureRecognizerDirectionLeft ;
    [
_imageView   addGestureRecognizer :swipe];
    
    
UISwipeGestureRecognizer  *swipeR = [[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
    
//    swipeR.direction = UISwipeGestureRecognizerDirectionLeft;
    [
_imageView   addGestureRecognizer :swipeR];
}

细节: 默认一个手势只支持一个方向
// 识别滑动方向
@property ( nonatomic UISwipeGestureRecognizerDirection  direction;  


4.  Rotation 旋转手势!!!!!!
- ( void )addRotation
{
    
UIRotationGestureRecognizer  *rotation = [[ UIRotationGestureRecognizer   alloc initWithTarget : self   action : @selector (rotation:)];
    rotation.
delegate  =  self ;
    [
_imageView   addGestureRecognizer :rotation];
}

- (
void )rotation:( UIRotationGestureRecognizer  *)rotation
{
    
_imageView . transform  =  CGAffineTransformRotate ( _imageView . transform , rotation. rotation );
    
    
//  复位
    rotation.
rotation  =  0 ;
}

5.  Pinch 捏合手势
- ( void )addPinch
{
    
UIPinchGestureRecognizer  *pinch = [[ UIPinchGestureRecognizer   alloc initWithTarget : self   action : @selector (pinch:)];
    pinch.
delegate  =  self ;
    [
_imageView   addGestureRecognizer :pinch];
}

- (
void )pinch:( UIPinchGestureRecognizer  *)pinch
{
    
_imageView . transform  =  CGAffineTransformScale ( _imageView . transform , pinch. scale , pinch. scale );
    
    
//  复位
    pinch.
scale  =  1 ;
}



6. 缩放并旋转

// 添加捏合和旋转
[ self   addPinch ];
[
self   addRotation ];

// Simultaneously: 同时
//  调用这个方法判断是否支持很多手指
- (
BOOL )gestureRecognizer:( UIGestureRecognizer  *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:( UIGestureRecognizer  *)otherGestureRecognizer
{
    
return   YES ;
}


7. 拖拽
- ( void )addPan
{
    
UIPanGestureRecognizer  *pan = [[ UIPanGestureRecognizer   alloc initWithTarget : self   action : @selector (pan:)];
    
    [
_imageView   addGestureRecognizer :pan];
}
- (
void )pan:( UIPanGestureRecognizer  *)pan
{
    
CGPoint  transP = [pan  translationInView : _imageView ];

    
_imageView . transform  =  CGAffineTransformTranslate ( _imageView . transform , transP. x , transP. y );

    [pan 
setTranslation : CGPointZero   inView : _imageView ];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值