iOS手势应用
2016-07-25 本文已影响37人
Mr丶炎
常用手势包括点按、轻扫、长按、捏合、旋转、拖拽手势等等
注意点:
- 对图片添加手势要开启用户交互
- 可以对图片添加多个手势,要实现代理协议,这个意思是说手势同时执行,比如一边放大一边旋转。
点按手势requireGestureRecognizerToFail
- 这个方法很好的控制了冲突问题,双击的时候点击也调用了
#pragma mark - 点按手势
// 创建点按手势
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
tap1.delegate = self;
[_middleImageView addGestureRecognizer:tap1];
// 创建点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 点击两次触发
tap.numberOfTapsRequired = 2;
tap.delegate = self;
[_middleImageView addGestureRecognizer:tap];
// 双击失败算单机
[tap1 requireGestureRecognizerToFail:tap];
- (void)tap1:(UITapGestureRecognizer *)tap1 {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)tap:(UITapGestureRecognizer *)tap {
WXLog(@"///");
// 获取手指的触摸点
// CGPoint curP = [tap locationInView:_middleImageView];
_middleImageView.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
轻扫手势, direction是手势的方向
// 添加手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipe];
#pragma mark - 轻扫手势
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
[self shouHead];
self.btn.selected = YES;
}
捏合手势
#pragma mark - 捏合手势
- (void)setUpPinch {
UIPinchGestureRecognizer *pingch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pingch:)];
pingch.delegate = self;
[_middleImageView addGestureRecognizer:pingch];
}
- (void)pingch:(UIPinchGestureRecognizer *)pingch {
_middleImageView.transform = CGAffineTransformScale(_middleImageView.transform, pingch.scale, pingch.scale);
// 复位
pingch.scale = 1;
}
旋转手势
#pragma mark - 旋转手势
- (void)setUpRotation {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_middleImageView addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
_middleImageView.transform = CGAffineTransformRotate(_middleImageView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
}
长按手势
#pragma mark - 长按手势
- (void)setUpLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.delegate = self;
[_middleImageView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateEnded) {
// 弹出alert ,保存到手机
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"保存图片"
message:@"是否保存"
preferredStyle:UIAlertControllerStyleActionSheet];
// 取消
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
WXLog(@"取消");
}];
// 保存到手机
UIAlertAction *phone = [UIAlertAction actionWithTitle:@"保存到手机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImageWriteToSavedPhotosAlbum(self.middleImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}];
[sheet addAction:phone];
[sheet addAction:cancel];
// 显示到界面
[self presentViewController:sheet animated:YES completion:nil];
}
}
// 存储图片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失败!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存成功!"];
}
}
拖拽手势
#pragma mark - 拖拽手势
- (void)setUpPan {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_middleImageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
// 获取手势的移动
CGPoint tranP = [pan translationInView:_middleImageView];
_middleImageView.transform = CGAffineTransformTranslate(_middleImageView.transform, tranP.x, tranP.y);
// 复位
[pan setTranslation:CGPointZero inView:_middleImageView];
}
- 协议方法,应该说允许同时执行两个手势,比如边放大边缩放
#pragma mark - UIGestureRecognizerDelegate代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}