#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
/** 图片 */
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.userInteractionEnabled = YES;
[self setUpTap];
[self setUpLongPress];
[self setUpSwipe];
[self setUpRotation];
[self setPinch];
[self setPan];
}
/*------------长按手势----------------*/
- (void)setUpTap {
// 创建点击事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tap.delegate = self;
[self.imageView addGestureRecognizer:tap];
}
// 点击事件
- (void)tap {
NSLog(@"%s", __func__);
}
/*------------长按手势----------------*/
- (void)setUpLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.delegate = self;
[self.imageView addGestureRecognizer:longPress];
}
// 长按手势事件
- (void)longPress:(UILongPressGestureRecognizer *)sender {
// 注意:长安手势可以触发两次事件,我们需要判断手势状态,如下
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"%s", __func__);
}
}
/*------------轻扫手势----------------*/
- (void)setUpSwipe {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.delegate = self;
// 注意:如果以后想要一个控件支持多个方向的清扫,必须创建多个清扫手势,一个清扫手势只能支持一个方向
// 默认清扫手势的方向是向右
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:swipe];
}
// 轻扫手势事件
- (void)swipe:(UISwipeGestureRecognizer *)sender {
NSLog(@"%s", __func__);
}
/*------------旋转手势----------------*/
- (void)setUpRotation {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
// 旋转手势事件
- (void)rotation:(UIRotationGestureRecognizer *)sender {
// 注意:手势传递的旋转角度都是相对于最开始的位置
CGFloat rotation = sender.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation);
// 复位
sender.rotation = 0;
NSLog(@"%s", __func__);
}
/*------------捏合手势----------------*/
- (void)setPinch {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
// 捏合手势事件
- (void)pinch:(UIPinchGestureRecognizer *)sender {
CGFloat scale = sender.scale;
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
// 复位
sender.scale = 1.0;
NSLog(@"%s", __func__);
}
/*------------拖拽手势----------------*/
- (void)setPan {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.delegate = self;
[self.imageView addGestureRecognizer:pan];
}
// 拖拽手势事件
- (void)pan:(UIPanGestureRecognizer *)sender {
// 获取手势的移动,也是相对于最开始的位置
CGPoint transP = [sender translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 复位
[sender setTranslation:CGPointZero inView:self.imageView];
NSLog(@"%s", __func__);
}
#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
// 是否允许同时支持多个手势, 默认不支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
// 是否可以接收手指的触摸点(touch)
// 实例:该方法可以控制让一个视图,左边能点击,右边不能点击
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
// CGPoint currentPoint = [touch locationInView:self.imageView];
//
// if (currentPoint.x > self.imageView.bounds.size.width * 0.5) {
//
// return YES;
// }
//
// return NO;
//}
@end
几种常用的iOS手势操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在 iOS 的开发过程中,我们一般使用 touches 方法来监听 view 的触摸事件,但是这样使用会有一些弊端...
- 一、概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: ...