UI手势

作者: dliys | 来源:发表于2016-02-18 17:39 被阅读0次

UIGestureRecognizer手势识别器

手势:有规律的触摸

UIGestureRecognizer抽象类

七种手势:轻拍(tap)长按(longPress)旋转(rotation)捏合(pinch)拖拽(pan)轻扫(swipe)屏幕边缘拖拽(screenEdgePan)

轻拍tap

创建对象

获取到轻拍手势时让self调用tapAction:方法

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapAction:)];

添加手势

[imgView addGestureRecognizer:tap];

内存管理

[tap release];

点击次数

tap.numberOfTapsRequired =2;

手指个数

tap.numberOfTouchesRequired =2;

#pragma mark -轻拍tap

- (void)tapAction:(UITapGestureRecognizer*)tap

{

NSLog(@"轻拍");

}

长按longPress

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPressAction:)];

[imgView addGestureRecognizer:longPress];

[longPress release];

长按时间

longPress.minimumPressDuration =1;

#pragma mark -长按longPress

- (void)longPressAction:(UILongPressGestureRecognizer*)longPress

{

if(longPress.state==UIGestureRecognizerStateBegan) {

NSLog(@"长按");

}

}

旋转rotation

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationAction:)];

[imgView addGestureRecognizer:rotation];

[rotation release];

#pragma mark -旋转rotation

- (void)rotationAction:(UIRotationGestureRecognizer*)rotation

{

*UIView transform属性专门用来进行形变(位置position/旋转rotation/缩放scale)设置

UIImageView*imgView = (UIImageView*)rotation.view;

imgView.transform=CGAffineTransformMakeRotation(rotation.rotation);

NSLog(@"旋转");

}

捏合pinch

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinchAction:)];

[imgView addGestureRecognizer:pinch];

[pinch release];

#pragma mark -捏合pinch

- (void)pinchAction:(UIPinchGestureRecognizer*)pinch

{

UIImageView*imgView = (UIImageView*)pinch.view;

imgView.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);

//缩放scale(比例)(下面两行代码效果等同于上一行)

//    imgView.transform = CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale);

//    pinch.scale = 1;

NSLog(@"捏合");

}

拖拽pan

UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

[imgViewaddGestureRecognizer:pan];

[panrelease];

#pragma mark -拖拽pan

- (void)panAction:(UIPanGestureRecognizer*)pan

{

NSLog(@"拖拽");

UIImageView*imgView = (UIImageView*)pan.view;

CGPointp = [pantranslationInView:imgView];

imgView.transform=CGAffineTransformMakeTranslation(p.x, p.y);

//设置跟随属性(下面两行代码效果等同于上一行)

//    imgView.transform = CGAffineTransformTranslate(imgView.transform, p.x, p.y);

//    [pan setTranslation:CGPointZero inView:imgView];

}

轻扫swipe

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];

默认只识别向右

设置方向时最多只能设置水平(左/右)或者垂直(上/下)

swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

[imgView addGestureRecognizer:swipe];

[swipe release];

#pragma mark -轻扫swipe

- (void)swipeAction:(UISwipeGestureRecognizer*)swipe

{

if(swipe.direction==UISwipeGestureRecognizerDirectionLeft) {

NSLog(@"向左");

}

NSLog(@"轻扫");

}

屏幕边缘拖拽screenEdgePan

UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];

sep.edges=UIRectEdgeLeft;

[self.viewaddGestureRecognizer:sep];

[seprelease];

#pragma mark -屏幕边缘拖拽screenEdgePan

- (void)sepAction:(UIScreenEdgePanGestureRecognizer*)sep

{

NSLog(@"屏幕边缘");

}

相关文章

  • UI总结-手势

    UI总结-手势 常用的6种手势: #import "ViewController.h" @...

  • UI手势

    手势识别器有:轻拍 长按 旋转 捏合 拖拽 清扫 屏幕边缘拖拽 在使用这些手势识别器时 先打开用户交互 image...

  • UI手势

    UIGestureRecongnizer:NSObject 1.-(void)addGestureRecogniz...

  • UI - 手势

    UIGestureRecognizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象类...

  • UI手势

    手势:有规律的触摸 UIGestureRecognizer抽象类 七种手势:轻拍(tap)长按(longPress...

  • UI手势

    UIGestureRecognizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象类...

  • 5.2 GestureRecognizer---UI手势

    GestureRecognizer---UI手势 基础控制器 导航栏视图控制器, 根试图控制器 单击手势 双击手势...

  • UI梳理——手势

    手势分类: 手势的创建: 方法的实现: 轻扫手势:UISwipeGestureRecognizer 长按手势: 以...

  • UI手势回顾

    UIGestureRecongnizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象...

  • UI手势控件

    一、拖拽 示例代码: 复制代码 1 // 2 // YYViewController.m 3 // 06-拖拽事件...

网友评论

      本文标题:UI手势

      本文链接:https://www.haomeiwen.com/subject/ibwmkttx.html