UI手势

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(@"屏幕边缘");

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • - (void)viewDidLoad { [super viewDidLoad]; // Do any addi...
    焦六金Jxx阅读 2,817评论 0 0
  • UIGestureRecongnizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象...
    陈亮宇阅读 1,864评论 0 0
  • 手势:有规律的触摸 UIGestureRecognizer抽象类 七种手势:轻拍(tap)长按(longPress...
    爱吃芒果的淼小猪阅读 3,434评论 0 1
  • UIGestureRecognizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象类...
    Sherry宇阅读 2,948评论 0 0
  • //七中手势:轻拍(tap) 长按(longPress) 旋转(rotation) 捏合(pinch) 拖拽(p...
    画个完美句号阅读 1,837评论 1 1

友情链接更多精彩内容