UI: UIGestureRecognizer IOS中手势的用法


UIGestureRecognizer 手势识别器,是常用手势的父类

可以手写代码,也可以拖拽应用手势。

1.触摸Touch

四个方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _label1.text = @"触摸开始";
    //1.获得触摸屏幕的手指
    UITouch *touch = [touches anyObject];
    //2.得到当前手指所在的位置的坐标
    _startPoint = [touch locationInView:self.view];
    //3.显示坐标
    _label1.text = [NSString stringWithFormat:@"开始:x=%.1f,y=%.1f",_startPoint.x,_startPoint.y];
    //5.获得触摸屏幕次数
    int tapCount = touch.tapCount;
    //6.获得点击手指数目
    int fingerCount = touches.count;
    _label4.text = [NSString stringWithFormat:@"触摸次数:%i,手指数:%i",tapCount,fingerCount];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //只要有移动就进入此方法
    _label2.text = @"触摸move";
    //1.获得触摸屏幕的手指
    UITouch *touch = [touches anyObject];
    //2.得到当前手指所在的位置的坐标
    CGPoint point = [touch locationInView:self.view];
    //3.显示坐标
    _label2.text = [NSString stringWithFormat:@"x=%.1f,y=%.1f",point.x,point.y];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    _label3.text = @"触摸end";
    //1.获得触摸屏幕的手指
    UITouch *touch = [touches anyObject];
    //2.得到当前手指所在的位置的坐标
    _endPoint = [touch locationInView:self.view];
    //3.显示坐标
    _label3.text = [NSString stringWithFormat:@"x=%.1f,y=%.1f",_endPoint.x,_endPoint.y];
    //4.计算位移  fabsf()计算绝对值
    float distancesX = fabsf(_endPoint.x - _startPoint.x);
    float distancesY = fabsf(_endPoint.y - _startPoint.y);
    _label4.text = [NSString stringWithFormat:@"结束:x方向位移:%.1f,y方向位移:%.1f",distancesX,distancesY];

    //7.判断最后的点是否在图片里面
    if (CGRectContainsPoint(_imageView.frame, _endPoint)) {
        _label4.text = @"抬起时在图片里面";
    }else{
        _label4.text = @"抬起时不在图片里面";
    }

    //默认接受单点触碰事件,多点触碰需要开启
    self.view.multipleTouchEnabled = YES;
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    _label.text = @"触摸cancel";
}

2.清扫

//1.创建轻扫手势对象,为它添加执行手势时候的方法
    UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
    //2.设定轻扫手势的方向
    /*
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     */
    swipeR.direction = UISwipeGestureRecognizerDirectionRight;

    //3.将手势添加到视图上
    [self.view addGestureRecognizer:swipeR];

    //不能为一个手势添加多个轻扫方向!!!

    //向左清扫
    UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
    swipeL.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeL];

    //向上清扫
    UISwipeGestureRecognizer *swipeU = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpAction:)];
    swipeU.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeU];

    //向下清扫
    UISwipeGestureRecognizer *swipeD = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownAction:)];
    swipeD.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeD];

3.点击

//1.创建单击手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleAction:)];
    //2.1设置点击的手指数
    [singleTap setNumberOfTouchesRequired:1];
    //2.2设置点击次数
    [singleTap setNumberOfTapsRequired:1];
    //3.手指加到视图上
    [self.view addGestureRecognizer:singleTap];

    //一个手指双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleAction:)];
    [doubleTap setNumberOfTouchesRequired:1];
    [doubleTap setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleTap];

    //双击的时候可以响应双击时间,但是同时也被看成是两次单击事件,所以需要设定手势的优先级
    [singleTap requireGestureRecognizerToFail:doubleTap];
    //单击优先级低于双击的优先级别

    //两个手指的双击事件
    UITapGestureRecognizer *doubleTapEve = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapEveAction:)];
    [doubleTapEve setNumberOfTouchesRequired:2];
    [doubleTapEve setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleTapEve];

    //1个手指的三击事件
    UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tripleTapAction:)];
    [tripleTap setNumberOfTouchesRequired:1];
    [tripleTap setNumberOfTapsRequired:3];
    [self.view addGestureRecognizer:tripleTap];

    [doubleTap requireGestureRecognizerToFail:tripleTap];

4.长按

// 1.创建长按的手势
    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressAction:)];
    //2.1设置长按的时间
    press.minimumPressDuration = 1;
    //2.2设置手指数量
    [press setNumberOfTouchesRequired:1];

    //3.把手势加到视图上
    //如果加到UILabel、UIImageView等上面,需要开启用户交互才能使用
    _label.userInteractionEnabled = YES;
    [_label addGestureRecognizer:press];
//    [self.view addGestureRecognizer:press];

5.捏合

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1.创建捏合手势(默认捏合手指数为2)
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

    //2.加到视图上
    [_imageView addGestureRecognizer:pinch];
    _imageView.userInteractionEnabled = YES;
}
-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{
    _imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    if (pinch.scale == UIGestureRecognizerStateEnded) {
        _imageView.transform = CGAffineTransformMakeScale(1, 1);
    }
}

6.旋转

//1.创建旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

    //2.添加手势
    _imageView.userInteractionEnabled = YES;
    [_imageView addGestureRecognizer:rotation];
}
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{
    _imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
}

7.拖拽

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1.创建拖拽的手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

    //2.把手势加到视图上
    [_imageView addGestureRecognizer:pan];
    _imageView.userInteractionEnabled = YES;
}
-(void)panAction:(UIPanGestureRecognizer*)pan{
    //1.取到图片中点坐标
    CGPoint centerPoint = _imageView.center;
    //2.获取拖拽手势相对于当前视图的偏移位置
    CGPoint point = [pan translationInView:self.view];

    //3.计算图片新的坐标
    _imageView.center = CGPointMake(centerPoint.x+point.x, centerPoint.y+point.y);
    //4.每拖拽一点,就将translation设置为CGPointZero,否则每次拖拽都会在原有基础上累加
    if (pan.state == UIGestureRecognizerStateChanged) {
        [pan setTranslation:CGPointZero inView:self.view];
    }
}
时间: 2024-09-29 09:50:15

UI: UIGestureRecognizer IOS中手势的用法的相关文章

iOS中手势的delaysTouchesBegan属性用法(挖坑)

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/css/cuteeditor.css);iOS中手势的delaysTouchesBegan属性用法(挖坑),布布扣,bubuko.com

IOS中手势UIGestureRecognizer

通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRotationGestureRecognizer IOS中手势有很多: UIRotationGestureRecognizer旋转 UITapGestureRecognizer手指点击 UIPinchGestureRecognizer缩放 UISwipeGestureRecognizer手指快速扫过 UI

IOS中NSSData常见用法

一.NSdata的概念 1.使用文件时需要频繁地将数据读入一个临时存储区,它通常称为缓冲区 2.NSdata类提供了一种简单的方式,它用来设置缓冲区,将文件的内容读入缓冲区,或者将缓冲区内容写到一个文件. 3.对于32位应用程序,NSdata缓存最多2GB 4.我们有两种定义 NSData(不可变缓冲区),NSMutableData(可变缓冲区) NSData *fileData; NSFileManager *fileManager = [[NSFileManager alloc]init];

IOS中NSNumber常见用法

一.NSnumber常见用法 NSNumber + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithDouble:(double)value; - (int)intValue; - (double)doubleValue; -(float)floatValue; 二.使用 NSNumber * intNumber=[NSNumber numberWithInt:100]: NSNumber *floatNumber=[N

IOS中NSUserDefaults的用法(轻量级本地数据存储)

iOS数据保存 IOS中NSUserDefaults的用法(轻量级本地数据存储) IOS中NSUserDefaults的用法(轻量级本地数据存储),布布扣,bubuko.com

ios中NSUserDefaults的用法

ios中NSUserDefaults的用法 NSUserDefaults类提供了一个与默认系统进行交互的编程接口.NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等.默认系统允许应用程序自定义它的行为去迎合用户的喜好.你可以在程序运行的时候从用户默认的数据库中读取程序的设置.同时NSUserDefaults的缓存避免了在每次读取数据时候都打开用户默认数据库的操作.可以通过调用synchronize方法来使内存中的缓存与用户默认系统进行同步. NSUserDefa

IOS中TableView的用法

IOS中TableView的用法 一.UITableView 1.数据展示的条件 1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象 2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法 3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源

iOS中手势的使用

在iOS中添加手势比较简单,可以归纳为以下几个步骤: 创建对应的手势对象: 设置手势识别属性[可选]: 附加手势到指定的对象: 编写手势操作方法: 为了帮助大家理解,下面以一个图片查看程序演示一下上面几种手势,在这个程序中我们完成以下功能: 如果点按图片会在导航栏显示图片名称: 如果长按图片会显示删除按钮,提示用户是否删除: 如果捏合会放大.缩小图片: 如果轻扫会切换到下一张或上一张图片: 如果旋转会旋转图片: 如果拖动会移动图片: 具体布局草图如下: 为了显示导航条,我们首先将主视图控制器KC

iOS中block的用法 以及和函数用法的区别

ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候  MyBlock(); 带参数的block: int ^(MyBlock)(int,int) = ^(int a,int b){return a+b;} 调用MyBlock(5,6); 将block当作某个类的属性的写法 typedef void (^BlockOption)(); @property (nonatomic,ass