注意:
只有加了下面的代码后,对同一个View才能同时使用两种手势:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
一些概念:
1. //什么是响应者对象?
//只要是能够接收事件并且能响应事件的对象,我们称作为响应者对象!在iOS中UIResponder这个类所创建的对象都是响应者对象!
//UIApplication是用来管理事件的,管理事件的方式是队列的方式(FIFO)
//前提条件 都是响应者对象 用户交互处于打开状态
//事件的分发顺序:UIApplication--->window--->viewController--->self.view--->self.view的子视图
//事件的响应顺序:self.view的子视图--->self.view--->viewController--->window--->UIApplication--->废弃
//事件没有响应的几个原因:
//用户交互处于关闭状态
//父视图的用户交互是否打开
//子视图超出父视图
//window的大小为全屏大小(window是产生事件的)
2.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"ViewController");
}
3. //单击手势
UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//设置点击次数 默认为1
tapOne.numberOfTapsRequired = 1;
//设置手指个数 默认为1
tapOne.numberOfTouchesRequired = 1;
//加手势
[imageView addGestureRecognizer:tapOne];
- (void)tap:(UITapGestureRecognizer *)tap
{
//相对于某一个视图 获取手势点击的位置
// [tap locationInView:tap.view]
CGPoint point = [tap locationInView:self.view];
//tap.view 通过这个属性我们可以获得添加此手势的视图
NSLog(@"图片被点击---%@---%@", tap.view, NSStringFromCGPoint(point));
}
4//单指双击
UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwo:)];
tapTwo.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapTwo];
/***********************************/
[tapOne requireGestureRecognizerToFail:tapTwo];
- (void)tapTwo:(UITapGestureRecognizer *)tap
{
NSLog(@"双击");
if (isTap == NO)
{
[UIView animateWithDuration:0.5 animations:^{
tap.view.frame = [UIScreen mainScreen].bounds;
}];
}
else
{
[UIView animateWithDuration:0.5 animations:^{
tap.view.frame = CGRectMake(40, 100, 200, 200);
}];
}
isTap = !isTap;
}
5. //长按手势 长按手势我们需要注意手势的状态 state
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//设置长按多少秒之后开始执行方法 默认为0.5
longPress.minimumPressDuration = 1.0;
[imageView addGestureRecognizer:longPress];
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan)
{
NSLog(@"长按手势");
}
else if (longPress.state == UIGestureRecognizerStateEnded)
{
NSLog(@"长按手势结束");
}
}
6. //滑动手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRight];
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
SecondViewController *vc2 = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc2 animated:YES];
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
SecondViewController *vc2 = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc2 animated:YES];
}
}
7. //移动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[view addGestureRecognizer:pan];
- (void)pan:(UIPanGestureRecognizer *)pan
{
UIView *view = pan.view;
//法一:
//获得偏移量
// CGPoint point = [pan translationInView:self.view];
// view.frame = CGRectMake(point.x+view.frame.origin.x, point.y+view.frame.origin.y, view.frame.size.width, view.frame.size.height);
CGPointZero 相当于 CGPointMake(0, 0)
// [pan setTranslation:CGPointZero inView:self.view];
//法二:
CGPoint point = [pan translationInView:self.view];
// view.transform = CGAffineTransformTranslate(view.transform, point.x, point.y);
//上面的self.view改为view 旋转后再移动就不变
view.center = CGPointMake(point.x+view.center.x, point.y+view.center.y);
[pan setTranslation:CGPointZero inView:self.view];
}
8.//捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[view addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
UIView *view = pinch.view;
CGFloat scale = pinch.scale;
view.transform = CGAffineTransformScale(view.transform, scale, scale);
pinch.scale = 1.0;
}
9.//旋转手势
UIRotationGestureRecognizer *rotatation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[view addGestureRecognizer:rotatation];
- (void)rotate:(UIRotationGestureRecognizer *)rotate
{
UIView *view = rotate.view;
CGFloat rotation = rotate.rotation;
view.transform = CGAffineTransformRotate(view.transform, rotation);
rotate.rotation = 0.0;
}