美文网首页
七大手势

七大手势

作者: nothing_c | 来源:发表于2016-10-30 01:14 被阅读29次

//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 {

}

相关文章

  • iOS七大手势

    iOS七大手势 轻拍手势 UITapGestureRecognizer 轻扫手势 UISwipeGestureRe...

  • 七大手势

    //UIGestureRecognizer手势是一个抽象类(子类有用) //手势:点击,平移,捏合,轻扫,旋转,长...

  • IOS-手势事件简述

    在iOS中事件分为三类: 触摸事件:通过触摸。手势进行触发事件(七大手势)运动时间:通过加速器进行触发的事件(摇一...

  • 手势

    点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势

  • iOS-手势详细参数说明

    敲击手势 长按手势 滑动手势 拖动手势 旋转手势 捏合手势 两种手势作用在同一个视图

  • 【iOS学习】——手势识别

    iOS 手势 手势需要开启用户交互 点击手势 单击手势 双击手势 添加 numberOfTapsRequired...

  • Swift - UIGestureRecognizer 各种手势

    1、点击手势2、拖动手势3、长按手势4、滑动手势5、捏合手势6、旋转手势 完整代码

  • iOS七种手势详解

    1、轻拍手势 2、捏合手势 3、旋转手势 4、平移手势 5、边缘轻扫手势 6、长按手势 7、轻扫手势 给image...

  • iOS手势总结

    1.轻拍手势 2.长按手势 3.轻扫手势 4.平移手势 5.捏合手势 6.旋转手势 7.边缘手势

  • iOS 手势

    修改时间: 2016-12-19修改次数: 0 手势传递 点击手势 捏合手势 轻扫手势 拖动手势 长按手势

网友评论

      本文标题:七大手势

      本文链接:https://www.haomeiwen.com/subject/tmnyuttx.html