标签:
#import "ViewController.h"
@interface ViewController () {
UIView *_view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_view = [[UIView alloc] initWithFrame:CGRectMake(90, 90, 20, 20)];
_view.backgroundColor = [UIColor redColor];
[self.view addSubview:_view];
[self _initPinchGesture];
}
//--------------------轻击手势---------------------------
- (void)_initTap {
//单击手势
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
//设置点击的次数
tap1.numberOfTapsRequired = 1;
//设置手指的个数
tap1.numberOfTouchesRequired = 1;
//将手势添加到视图上
[self.view addGestureRecognizer:tap1];
//双击手势
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleAction:)];
tap2.numberOfTapsRequired = 2;
//添加手势
[self.view addGestureRecognizer:tap2];
//区别两个手势
[tap1 requireGestureRecognizerToFail:tap2];
}
//单击手势响应的事件
- (void)singleTapAction:(UITapGestureRecognizer *)tap {
//关闭手势
// tap.enabled = NO;
//获取手势所在的视图
UIView *view = tap.view;
view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(10)/10.0 green:arc4random_uniform(10)/10.0 blue:arc4random_uniform(10)/10.0 alpha:1];
NSLog(@"singleTapAction");
}
//双击手势响应的事件
- (void)doubleAction:(UITapGestureRecognizer *)tap {
NSLog(@"doubleAction");
}
//--------------------轻扫手势---------------------------
- (void)_initSwipeGesture {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//设置轻扫的方向
/*
UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp
UISwipeGestureRecognizerDirectionDown
*/
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
NSLog(@"swipeAction");
}
//--------------------平移手势---------------------------
- (void)_initPanGesture {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:panGesture];
}
- (void)panAction:(UIPanGestureRecognizer *)pan {
//手指所在的坐标
CGPoint point = [pan locationInView:self.view];
_view.center = point;
}
//--------------------长按手势---------------------------
- (void)_initLongGesture {
UILongPressGestureRecognizer *pressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressAction:)];
//设置最短时间
pressGesture.minimumPressDuration = 1;
[self.view addGestureRecognizer:pressGesture];
}
- (void)pressAction:(UILongPressGestureRecognizer *)press {
/*
UIGestureRecognizerStateBegan 开始
UIGestureRecognizerStateChanged 改变
UIGestureRecognizerStateEnded 结束
UIGestureRecognizerStateCancelled 取消
*/
if (press.state == UIGestureRecognizerStateBegan) {
NSLog(@"pressAction");
}
}
//--------------------旋转手势---------------------------
- (void)_initRotationGesture {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.userInteractionEnabled = YES;
imgView.image = [UIImage imageNamed:@"5.jpeg"];
[self.view addSubview:imgView];
UIRotationGestureRecognizer *rotationGresutre = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotationGresutre];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
NSLog(@"%f",rotationGesture.rotation);
UIImageView *imgView = (UIImageView *)rotationGesture.view;
if (rotationGesture.state == UIGestureRecognizerStateChanged) {
imgView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}else if (rotationGesture.state == UIGestureRecognizerStateEnded || rotationGesture.state == UIGestureRecognizerStateCancelled) {
[UIView animateWithDuration:0.3 animations:^{
imgView.transform = CGAffineTransformIdentity;
}];
}
}
//--------------------缩放手势---------------------------
- (void)_initPinchGesture {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.userInteractionEnabled = YES;
imgView.image = [UIImage imageNamed:@"5.jpeg"];
[self.view addSubview:imgView];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinchGesture];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
// pinch.scale; //缩放比
NSLog(@"pinchAction:%f",pinch.scale);
UIImageView *imgView = (UIImageView *)pinch.view;
if (pinch.state == UIGestureRecognizerStateChanged) {
imgView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}else if (pinch.state == UIGestureRecognizerStateEnded || pinch.state == UIGestureRecognizerStateCancelled) {
[UIView animateWithDuration:0.3 animations:^{
imgView.transform = CGAffineTransformIdentity;
}];
}
}
@end
标签:
原文地址:http://www.cnblogs.com/answer-Allen/p/4815330.html