码迷,mamicode.com
首页 > 其他好文 > 详细

手势识别的 创建 与 方法

时间:2016-06-06 16:24:28      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

  /// 手势识别器

    // 1. 轻拍手势

    // 手势需要在定义是绑定一个触发方法(SEL)

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

//    

//    // 轻拍的设置

//    // 需要轻拍两次 才响应事件

//    tap.numberOfTapsRequired = 2;

//    // 需要几根手指 才响应事件

//    tap.numberOfTouchesRequired = 2;

//    // 给view添加一个手势

//    [imageView addGestureRecognizer:tap];

//    [tap release];

    

    

    

    // 2. 长按手势(longPress)

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

    // 长按 触发方法 需要时间

    longPress.minimumPressDuration = 2;

    // 长按时 允许用户移动手指的距离

    longPress.allowableMovement = 100;

    

    [imageView addGestureRecognizer:longPress];

    [longPress release];

    

    

    

    

    // 3. 清扫手势(swipe)

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

//    // 设置清扫方向

//    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

//    

//    [imageView addGestureRecognizer:swipe];

//    [swipe release];

    

    

    

    

//    // 4.拖拽手势(pan)

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

//    

//    [imageView addGestureRecognizer:pan];

//    [pan release];

//    

//    

//    

//    

//    // 5.旋转(rotation)

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

//    

//    [imageView addGestureRecognizer:rotation];

//    [rotation release];

//    

//    

//    

//    

//    // 6. 捏合手势(pinch)

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

//    

//    [imageView addGestureRecognizer:pinch];

//    [pinch release];

    

    

    

    // 7. 屏幕边缘的拖拽

    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenPan:)];

    // 设置监测那一边的屏幕边缘

    screenEdgePan.edges = UIRectEdgeLeft;

    [imageView addGestureRecognizer:screenEdgePan];

    [screenEdgePan release];

    

    // 将UIImageView的用户交互打开,使它能响应轻拍

    [imageView setUserInteractionEnabled:YES];

}

 

- (void)screenPan:(UIScreenEdgePanGestureRecognizer *)screenPan

 

{

    NSLog(@"边缘拖拽");

}

 

// 捏合的触发方法

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    NSLog(@"捏合");

    // 获取当前的view

    UIImageView *imageView = (UIImageView *)pinch.view;

    // 在x,y轴方向 放大/缩小

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

    pinch.scale = 1;  // 放大缩小的尺度(速度)

}

 

// 旋转方法

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

    NSLog(@"旋转");

    // 获取到当前手势添加到的view

    UIImageView *imageView = (UIImageView *)rotation.view;

    // 让view旋转 利用旋转手势的旋转弧度

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

    

    rotation.rotation = 0;

}

 

// 拖拽的触发方法

- (void)panAction:(UIPanGestureRecognizer *)pan

{

    NSLog(@"拖拽");

    // 通过手势的view属性 获取到当前手势添加到的 view

    UIImageView *imageview = (UIImageView *)pan.view;

    

    // 获取到当前手指接触的点

    CGPoint p = [pan translationInView:imageview];

    

    // 让view变形

    imageview.transform = CGAffineTransformTranslate(imageview.transform,p.x, p.y);

    

    // 重置手势的属性

    [pan setTranslation:CGPointZero inView:imageview];

}

 

// 清扫的触发方法

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe

{

    NSLog(@"清扫");

}

// 轻拍的触发方法

- (void)tapAction:(UITapGestureRecognizer *)tap

{

    NSLog(@"轻拍");

    

}

 

// 长按的触发方法

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

 

{

    if (longPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"长按");

    }

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

手势识别的 创建 与 方法

标签:

原文地址:http://www.cnblogs.com/-Eric-Liu/p/5563940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!