IOS中几种常见手势的使用

  • 在IOS开发中,我们经常用到手势操作 比如捏合 、点击、 长按、旋转、滑动等 下面是我自己写的一个小例子 可以参照这个使用IOS开发中的几种手势操作.

代码区:##

#import "ViewController.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT
self.view.bounds.size.height
@interface ViewController (){
UIImageView *imageView ;
}

@end


@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

imageView = [[UIImageView alloc]init];

imageView.image = [UIImage imageNamed:@"屏幕快照 2015-12-09 下午4.42.42"];
imageView.frame = CGRectMake(0, 0, 270, 270);

imageView.center = CGPointMake(WIDTH/2, HEIGHT/2);

[self.view addSubview:imageView];

//允许和用户进行交互
imageView.userInteractionEnabled = YES;

//旋转手势
UIRotationGestureRecognizer *r1 = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRotate:)];

r1.delegate = self;
//添加手势识别器
[imageView addGestureRecognizer:r1];

//捏合手势

UIPinchGestureRecognizer *r2 = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doPinch:)];

r2.delegate = self;

[imageView addGestureRecognizer:r2];

//滑动手势

UISwipeGestureRecognizer *r3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doSwipe:)];

r3.direction = UISwipeGestureRecognizerDirectionRight;

[imageView addGestureRecognizer:r3];

UISwipeGestureRecognizer *r4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doSwipe:)];

r4.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:r4];

//点击手势
UITapGestureRecognizer *r5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapChange:)];
r5.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:r5];
//长按手势
UILongPressGestureRecognizer *r6 = [[UILongPressGestureRecognizer  alloc]initWithTarget:self action:@selector(longPressChange:)];
r6.minimumPressDuration = 1;
[self.view addGestureRecognizer:r6];
}
//旋转
-(void)doRotate:(UIRotationGestureRecognizer *)sender{
imageView.transform = CGAffineTransformRotate(imageView.transform, sender.rotation);
//每次转完后清0  防止叠加
sender.rotation = 0;
}
//捏合
-(void)doPinch:(UIPinchGestureRecognizer *)sender{
imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);
sender.scale = 1;
}
//滑动
-(void)doSwipe:(UISwipeGestureRecognizer *)sender{
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
imageView.center = CGPointMake(WIDTH/2+20, HEIGHT/2);
}
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
imageView.center = CGPointMake(WIDTH/2-20, HEIGHT/2);
}
}
//点击
-(void)doTapChange:(UITapGestureRecognizer *)sender{
imageView.frame = CGRectMake(0, 0, 100, 100);
}
//长按
-(void)longPressChange:(UILongPressGestureRecognizer *)sender{
//imageView.frame = CGRectMake(100, 100, 100, 100);
//获取长按的位置在屏幕上对应的点
CGPoint point  = [sender locationInView:self.view];
imageView.center = point;
}
//返回YES表示同时支持识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;

}
@end

你可能感兴趣的:(IOS中几种常见手势的使用)