当你看见这个题目的时候,就应该知道平移缩放旋转手势和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可选的。
那我能在某个视图上添加上手势了,然后也知道手势触发的方法了,之后要做的,就是从手势中提取相关的信息,然后对视图进行变换了,这个将在下篇文章中详述。