iOS 利用平移缩放旋转手势对view实现对应的平移缩放旋转效果(一)

本文介绍了如何利用iOS手势识别类处理平移、缩放和旋转操作,并将这些手势应用于view。通过初始化手势对象并关联相应的方法,当view触发手势时,可以获取手势信息并据此改变view的属性。详细的操作将在后续文章中展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     当你看见这个题目的时候,就应该知道平移缩放旋转手势和view对应的平移缩放旋转是两码事儿。也就是说如果我们想利用手势,对某个view进行某些操作,就必须明白两个问题,一是如何分辨和接收手势操作,二是如何根据接收到的手势对视图进行平移缩放旋转。

    一、先说说如何分辨和接收手势操作

    手机端的手势无非分为三种,就是题目中所提到的平移缩放旋转,ios有专门的类,来对应这三种手势。把这些手势绑定在那个view上,那一旦这个view触发了这个手势,我们该调哪个函数呢?这个问题问的好,在这些手势累初始化的时候,把其对应的view和触发后调用的的方法传进去,请看下边的示例代码:    

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]//缩放手势
                                                       initWithTarget:self
                                                        action:@selector(handlePinch:)];//绑定方法
    [_scrollView addGestureRecognizer:pinchGestureRecognizer];   //绑定view
   
    //平移手势 
    UIPanGestureRecognizer  *moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
                        
                                                               action:@selector(moveGesture:)];  
    moveGesture.delegate = self;
    
    [_scrollView addGestureRecognizer:moveGesture];
    
   
 
    //单指双击手势
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                       
                                                              action:@selector(singleTapGesture:)];
    
    tapGesture.numberOfTapsRequired = 2;
    
        self.tapGesture.delegate = self;
        [_scrollView addGestureRecognizer:tapGesture];

细心的同学,可能发现了这句话

 moveGesture.delegate = self;
手势的代理,如果我们把代理指向了自己,应该实现什么方法呢?具体先看看这个代理具体是什么样的吧?

@protocol UIGestureRecognizerDelegate <NSObject>
@optional
// called when a gesture recognizer attempts to transition out of UIGestureRecognizerStatePossible. returning NO causes it to transition to UIGestureRecognizerStateFailed
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
//
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

// called once per attempt to recognize, so failure requirements can be determined lazily and may be set up between recognizers across view hierarchies
// return YES to set up a dynamic failure requirement between gestureRecognizer and otherGestureRecognizer
//
// note: returning YES is guaranteed to set up the failure requirement. returning NO does not guarantee that there will not be a failure requirement as the other gesture's counterpart delegate or subclass methods may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

@end

上边是从UIKit中拷取的源码,很明显,手势不同的的状态会调用不同的方法,这个我们可以根据需要自己实现,反正是optional可选的。

那我能在某个视图上添加上手势了,然后也知道手势触发的方法了,之后要做的,就是从手势中提取相关的信息,然后对视图进行变换了,这个将在下篇文章中详述。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一步一台阶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值