轻扫手势/平移、长按手势
来源:互联网 发布:定做软件 猪八戒网 编辑:程序博客网 时间:2024/04/29 13:47
(1)创建故事板
(2)在ViewController.h中设置全局_myView
(3)ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 400)]; _myView.backgroundColor = [UIColor greenColor]; [self.view addSubview:_myView]; /*__________________________________手势的使用_____________________________________*/ //_________________________点击手势__________________________ //单击 UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)]; //设置点击的次数,默认是1 tap1.numberOfTapsRequired = 1; //设置点击的手指个数 tap1.numberOfTouchesRequired = 1; //将点击的手势添加到视图上 [_myView addGestureRecognizer:tap1]; //双击 UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)]; //设置点击的次数 tap2.numberOfTapsRequired = 2; //设置手指个数 tap2.numberOfTouchesRequired = 1; //添加到视图上 [_myView addGestureRecognizer:tap2]; //如果我们双击则取消单击相应事件(注意:需要取消的手势放在前面) [tap1 requireGestureRecognizerToFail:tap2]; //_________________________轻扫手势__________________________ UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; //设置轻扫的方向 /* UISwipeGestureRecognizerDirectionRight 右(默认) UISwipeGestureRecognizerDirectionLeft 左 UISwipeGestureRecognizerDirectionUp 上 UISwipeGestureRecognizerDirectionDown 下 */ swipe.direction = UISwipeGestureRecognizerDirectionUp; [_myView addGestureRecognizer:swipe]; //_________________________平移手势__________________________ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [_myView addGestureRecognizer:pan]; //_________________________长按手势__________________________ UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; //设置长按的最短时间 longPress.minimumPressDuration = 2; [_myView addGestureRecognizer:longPress]; //_________________________旋转手势__________________________ UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; [_myView addGestureRecognizer:rotation]; //_________________________捏合手势__________________________ UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; [_myView addGestureRecognizer:pinch]; }#pragma mark - 手势响应事件//单击相应的事件- (void)tap1Action:(UITapGestureRecognizer *)tap { //取得点击手指的个数// NSInteger count = tap.numberOfTouches; CGPoint point = [tap locationInView:_myView]; NSString *str = NSStringFromCGPoint(point); NSLog(@"单击了,坐标是:%@",str); }- (void)tap2Action:(UITapGestureRecognizer *)tap { NSLog(@"双击了");}- (void)swipeAction:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"轻扫了"); } }//手指移动的时候会不停的调用这个方法:注意此时轻扫不能使用了- (void)panAction:(UIPanGestureRecognizer *)pan { //可以通过手势取得手势所在的视图 CGPoint point = [pan locationInView:pan.view];// NSString *str = NSStringFromCGPoint(point); // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, 5, 5)];// view.backgroundColor = [UIColor redColor];// [pan.view addSubview:view]; // NSLog(@"平移坐标:%@",str); }- (void)longPressAction:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"长按开始了"); }else if (longPress.state == UIGestureRecognizerStateEnded) { NSLog(@"长按结束了"); }}//旋转的时候一直调用- (void)rotationAction:(UIRotationGestureRecognizer *)rt {// NSLog(@"旋转了"); /* 180/M_PI = 角度/r; 角度 = 180*r/M_PI; */ CGFloat r = rt.rotation; // 角度 float jd = 180/M_PI*r; NSLog(@"jd:%.2f",jd); }- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {// NSLog(@"捏合"); //取得缩放的倍数 CGFloat scale = pinch.scale; // NSLog(@"scale:%.2f",scale); pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale); }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- 轻扫手势/平移、长按手势
- iOS 手势识别(点按、长按、轻扫)
- iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法
- iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法
- iOS 手势的使用 六个手势 轻拍 长按 拖拽 捏合 轻扫 旋转
- IOS学习 触摸和手势 手势识别器:单击、双击、轻扫、滑动、长按、旋转
- 轻扫手势
- 轻扫手势
- ios 轻扫手势
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
- iOS开发手势---拖动、捏合、旋转、点按、长按、轻扫
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
- UIGestureRecognizer iphone 手势识别(点击,捏合,旋转,拖拽,轻扫,长按)
- IOS 手势学习(点击,长按,轻扫,拖拽,旋转,捏合缩放)
- iOS 手势识别 ——长按和轻扫(swipe)
- 常用手势(敲击)(拖拽)(长按)(轻扫)(捏合)(选转)
- ios手势识别(敲击,捏合,拖拽,轻扫,旋转,长按)
- Redis源码分析(十九)——二进制位操作bitops
- 让apache虚拟目录外网可以访问
- 【Git学习笔记】对已经添加到仓库的文件修改后再次提交到仓库
- JUC (Java Util Concurrency) 基础内容概述
- CentOS下找不到eth0设备的解决方法
- 轻扫手势/平移、长按手势
- 网站常用的一些javascript封装 简化调用
- JCobBox的使用
- 强静态类型、变量、初始化
- 用readline巧妙增强mysql等linux命令行客户端
- 播客第一天 android之 aidl(一)
- 【匈牙利算法】匈牙利模板dfs、bfs
- Selenium 鼠标和键盘操作
- jQuery的选择器中的通配符