#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createUI];
}
//创建UI
- (void)createUI{
NSArray *array = [NSArray arrayWithObjects:@"blue.png",@"red.png",@"yellow.png",nil];
for (int i=0; i<array.count; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+i*100,30+i*110, 100,100)];
imageView.image = [UIImage imageNamed:array[i]];
//默认情况下UIImageView UILabel无法响应用户的交互, 需要将他们的userInteractionEnabled= YES
imageView.userInteractionEnabled = YES;
//向imageView中添加多个手势
imageView.tag = 100+i;
[self addGestureToView:imageView];
[self.view addSubview:imageView];
}
}
//向视图中添加手势的方法
- (void)addGestureToView:(UIView *)myView{
//UIGestureRecognizer 所有手势的父类
//点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//将手势添加到视图上
//设置点击次数(双击)
tap.numberOfTapsRequired =2;
tap.delegate =self;
//需要多少个手指点击
//tap.numberOfTouchesRequired =2;
[myView addGestureRecognizer:tap];
//移动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
pan.delegate = self;
[myView addGestureRecognizer:pan];
//缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
pinch.delegate = self;
[myView addGestureRecognizer:pinch];
//旋转手势
UIRotationGestureRecognizer *rot = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
rot.delegate = self;
[myView addGestureRecognizer:rot];
}
- (void)rotationGesture:(UIRotationGestureRecognizer *)rot{
if (rot.state == UIGestureRecognizerStateBegan || rot.state == UIGestureRecognizerStateChanged) {
UIView *view = rot.view;
//改变旋转矩阵(改变的是view旋转的角度)
view.transform = CGAffineTransformRotate(view.transform,rot.rotation);
//手势的rotation属性能够拿到视图的旋转的角度
//重置角度为0
rot.rotation = 0.0;
}
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
if (pinch.state == UIGestureRecognizerStateBegan||pinch.state == UIGestureRecognizerStateChanged) {
UIView *view = pinch.view;
//transform 仿射变换矩阵:里面预置了view的位置和缩放倍数、角度等一些参数
//改变矩阵中的缩放参数
//CGAffineTransformScale 缩放的矩阵
//x y两个方向等倍缩放
view.transform = CGAffineTransformScale(view.transform,pinch.scale,pinch.scale);
//手势的scale属性能够拿到视图的缩放倍数
//重置缩放系数为1.0
pinch.scale = 1.0;
}
}
//view在移动的过程中会时时调用此方法
- (void)panGesture:(UIPanGestureRecognizer *)pan{
//实现view的移动
//一个手势作用的生命周期(began-changed-ended)
if (pan.state==UIGestureRecognizerStateBegan||pan.state==UIGestureRecognizerStateChanged) {
//认为手势有效
//通过移动手势,能够获取到imageView在self.view上的偏移量
CGPoint pt = [pan translationInView:self.view];
UIView *view = pan.view;
//改变中心点的坐标,加上偏移量
view.center = CGPointMake(view.center.x+pt.x, view.center.y+pt.y);
//每次调用完此方法之后,需要重置手势的偏移量,否则移动手势会自动累加偏移量
//CGPointZero CGPointMake(0,0)
[pan setTranslation:CGPointZero inView:self.view];
}
}
- (void)tapGesture:(UITapGestureRecognizer *)tap{
NSLog(@"tap!");
//手势的view属性,能够拿到手势所在的视图
UIView *view = tap.view;
NSLog(@"view tag:%ld",(long)view.tag);
}
//设置视图是否可以同时识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
//如果两个手势都在同一个视图上
if ([gestureRecognizer.view isEqual:otherGestureRecognizer.view]) {
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end