//UIGestureRecognizer是一个手势识别器的抽象基类,只提供手势识别器的基本功能,使用其具体的子类功能(7大子类)
//轻拍手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
//设置轻拍的次数
tapGesture.numberOfTapsRequired = 1;
//设置轻拍时需要的手指个数
tapGesture.numberOfTouchesRequired = 2;
//给视图添加轻拍手势
[redView addGestureRecognizer:tapGesture];
[tapGesture release];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handLongPressGesture:)];
//设置长按手势的最短时间(可不设,系统默认0.5秒,)
// longPressGesture.minimumPressDuration = 2;
[redView addGestureRecognizer:longPressGesture];
[longPressGesture release];
- (void)handLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture{
//当识别到长按手势时触发(长按时间到达后触发)
if (UIGestureRecognizerStateBegan == longPressGesture.state) {
longPressGesture.view.backgroundColor = [UIColor randomColor];
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
//轻扫手势
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handSwipe:)];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft ;
//设置清扫手势轻扫的方向,让左右方向都存在
//只要枚举值中有左移运算符,代表枚举值可以通过按位或(不是逻辑或),让多个枚举值同时存在
//warning:在一个手势识别器中,上下和左右不能同时存在.(如果想达到效果,可以另外设置一个手势识别器,再设置一个方法即可,,一个支持上下,一个支持左右)
[redView addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//平移手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handPanGesture:)];
[redView addGestureRecognizer:panGesture];
[panGesture release];
//处理平移手势的方法
- (void)handPanGesture:(UIPanGestureRecognizer *)panGesture{
//获取平移前后的改变量
CGPoint point = [panGesture translationInView:panGesture.view];
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//将之前的增量清0;
[panGesture setTranslation:CGPointZero inView:panGesture.view];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[redView addGestureRecognizer:pinchGesture];
[pinchGesture release];
//处理捏合手势的方法
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
//将之前变化的比例置1
pinchGesture.scale = 1.0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
[redView addGestureRecognizer:rotationGesture];
[rotationGesture release];
//处理旋转手势的方法
- (void)handleRotation:(UIRotationGestureRecognizer *)rotationGesture{
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
//将之前的该变量清零
rotationGesture.rotation = 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//屏幕边缘手势
UIScreenEdgePanGestureRecognizer *edgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleEdge:)];
//设置屏幕边缘
edgePanGesture.edges = UIRectEdgeLeft;
[redView addGestureRecognizer:edgePanGesture];
[edgePanGesture release];
//处理屏幕边缘手势的方法
- (void)handleEdge:(UIScreenEdgePanGestureRecognizer *)edgePanGesture{
CGPoint point = [edgePanGesture translationInView:edgePanGesture.view];
edgePanGesture.view.transform = CGAffineTransformTranslate(edgePanGesture.view.transform, point.x, 0);
//将移动量随时清零
[edgePanGesture setTranslation:CGPointZero inView:edgePanGesture.view];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~在手势处理后,一般涉及到视图的还原,以下提供常用的一个还原方法
//移除之前的手势方法
- (void)removeViewGesture{
NSArray *gestureArray = _view1.gestureRecognizers;//_view1是当前手势所在视图
for (UIGestureRecognizer *gesture in gestureArray) {
[_view1 removeGestureRecognizer:gesture];
}
}
//恢复原状方法
- (void)resetGestureView
{
//对于旋转之后与平移和捏合还原的方法是不一样的.
//如果_angle不为0,说明之前是旋转了.则还原时采用第二套方案,如果为0,说明之前没有旋转,则采用第一套方案.
if (!_angle) {//!_angle没有移动 _angle旋转的角度
//第一套方案
_view1.frame = _imageOriginFrame;
return;
}
//第二套方案
_view1.transform = CGAffineTransformRotate(_view1.transform, -_angle);
_angle = 0;
}