iOS中的手势
2016-05-10 00:00
423 查看
手势
单击或者双击手势
轻扫手势
移动手势
长按手势
旋转手势
捏合手势
单击或者双击手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //单击或者双击手势 [self addTapGesture]; } //单击或者双击手势的方法 -(void)addTapGesture{ //单击 UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]init]; //手指数量 tap1.numberOfTouchesRequired = 1; //单击次数 tap1.numberOfTapsRequired = 1; //添加单击方法 [tap1 addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:tap1]; //双击 UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]init]; //手指数量 tap2.numberOfTouchesRequired = 1; //双击次数 tap2.numberOfTapsRequired = 2; //添加双击方法 [tap2 addTarget:self action:@selector(haha2:)]; [self.view addGestureRecognizer:tap2]; //!!!注意:如果单击和双击同时进行,则取消单击手势(否则,如果单纯双击的话,会先出现一个单击,再出现一个双击) [tap1 requireGestureRecognizerToFail:tap2]; } //单击方法 -(void)haha:(UITapGestureRecognizer *)tap{ NSLog(@"单击"); } //双击方法 -(void)haha2:(UITapGestureRecognizer *)tap{ NSLog(@"双击"); }
轻扫手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //轻扫手势 [self addSwipeGesture]; } //轻扫手势 -(void)addSwipeGesture{ UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]init]; //设置轻扫方向(如果下面要添加不同方向的方法,则这里不要设置,视具体情况而定) swipe.direction = UISwipeGestureRecognizerDirectionUp; //添加轻扫方法 [swipe addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:swipe]; } //轻扫方法 -(void)haha:(UISwipeGestureRecognizer *)swipe{ if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"向右轻扫"); }else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){ NSLog(@"向左轻扫"); }else if (swipe.direction == UISwipeGestureRecognizerDirectionUp){ NSLog(@"向上轻扫"); } }
移动手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //移动手势 [self addPanGesture]; } //移动手势 -(void)addPanGesture{ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init]; //添加移动手势的方法 [pan addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:pan]; } //移动手势的方法 -(void)haha:(UIPanGestureRecognizer *)pan{ NSLog(@"移动"); }
长按手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //长按手势 [self addLongPressGesture]; } //长按手势 -(void)addLongPressGesture{ UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init]; //长按开始时间(手指按住屏幕5秒,才会开始进入长按时间) longPress.minimumPressDuration = 5; //长按手势方法 [longPress addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:longPress]; } //长按手势的方法 -(void)haha:(UILongPressGestureRecognizer *)longPress{ if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"长按开始"); }else if (longPress.state == UIGestureRecognizerStateEnded){ NSLog(@"长按结束"); } }
旋转手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //旋转手势 [self addRotationGesture]; } //旋转手势 -(void)addRotationGesture{ UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]init]; //旋转手势方法 [rotation addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:rotation]; } //旋转手势的方法 -(void)haha:(UIRotationGestureRecognizer *)rotition{ NSLog(@"旋转"); //获取旋转的弧度制 CGFloat r = rotition.rotation; float degress = 180/M_PI * r; NSLog(@"%f", degress); }
捏合手势
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //捏合手势 [self addPinchGesture]; } //捏合手势 -(void)addPinchGesture{ UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]init]; //捏合手势方法 [pinch addTarget:self action:@selector(haha:)]; [self.view addGestureRecognizer:pinch]; } //捏合手势的方法 -(void)haha:(UIPinchGestureRecognizer *)pinch{ NSLog(@"捏合手势"); CGFloat scale = [pinch scale]; self.view.transform = CGAffineTransformScale(self.view.transform, scale, scale); }
相关文章推荐
- iOS中的 摇一摇 功能实现
- 带着大家走进iOS单元测试世界
- 文件管理 - 4
- iOS开发总结(下)
- iOS开发获取设备唯一标识、系统版本、应用版本等信息
- iOS开发总结(上)
- iOS的几种本地存储
- iOS开发16进制颜色转10进制RGB颜色
- iOS SEL
- iOS开发- backBarButtonItem 颜色/文字修改
- iOS - (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc 代替方法的使用
- iOS 开发 带区号的城市三级联动(xml解析)
- IOS客户端Coding项目记录(六)
- iOS assign copy retain...含义
- XMPP在iOS上使用-翻译文档
- iOS学习——NSString属性的strong和copy
- iOS获取UDID问题
- [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
- iOS ffmpeg编译,C compiler test failed.问题
- CocoaPods解决Unable to add a source with url ...