iOS 手势
2021-11-04 本文已影响0人
失忆的程序员

手势类型
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势。其子类有:
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
属性state
另外,UIGestureRecognizer类中有一个属性state,表示手势的当前状态,如开始、正在..中、结束等。其子类也继承了这个属性,有以下状态:
UIGestureRecognizerStatePossible、Began、Changed、Ended、Cancelled、Failed。
// 轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(ActionSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[<#加在你要加的控件上#> addGestureRecognizer:swipe];
// 旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(ActionRotationGes:)];
[<#加在你要加的控件上#> addGestureRecognizer:rotation];
// 捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(ActionPinch:)];
[<#加在你要加的控件上#> addGestureRecognizer:pinch];
// 长按手势
UILongPressGestureRecognizer *longp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(ActionLongP:)];
[<#加在你要加的控件上#> addGestureRecognizer:longp];
// 点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ActionTap:)];
[<#加在你要加的控件上#> addGestureRecognizer:tap];
参考
