UIGestureRecognizer的混合使用

最近在处理一些iOS相关的手势,早期的用法是使用UIView自带的一些touch事件:

 

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

 

然后计算点击了哪里,手指移动的方向等等,最后计算出图片平移的位置,旋转的角度,放缩的程度。

 

好吧,这种方法简直是太弱了。

 

最方便的处理手势的方法是使用UIGestureRecognizer相关类:

 

UITapGestureRecognizer

UIPinchGestureRecognizer

UIRotationGestureRecognizer

UISwipeGestureRecognizer

UIPanGestureRecognizer

UILongPressGestureRecognizer

 

分别处理点击,放缩,旋转,滑动,拖动和长按。

 

这些类都很容易使用,但今天我遇到了一个问题,就是能不能让一个界面同时响应放缩,旋转,滑动这三个不同的动作呢。

 

答案是肯定的,具体的做法就是将self设置为上面类对象的delegate,然后实现代理方法:

 

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

{        

    returnYES;

 

这样就可以同时响应了。

 

但还有一个问题,就是界面的跟着变化呢?

 

下面是具体实现:

- (void)twoFingerPan:(UIGestureRecognizer *)gestureRecognizer

{

switch (gestureRecognizer.state) 

            {

                caseUIGestureRecognizerStateChanged:

                {

                    _point = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.view];

                    _scrollView.transform = CGAffineTransformMake(cosf(_rotation) * _scale, sinf(_rotation) * _scale, -sinf(_rotation) * _scale, cosf(_rotation) * _scale, _point.x, _point.y);

                }

                    break;

default:

                    break;

            }

}

- (void)twoFingerRotation:(UIGestureRecognizer *)gestureRecognizer

{

            switch (gestureRecognizer.state)

            {

                caseUIGestureRecognizerStateChanged:

                {

                    _rotation = [(UIRotationGestureRecognizer *)gestureRecognizer rotation];

                    _scrollView.transform = CGAffineTransformMake(cosf(_rotation) * _scale, sinf(_rotation) * _scale, -sinf(_rotation) * _scale, cosf(_rotation) * _scale, _point.x, _point.y);

                }

                    break;

default:

                    break;

            }

}

- (void)twoFingerPinch:(UIGestureRecognizer *)gestureRecognizer

{

            switch (gestureRecognizer.state)

            {

              caseUIGestureRecognizerStateChanged:

                {

                    _scale = [(UIPinchGestureRecognizer *)gestureRecognizer scale];

                    _scrollView.transform = CGAffineTransformMake(cosf(_rotation) * _scale, sinf(_rotation) * _scale, -sinf(_rotation) * _scale, cosf(_rotation) * _scale, _point.x, _point.y);

                }

                    break;

              default:

                    break;

            }

 }
就是在不同的UIGestureRecognizer绑定方法中,统一实现对View的transform,这样就可以了,希望对大家有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值