手势识别

1、点击手势

- (void)testTap

{

    // 1.创建手势识别器对象

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

    // 连续敲击2次,手势才能识别成功

    tap.numberOfTapsRequired = 2;

    tap.numberOfTouchesRequired = 2;

    

    // 2.添加手势识别器对象到对应的view

    [self.iconView addGestureRecognizer:tap];

    

    // 3.添加监听方法(识别到了对应的收拾,就会调用监听方法)

    [tap addTarget:self action:@selector(tapView)];

}

/**

 *  当点击view的时候,会先调用这个方法

 */

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    CGPoint pos = [touch locationInView:touch.view];

    if (pos.x <= self.iconView.frame.size.width * 0.5) {

        return YES;

    }

    return NO;

}

/**

 *  当点击view的时候,会先调上面的方法,然后再调下面的方法

 */

- (void)tapView

{

    NSLog(@"-----tapView");

}

2、轻扫手势

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

swipe.direction = UISwipeGestureRecognizerDirectionUp;

[self.redView addGestureRecognizer:swipe];

- (void)swipeView

{

    NSLog(@"swipeView");

}

3、 长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];

[longPress addTarget:self action:@selector(longPressView)];

// 至少长按2秒

longPress.minimumPressDuration = 2;

 // 在触发手势之前,50px范围内长按有效

 longPress.allowableMovement = 50;    

[self.redView addGestureRecognizer:longPress];

 

- (void)longPressView

{

    NSLog(@"长按了红色的view");

}

4、 缩放手势(捏合手势)

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

pinch.delegate = self;

[self.iconView addGestureRecognizer:pinch];

- (void)pinchView:(UIPinchGestureRecognizer *)pinch

{

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

    pinch.scale = 1; // 还原缩放比例,

}

5、旋转手势

UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizeralloc] initWithTarget:self action:@selector(rotateView:)];

    recognizer.delegate = self;

    [self.iconView addGestureRecognizer:recognizer];

 

- (void)rotateView:(UIRotationGestureRecognizer *)recognizer

{

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

    recognizer.rotation = 0; // 这个很重要!!!!!

}

6、拖拽平移手势

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

[self.purpleView addGestureRecognizer:pan];

- (void)panView:(UIPanGestureRecognizer *)pan

{

    switch (pan.state) {

        case UIGestureRecognizerStateBegan: // 开始触发手势

            break;

        case UIGestureRecognizerStateEnded: // 手势结束

            break;

        default:

            break;

    }

    // 1.在view上面挪动的距离

    CGPoint translation = [pan translationInView:pan.view];

    CGPoint center = pan.view.center;

    center.x += translation.x;

    center.y += translation.y;

    pan.view.center = center;

    // 2.清空移动的距离

    [pan setTranslation:CGPointZero inView:pan.view];

}

7、同时允许多个手势

1->实现以下方法,让控制器可以同时识别多个手势

/**

 *  是否允许多个手势识别器同时有效

 *  Simultaneously : 同时地

 */

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

2->遵守UIGestureRecognizerDelegate协议

3->哪几手势需要同时识别,则这每个手势都要设置代理

 
posted @ 2014-08-26 23:00  树籽  阅读(201)  评论(0编辑  收藏  举报