UIGestureRecognizer手势

UIView  gusterVier=[[UIView alloc]initWithFrame:CGRectMake(5010270400)];

    gusterVier.backgroundColor=[UIColor orangeColor];

    

    [self.view addSubviewgusterVier];

    

    //允许多指操作

    self.view.multipleTouchEnabled=YES;

    

    //[self createTapGuster];

    

    //[self creatLongGesture];

    

   // [self creatPanGesture];

    

    //[self createPinchGesture];

    

    //[self creatRotationGesture];

    

    [self creatSwipeGusture];

 

#pragma mark -----创建手势-----

//1 点击手势

-(void)createTapGuster

{

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

    

    //允许几根手指同时点击方法

    tapGuster.numberOfTouchesRequired=2;

    

    //手指连续点击的触发方法

    tapGuster.numberOfTapsRequired=2;

    

    //添加手势方法

    [gusterVier addGestureRecognizer:tapGuster];

   // [ges]

    //

 

}

//2 长安手势

-(void)creatLongGesture

{

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

    

    [gusterVier addGestureRecognizer:longPress];

    

}

 

//3 拖动手势

-(void)creatPanGesture

{

    UIPanGestureRecognizer * panGusture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

    

    //最少几根手指拖动

    panGusture.minimumNumberOfTouches=1;

    

     //最多几根手指拖动

    panGusture.maximumNumberOfTouches=2;

 

    

    [gusterVier addGestureRecognizer:panGusture];

 

}

 

 

//4 捏合手势

-(void)createPinchGesture

{

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

    

    [gusterVier addGestureRecognizer:pinch];

 

}

 

//5 旋转手势

-(void)creatRotationGesture

{

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

    

    [gusterVier addGestureRecognizer:rotation];

 

}

 

 

//侧滑

-(void)creatSwipeGusture

{

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

    

    //默认向右滑

    /**

     UISwipeGestureRecognizerDirectionRight 右滑,

     UISwipeGestureRecognizerDirectionLeft  左滑,

     UISwipeGestureRecognizerDirectionUp    上滑,

     UISwipeGestureRecognizerDirectionDown  下滑

     */

    

    swip.direction=UISwipeGestureRecognizerDirectionDown;

    

    [gusterVier addGestureRecognizer:swip];

    

    //将四个方向 都添加

    

    for (int i=0; i<4; i++)

    {

        

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

        

        swipeGestuer.direction = 1 << i;

        

        [gusterVier addGestureRecognizer:swipeGestuer];

        

    }

 

}

#pragma mark -----手势触发的方法-----

-(void)tap:(UITapGestureRecognizer*)tap

{

    self.view.backgroundColor=[UIColor greenColor];

}

//长按点击方法

-(void)longPress:(UILongPressGestureRecognizer*)longPress

{

    //动画类的类型

    CABasicAnimation* animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    //旋转幅度

    animation.fromValue=[NSNumber numberWithFloat:-0.2 ];

    

    animation.toValue=[NSNumber numberWithFloat:0.2];

    

    //重复次数

    animation.repeatCount=CGFLOAT_MAX;

    

    //是否允许左右摆动

    animation.autoreverses=YES;

    

    //添加动画

    [gusterVier.layer addAnimation:animation forKey:@"image"];

    

    

    

 

}

-(void)pan:(UIPanGestureRecognizer*)pan

{

    //拖动速度

    CGPoint speed=[pan velocityInView:gusterVier];

    

    NSLog(@"speed = %@",NSStringFromCGPoint(speed));

    

    //拖动的位移

    

    CGPoint translation=[pan translationInView:gusterVier ];

    

    NSLog(@"traslation=%@",NSStringFromCGPoint(translation));

    

    //还可以得到在self.view 的点 通过手势

    

    CGPoint center=[pan locationInView:gusterVier];

    

    gusterVier.center=center;

    

    

 

}

-(void)pinch:(UIPinchGestureRecognizer*)pinch

{

    //得到捏合的比例

    CGFloat scale=pinch.scale;

    /**

     *  按照此比例形变

     */

    gusterVier.transform=CGAffineTransformMakeScale(scale, scale);

    

    

 

}

-(void)rotation:(UIRotationGestureRecognizer*)rotation

{

    //得到旋转的弧度

    CGFloat angle=rotation.rotation;

    

    gusterVier.transform=CGAffineTransformMakeRotation(angle);

 

}

//滑动手势

-(void)swip:(UISwipeGestureRecognizer*)swip

{

    NSLog(@"侧滑");

    if (swip.direction==UISwipeGestureRecognizerDirectionUp)

    {

        NSLog(@"向上滑");

    }else if(swip.direction==UISwipeGestureRecognizerDirectionDown)

    {

        NSLog(@"向下滑");

    }else if(swip.direction==UISwipeGestureRecognizerDirectionLeft)

    {

        NSLog(@"向左滑");

    }else if(swip.direction==UISwipeGestureRecognizerDirectionRight)

    {

        NSLog(@"向右滑");

    }

 

 

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    [gusterVier.layer removeAnimationForKey:@"image"];

    

    //停止所有动画 暴力型的

    //[gusterVier.layer removeAllAnimations];

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值