屏幕的各种手势识别
#import "MangoView.h"
@interface MangoView()
@property(nonatomic,retain)UIImageView *rotationImageView;
@end
@implementation MangoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self loadingCustomView];
}
return self;
}
- (void)dealloc{
[self.rotationImageView release];
[super dealloc];
}
- (void)loadingCustomView{
self.backgroundColor = [UIColor orangeColor];
//轻拍手势
//初始化创建一个轻拍手势,并设置target action
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
//点击次数
tap.numberOfTapsRequired = 1;
//手指个数
tap.numberOfTouchesRequired = 1;
//将手势添加到视图上
[self addGestureRecognizer:tap];
[tap release];
//长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//长按时间
longPress.minimumPressDuration = 0.1;
//点击次数
longPress.numberOfTapsRequired = 1;
//longPress.numberOfTouchesRequired = 1;
[self addGestureRecognizer:longPress];
[longPress release];
//创建一个UIImageView+image相框加相片
self.rotationImageView = [[UIImageView alloc]initWithFrame:self.frame];
self.rotationImageView.image = [UIImage imageNamed:@"1.png"];
//打开imageView的用户交互
self.rotationImageView.userInteractionEnabled = YES;
[self addSubview:self.rotationImageView];
[self.rotationImageView release];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.rotationImageView addGestureRecognizer:rotation];
[rotation release];
//捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[self addGestureRecognizer:pinch];
[pinch release];
//平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.rotationImageView addGestureRecognizer:pan];
[pan release];
//轻扫
//只能设置水平或者竖直方向的轻扫,不可同时设置
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//设置方向,水平
swipe.direction = UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
[self.rotationImageView addGestureRecognizer:swipe];
[swipe release];
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe1Action:)];
//设置方向,竖直
swipe1.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
[self.rotationImageView addGestureRecognizer:swipe1];
[swipe1 release];
//从屏幕边缘轻扫
UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenAction:)];
//只能设置一个方向的边缘轻扫
screen.edges = UIRectEdgeLeft;
[self.rotationImageView addGestureRecognizer:screen];
[screen release];
}
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screeen{
NSLog(@"从屏幕边缘轻扫");
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"水平方向清扫");
}
- (void)swipe1Action:(UISwipeGestureRecognizer *)swipe1{
NSLog(@"竖直方向清扫");
}
- (void)rotation:(UIRotationGestureRecognizer *)ro{
//transform形变
//第一个参数是视图原来的形变状态,旋转手势的旋转弧度
self.rotationImageView.transform = CGAffineTransformRotate(self.rotationImageView.transform, ro.rotation);
NSLog(@"%f",ro.rotation);
//重置旋转角度,如果不重置那么每次的旋转都是基于原来的基础上旋转的(假如上次旋转到四十五°的位置,下一个点就是46°,如果不重置为0,那么下次的形变就是从45°的位置开始旋转46°,一下就到了91°,跨度范围越来越大,导致图片旋转越来越快)
ro.rotation = 0;
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPoint offset = [pan translationInView:self.rotationImageView];
self.rotationImageView.transform = CGAffineTransformTranslate(self.rotationImageView.transform, offset.x, offset.y);
//每次相对于0.0平移
[pan setTranslation:CGPointZero inView:self.rotationImageView];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
//捏合手势有一个属性scale就是捏合的比例
//第一个参数是上次形变的位置
self.rotationImageView.transform = CGAffineTransformScale(self.rotationImageView.transform, pinch.scale, pinch.scale);
NSLog(@"%f",pinch.scale);
//把缩放比例重置为1.0,每次放大和缩小的比例都基于1.0
pinch.scale = 1.0;
}
- (void)tapAction{
NSLog(@"1根手指点击一次");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
//长按手势默认执行两次,长按开始和手指离开都触发
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"*****");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"@@@@@");
}
}
@end
#import "MangoViewController.h"
#import "MangoView.h"
@interface MangoViewController ()
@end
@implementation MangoViewController
- (void)loadView{
[super loadView];
self.view = [[MangoView alloc] initWithFrame:self.view.frame];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end