各种UIGestureRecognizer 手势

注:本文只为记录,没仔细讲解


#import "TouchViewController.h"


@interface TouchViewController ()


@end


@implementation TouchViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColorwhiteColor];

    

    UILabel *label = [[UILabelalloc]init];

    label.center = self.view.center;

    label.bounds = CGRectMake(0, 0, 150, 150);

    label.backgroundColor = [UIColororangeColor];

    label.alpha = 0.7;

    label.userInteractionEnabled =YES;

    [self.viewaddSubview:label];

    //点击手势

//    [self TapGestureRecognizer:label];

    //长按手势

//    [self LongPressGestureRecognizer:label];

    //轻扫手势

//    [self SwipeGestureRecognizer:label];

    //缩放手势

//    [self PinchGestureRecognizer:label];

    //拖动手势

//    [self PanGestureRecognizer:label];

    //旋转手势

    [selfRotationGestureRecognizer:label];

}


/**

 *  点击手势

 *  @param label 响应手势视图

 */

- (void)TapGestureRecognizer:(UILabel *)label{

    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSingleFingerEvent:)];

    /**

     *  这两个可以随意匹配,可形成 单指双击||双指单击||双指双击等...

     */

    singleFingerOne.numberOfTouchesRequired = 2; //手指数

    singleFingerOne.numberOfTapsRequired = 2; //tap次数

    [label addGestureRecognizer:singleFingerOne];//给视图添加点击手势

}


/**

 *  点击手势执行的方法

 *  @param gestureRecognizer 传递点击手势

 */

- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)gestureRecognizer{

    NSLog(@"111 --- %f",gestureRecognizer.view.alpha);

}


/**

 *  长按手势

 *  @param label 响应手势视图

 */

- (void)LongPressGestureRecognizer:(UILabel *)label{

    //添加一个长按手势

    UILongPressGestureRecognizer *LPGR = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(LongPressGestureRecognizerFunction:)];

    LPGR.minimumPressDuration =1.0;//长按时间

    [label addGestureRecognizer:LPGR];

}


/**

 *  长按手势执行方法

 *  @param gestureRecognizer 传递长按手势

 */

- (void)LongPressGestureRecognizerFunction:(UILongPressGestureRecognizer *)gestureRecognizer{

    NSLog(@"111 --- %f",gestureRecognizer.view.alpha);

    /**

     *  如若不区分,则会执行两次

     */

    if (gestureRecognizer.state==UIGestureRecognizerStateBegan) {

        //开始状态

    }

    if(gestureRecognizer.state ==UIGestureRecognizerStateEnded){

        //结束状态

    }

}


/**

 *  轻扫手势

 *  @param label 响应手势视图

 */

- (void)SwipeGestureRecognizer:(UILabel *)label{

    UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(SwipeGestureRecognizerFunction:)];

    Swipe.numberOfTouchesRequired =2;//手指数

    [label addGestureRecognizer:Swipe];

}


/**

 *  轻扫手势响应方法

 *  @param GestureRecognizer 传递轻扫手势

 */

-(void)SwipeGestureRecognizerFunction:(UISwipeGestureRecognizer *)GestureRecognizer{

    NSLog(@"111 --- %f",GestureRecognizer.view.alpha);

    //指定轻扫手势响应的方向

    if (GestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft||UISwipeGestureRecognizerDirectionRight) {

        

    }

}


/**

 *  缩放手势

 *  @param label 响应手势视图

 */

- (void)PinchGestureRecognizer:(UILabel *)label{

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(PinchGestureRecognizerFunction:)];

    [label addGestureRecognizer:pinch];

    

}


/**

 *  缩放手势响应方法

 *  @param GestureRecognizer 传递缩放手势

 */

- (void)PinchGestureRecognizerFunction:(UIPinchGestureRecognizer *)GestureRecognizer{

    NSLog(@" --- %f --- %f",GestureRecognizer.scale,GestureRecognizer.velocity);

    CGRect  bounds = GestureRecognizer.view.bounds;

    bounds.size.height = bounds.size.height *GestureRecognizer.scale;

    bounds.size.width  = bounds.size.width  *GestureRecognizer.scale;

    GestureRecognizer.view.bounds = bounds;

}


/**

 *  拖动手势

 *  @param label 响应拖动手势的视图

 */

- (void)PanGestureRecognizer:(UILabel *)label{

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

    panGesture.minimumNumberOfTouches =1;//最小需要的手指数

    panGesture.maximumNumberOfTouches =2;//最大需要的手指数

    [label addGestureRecognizer:panGesture];

}


/**

 *  拖动手势响应事件

 *  @param GestureRecognizer 传递拖动手势

 */

- (void)PanGestureRecognizerFunction:(UIPanGestureRecognizer *)GestureRecognizer{

    CGPoint translatedPoint = [GestureRecognizertranslationInView:self.view];

    CGFloat x = GestureRecognizer.view.center.x + translatedPoint.x;

    CGFloat y = GestureRecognizer.view.center.y + translatedPoint.y;

    GestureRecognizer.view.center =CGPointMake(x, y);

    [GestureRecognizer setTranslation:CGPointMake(0,0) inView:self.view];

    /**

     * 

     - (CGPoint)translationInView:(UIView *)view方法的API解释如下:

     

     The translation of the pan gesture in the coordinate system of the specified view.

     

     Return Value

     A point identifying the new location of a view in the coordinate system of its designated superview.

     字面理解是:

     在指定的视图坐标系统中转换(拖动?) pan gesture

     返回参数:返回一个明确的新的坐标位置,在指定的父视图坐标系统中

     简单的理解就是

     该方法返回在横坐标上、纵坐标上拖动了多少像素

     因为拖动起来一直是在递增,所以每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图

     

     - (CGPoint)velocityInView:(UIView *)view方法的API解释如下:

     

     The velocity of the pan gesture in the coordinate system of the specified view.

     

     Return Value

     The velocity of the pan gesture, which is expressed in points per second. The velocity is broken into horizontal and vertical components.

     字面理解:

     在指定坐标系统中pan gesture拖动的速度

     返回参数:返回这种速度

     简单的理解就是

     你拖动这个图片的时候肯定有个速度,因此返回值就是你拖动时XY轴上的速度,速度是矢量,有方向。

     *

     */

}


/**

 *  旋转手势

 *  @param label 响应手势视图

 */

- (void)RotationGestureRecognizer:(UILabel *)label{

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(RotationGestureRecognizerFunction:)];

    rotation.rotation = 0;//旋转的弧度

    [label addGestureRecognizer:rotation];

}


/**

 *  旋转手势响应事件

 *  @param GestureRecognizer 传递响应的手势

 */

- (void)RotationGestureRecognizerFunction:(UIRotationGestureRecognizer *)GestureRecognizer{

    

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

    GestureRecognizer.rotation = 0;

}



附:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

  UIGestureRecognizerStatePossible,  // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态

  UIGestureRecognizerStateBegan,   // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成

  UIGestureRecognizerStateChanged,  // 手势状态发生转变

  UIGestureRecognizerStateEnded,   // 手势识别操作完成(此时已经松开手指)

  UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态

  UIGestureRecognizerStateFailed,   // 手势识别失败,恢复到默认状态

  UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded

};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值