//UIGestureRecognizer手势是一个抽象类(子类有用)
//手势:点击,平移,捏合,轻扫,旋转,长按屏幕,边缘滑动
/*
1.创建手势对象
2.绑定触发手势的范围
3.实现触发手势是调用的方法
4.根据手势的不同的状态做出不同的操作
*/
/*
//1.点击
//创建点击手势类的对象并绑定触发方法
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置需要点击几次
tapGesture.numberOfTapsRequired = 2;
//设置需要几根手指点击
tapGesture.numberOfTouchesRequired = 2;
//给视图添加手势
[myView addGestureRecognizer:tapGesture];
*/
//点击方法
- (void)tapGesture:(UITapGestureRecognizer *)gesture {
//获取手势绑定的视图
UIView *view = gesture.view;
view.backgroundColor= [UIColorgreenColor];
}
/*
2.平移手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[myView addGestureRecognizer:panGesture];
*/
//平移方法
- (void)panGesture:(UIPanGestureRecognizer*)gesture {
//判断手势状态
switch(gesture.state) {
//开始
case UIGestureRecognizerStateBegan:
break;
//改变
case UIGestureRecognizerStateChanged:
{
UIView *view = gesture.view;
//偏移量
CGPoint offset = [gesture translationInView:view];
//移动位置
view.frame=CGRectOffset(view.frame, offset.x, offset.y);
//坐标位置
[gestureset Translation:CGPointZeroinView:view];
}
break;
//结束
case UIGestureRecognizerStateEnded:
break;
default:
break;
}
}
/*
//3.捏合(缩放) 一般不使用捏合手势,一般使用scrollView自带的缩放功能
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[myView addGestureRecognizer:pinchGesture];
*/
//捏合方法
- (void)pinchGesture:(UIPinchGestureRecognizer *)gesture {
CGRect _rect;
switch(gesture.state) {
case UIGestureRecognizerStateBegan:
{
_rect = gesture.view.frame;
}
break;
case UIGestureRecognizerStateChanged:
{
//第一种方式
// CGFloat scale = gesture.scale;
// CGFloat dx = (1 - scale) * CGRectGetWidth(gesture.view.frame) / 2;
// CGFloat dy = (1 - scale) * CGRectGetHeight(gesture.view.frame) / 2;
// //以初始的frame进行改变
// gesture.view.frame = CGRectInset(_rect, dx, dy);
//第二种方式
gesture.view.transform=CGAffineTransformScale(gesture.view.transform, gesture.scale, gesture.scale);
//每次都是比例为1
gesture.scale= 1;
}
break;
case UIGestureRecognizerStateEnded:
break;
default:
break;
}
}
/*
//4.轻扫
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//轻扫方向(上下左右)
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
//如果需要四个方向的轻扫就创建四个手势
[myView addGestureRecognizer:swipeGesture];
*/
//轻扫方法
- (void)swipeGesture:(UISwipeGestureRecognizer *)gesture {
switch(gesture.direction) {
caseUISwipeGestureRecognizerDirectionUp:
{
//获取手势绑定的视图
UIView*view = gesture.view;
view.backgroundColor= [UIColor greenColor];
}
break;
default:
break;
}
}
/*
//5.旋转
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[myView addGestureRecognizer:rotationGesture];
*/
//旋转方法
- (void)rotationGesture:(UIRotationGestureRecognizer *)gesture {
switch(gesture.state) {
case UIGestureRecognizerStateBegan:
break;
case UIGestureRecognizerStateChanged:
{
gesture.view.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
break;
case UIGestureRecognizerStateEnded:
{
//回到初始状态默认位置
gesture.view.transform=CGAffineTransformIdentity;
}
break;
default:
break;
}
}
/*
//6.长按
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGresture:)];
//设置长按反应时间
longPressGesture.minimumPressDuration = 1;
[myView addGestureRecognizer:longPressGesture];
*/
//长按方法
- (void)longPressGresture:(UILongPressGestureRecognizer *)gesture {
switch(gesture.state) {
case UIGestureRecognizerStateEnded:
NSLog(@"长按添加");
break;
default:
break;
}
//UIMenuController菜单控制器
//sharedMenuController单例类
UIMenuController *menuVC = [UIMenuController sharedMenuController];
//菜单项
UIMenuItem *itme1 = [[UIMenuItemalloc] initWithTitle:@"粘贴" action:@selector(item1Click)];
UIMenuItem *itme2 = [[UIMenuItemalloc] initWithTitle:@"复制" action:@selector(item2Click)];
menuVC.menuItems= @[itme1,itme2];
//出现位置
[menuVC setTargetRect:CGRectMake(gesture.view.center.x, gesture.view.center.y, 0, 0) inView:self.view];
//让menuVC显示出来
[menuVC setMenuVisible:YES animated:YES];
}
//如果想要UIMenuController显示出来必须重写下面的方法
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)item1Click{
NSLog(@"粘贴");
}
- (void)item2Click{
NSLog(@"复制");
}
//7.屏幕边缘滑动
UIScreenEdgePanGestureRecognizer *SEPGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGesture:)];
//必须制定一个滑动边缘
SEPGesture.edges=UIRectEdgeLeft;
[self.view addGestureRecognizer:SEPGesture];
//屏幕边缘滑动方法
- (void)screenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
switch(gesture.state) {
case UIGestureRecognizerStateEnded:
NSLog(@"滑动");
break;
default:
break;
}
}
#warning注意事项
//UIImageView添加手指之前需要设置userInteractionEnabled为YES
//TODO:这是错误
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tap1:)];
[myView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tap2:)];
[myView addGestureRecognizer:tap2];
//当tap2执行失败时,才使用tap1,避免崩溃
[tap1 requireGestureRecognizerToFail:tap2];
}
- (void)tap1:(UITapGestureRecognizer*)tap1 {
}
- (void)tap2:(UITapGestureRecognizer*)tap2 {
}