ios开发,6种手势的创建方法

来源:互联网 发布:国产 cpu.知乎 编辑:程序博客网 时间:2024/05/17 09:20

 在 ios 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
UITapGestureRecognizer  // Tap(点一下)
UIPinchGestureRecognizer  // Pinch(二指往內或往外拨动)
UIRotationGestureRecognizer  // Rotation(旋转)
UISwipeGestureRecognizer  // Swipe(滑动,快速移动)
UIPanGestureRecognizer  // Pan (拖移,慢速移动)
UILongPressGestureRecognizer  // LongPress(长按)

通过内置的手势识别器来处理一个手势的基本流程如下所示:

·        1.创建一个具有正确类型的对象,作为想要实现的手势识别器;

·        2.把该对象作为手势识别器添加到需要接收手势的视图中;

·        3.书写一个方法,该方法当手势事件发生时被调用,由其来执行应用所需的动作。

初始化

- (instancetype)initWithTarget:(id)targetaction:(SEL)action

 

添加一个点击手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:tag];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:image];  


[image release];


[pinch release];



添加一个捏合手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:pinch];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:pinch];  


[image release];


[pinch release];


触发事件:

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    // 通过tag值传值

    UIView *view = [self.view viewWithTag:100];

    // 设定放大倍数,让它长宽等比例放大

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

    // 每次放大后还原放大倍数,

    pinch.scale = 1;

}


添加一个旋转手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:rotation];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:rotation];  


[image release];


[rotation release];


触发事件:

- (void)rotationAction:(UIRotationGestureRecognizer *)rotate

{

    UIView *view = [self.view viewWithTag:100];

    // 设定旋转角度

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

    // 每次旋转完之后旋转角度清零

    rotate.rotation = 0;

    

}



添加一个滑动手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:swipe];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:swipe];  


[image release];


[swipe release];


添加一个拖拽手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:pan];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:pan];  


[image release];


[pan release];


触发事件:

- (void)panAction:(UIPanGestureRecognizer *)pan

{

    UIView *view = [self.view viewWithTag:100];

    // 获取当前视图的偏移量

    CGPoint point = [pan translationInView:view];

    // 设置偏移量

    view.transform=CGAffineTransformTranslate(view.transform, point.x, point.y);

    // 偏移量清零

    [pan setTranslation:CGPointMake(0, 0)inView:view];

}


添加一个长按手势

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];


image.frame = CGRectMake(5050100160);  


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


[image addGestureRecognizer:longPress];  


[self.view setBackgroundColor:[UIColor whiteColor]];  


[self.view addSubview:longPress];  


[image release];


[longPress release];


0 0