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

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

#import "ViewController.h"@interface ViewController (){    UIView *view1;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor brownColor];    view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    view1.backgroundColor = [UIColor greenColor];    [self.view addSubview:view1];//添加轻点手势    [self addGesture];//    平移手势    [self addPanGesAddSwipeGes];//    长按手势    [self longAction];//    旋转手势    [self rote];//    捏合手势    [self pinchGess];    }#define mark手势轻点- (void)addGesture{    //初始化手势识别器    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];    //设置点击次数    tap.numberOfTapsRequired = 1;    //设置触摸点数    tap.numberOfTouchesRequired = 1;    //视图添加手势    [self.view addGestureRecognizer:tap];    //添加target    [tap addTarget:self action:@selector(tapAct:)];        //手势的代理设置    tap.delegate = self;    }- (void)tapAct:(UIGestureRecognizer*)ges{    NSLog(@"点击了");}//添加旋转手势- (void)rote{    UIRotationGestureRecognizer *rate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(RoteAct:)];    [self.view addGestureRecognizer:rate];}#define mark旋转手势 点击手势的方法改变- (void)RoteAct:(UIRotationGestureRecognizer*)ratation{    if (ratation.state == UIGestureRecognizerStateChanged) {            view1.transform = CGAffineTransformMakeRotation(ratation.rotation);       }else if(ratation.state == UIGestureRecognizerStateEnded){        [UIView animateWithDuration:.35 animations:^{            //取消形变,恢复原状            view1.transform = CGAffineTransformIdentity;        }];           }    }#define mark添加平移和清扫手势- (void)addPanGesAddSwipeGes{   //创建平移手势    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];    pan.delegate = self;    [self.view addGestureRecognizer:pan];    // 设置最大识别数    pan.maximumNumberOfTouches = 1;//    pan.minimumNumberOfTouches = 1; //最小识别数    //添加平移手势调用的方法    [pan addTarget:self action:@selector(panAct:)];            //添加清扫手势    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]init];    swipe.delegate = self;    [self.view addGestureRecognizer:swipe];    [swipe addTarget:self action:@selector(swipAct:)];    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;#warning 优先级#define mark  识别器优先级冲突解决方案,优先级低的调用requireGestureRecognizerToFail方法    [pan requireGestureRecognizerToFail:swipe];    }//平移手势调用的方法- (void)panAct:(UIPanGestureRecognizer*)ges{#define mark获取当前位置的偏移量    CGPoint p = [ges translationInView:self.view];    NSLog(@"%@",NSStringFromCGPoint(p));}//清扫手势调用的方法- (void)swipAct:(UISwipeGestureRecognizer*)ges{    NSLog(@"-------%lu",ges.direction);}#define mark长按手势- (void)longAction{    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init];    //设置长按时间    longPress.minimumPressDuration = 2;    //添加手势    [self.view addGestureRecognizer:longPress];    [longPress addTarget:self action:@selector(action:)];}//长按手势调用的方法- (void)action:(UILongPressGestureRecognizer*)ges{    if (ges.state == UIGestureRecognizerStateBegan) {                UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"是否删除" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:@"其它", nil];    [actionSheet showInView:self.view];    }}#pragma mark-捏合手势- (void)pinchGess{    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];    [self.view addGestureRecognizer:pin];}- (void)pinchAction:(UIPinchGestureRecognizer *)pin{    if (pin.state == UIGestureRecognizerStateChanged) {            view1.transform = CGAffineTransformMakeScale(pin.scale, pin.scale);        }else if(pin.state == UIGestureRecognizerStateEnded){        [UIView animateWithDuration:.35 animations:^{            //取消一切形变            view1.transform = CGAffineTransformIdentity;        }];    }    }#define mark代理方法 识别器将要开始工作- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{        switch (gestureRecognizer.state) {            case UIGestureRecognizerStatePossible:            NSLog(@"等待");            break;        case UIGestureRecognizerStateBegan:            NSLog(@"开始");            break;            case UIGestureRecognizerStateChanged:            NSLog(@"改变");            break;            case UIGestureRecognizerStateEnded:            NSLog(@"结束");            break;            case UIGestureRecognizerStateFailed:            NSLog(@"失败");        default:            break;    }    return YES;}//代理方法,识别器将要识别- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    NSLog(@"");    return YES;}

 

转载于:https://www.cnblogs.com/zxh-iOS/p/5237714.html

你可能感兴趣的文章
QT - 内存泄漏检测
查看>>
三层架构
查看>>
epoll使用具体解释(精髓)
查看>>
数据库设计笔记
查看>>
JPA进行insert操作时会首先select吗
查看>>
AndroidArchitecture
查看>>
原生JavaScript第六篇
查看>>
JS基础学习3
查看>>
Tennis Championship
查看>>
SQL
查看>>
JavaScript基础-var
查看>>
javascript 进阶篇1 正则表达式,cookie管理,userData
查看>>
安装Endnote X6,但Word插件显示的总是Endnote Web"解决办法
查看>>
python全栈 计算机硬件管理 —— 硬件
查看>>
用WebClinet实现SharePoint上文档库中文件的上传与下载
查看>>
Silverlight和javascript的相互调用
查看>>
SQL Server 2005 Express 附加数据库只读 解决方案
查看>>
opencv中的Bayes分类器应用实例
查看>>
大数据学习
查看>>
[BZOJ2982]combination
查看>>