博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中的手势
阅读量:6412 次
发布时间:2019-06-23

本文共 4421 字,大约阅读时间需要 14 分钟。

hot3.png

手势

单击或者双击手势

#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);        }

 

转载于:https://my.oschina.net/LBBB/blog/672946

你可能感兴趣的文章
nginx反向代理配置及优化
查看>>
FlashRAID技术白皮书
查看>>
权限的管理
查看>>
思科GNS3
查看>>
前端基础01 HTML
查看>>
直方图均衡化 EqualizeHist
查看>>
tcpdump+wireshark 调试网络应用
查看>>
CentOS6.4配置yum源
查看>>
MFC 用CreateWindow创建的动态Button,如何指定ID?
查看>>
linux sed常用
查看>>
excel数据统计分析面面观
查看>>
jquery Map
查看>>
Linux下安装JDK的几种方法
查看>>
Centos6.5 WIFI无线网卡驱动BCM43142驱动安装
查看>>
linux 安装 oracle 11g
查看>>
我的友情链接
查看>>
windows注册表修改同步时间频率
查看>>
nginx的七层负载均衡
查看>>
TestNG report in IDEA
查看>>
mysql事务
查看>>