UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件。
- 1. 一根或者多根手指开始触摸屏幕
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- 2.一根或者多根手指在屏幕上移动(随着手指的移动,会持续调用该方法)
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- 3.一根或者多根手指离开屏幕
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- 4.触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程
- - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- 注意:所有UIKit控件均继承自UIView
常用属性:
- 1.UITouch *touch = [touches anyObject];//1. 从NSSet中取出UITouch对象
- 2.CGPoint location = [touch locationInView:self.view];//// 2. 手指触摸的位置
- 3.[touch previousLocationInView:]方法对位置进行修正
- 4.touches.count来判断是多点还是单点触摸
- 5. [self.view setMultipleTouchEnabled:YES]设置为yes 支持多点触摸
- 6.CGPoint greenPoint = [selfconvertPoint:point toView:self.greenView];//将point有点击的试图转到greenView上,返回此点在greenView上到坐标
- 7.[self.greenViewpointInside:greenPoint withEvent:event]判断greenPoint是否在greenView上
- 8.[touch view] 获取但前触摸到试图
- #pragma mark 触摸移动 -在一次触摸事件中,可能会被调用多次
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- // 1. 从NSSet中取出UITouch对象
- UITouch *touch = [touchesanyObject];
- // 2. 知道手指触摸的位置
- CGPoint location = [touchlocationInView:self.view];
- // 2.1 可以利用[touch previousLocationInView:]方法对位置进行修正
- CGPoint pLocation = [touchpreviousLocationInView:self.view];
- CGPoint deltaP =CGPointMake(location.x - pLocation.x, location.y - pLocation.y);
- CGPoint newCenter =CGPointMake(self.redView.center.x + deltaP.x, self.redView.center.y + deltaP.y);
- // 3. 设置红色视图的位置
- [self.redViewsetCenter:newCenter];
- }
多点触摸:
在iOS中,视图默认不支持多点触摸,通常在使用UITouch开发时,为了避免不必要的麻烦,也最好不要支持多点
需要支持多点必须把 [self.viewsetMultipleTouchEnabled:YES]设置为yes
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- //可以通过touches.count来判断是多点还是单点触摸
- // 遍历touches集合来添加图像
- NSInteger i =0;
- for (UITouch *touchin touches) {
- UIImageView *imageView = [[UIImageViewalloc]initWithImage:self.images[i]];
- CGPoint location = [touchlocationInView:self.view];//手指触摸的位置
- [imageView setCenter:location];
- [self.viewaddSubview:imageView];
- [UIViewanimateWithDuration:2.0fanimations:^{
- [imageView setAlpha:0.5f];
- } completion:^(BOOL finished) {
- [imageView removeFromSuperview];
- }];
- i++;
- }
- }
摇晃事件:
摇晃的试图必须重写此方法,并返回yes
- - (BOOL)canBecomeFirstResponder
- {
- returnYES;
- }
- #pragma mark - 监听运动事件
- - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
- {
- if (UIEventSubtypeMotionShake == motion) {
- NSLog(@"摇晃啦");
- // 摇晃的后续处理
- //
- // 1. 记录用户当前位置(经纬度)
- // 2. 去服务器上查询当前摇晃手机的用户记录
- // 3. 根据用户所在经纬度,取出前N名距离用户最近的用户记录
- // 4. 将用户记录发送到用户手机
- // 5. 手机接收到数据后,self.tableView reloadData
- }
- }
-
- // 1. 点按手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
- // 点按次数,例如双击2
- // 注意:在iOS中最好少用双击,如果一定要用,就一定要有一个图形化的界面告知用户可以双击
- [tap setNumberOfTapsRequired:2];
- // 用几根手指点按
- [tap setNumberOfTouchesRequired:1];
- [imageView addGestureRecognizer:tap];
- // 2. 长按手势 Long Press
- UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
- [imageView addGestureRecognizer:longTap];
- // 3. 拖动手势 Pan
- UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
- [imageView addGestureRecognizer:pan];
- // 4. 旋转手势 Rotation
- UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
- [imageView addGestureRecognizer:rotation];
- // 5. 缩放(捏合)手势 Pinch
- UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
- [imageView addGestureRecognizer:pinch];
- // 6. 轻扫手势 Swipe
- /*
- 手指在屏幕上扫动,和拖动手势的区别在于,手指离开屏幕才会触发监听方法
- 1> 手指可以上、下、左、右四个方向轻轻扫动,如果没有指定方向,默认都是向右扫动
- 2> 在设置轻扫手势时,通常需要将手势识别添加到父视图上监听
- 3> 在监听方法中,注意不要使用recognizaer.view,因为手势监听的是父视图,而要处理的视图通常是其他的视图
- 如果要使用多个方向的轻扫手势,需要指定多个轻扫手势,通常只需指定左右两个方向即可。
- 因为iOS用户大多不习惯上下的轻扫动作
- */
- UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe1 setDirection:UISwipeGestureRecognizerDirectionUp];
- [self.view addGestureRecognizer:swipe1];
- UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe2 setDirection:UISwipeGestureRecognizerDirectionDown];
- [self.view addGestureRecognizer:swipe2];
- UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe3 setDirection:UISwipeGestureRecognizerDirectionLeft];
- [self.view addGestureRecognizer:swipe3];
- UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
- [swipe4 setDirection:UISwipeGestureRecognizerDirectionRight];
- [self.view addGestureRecognizer:swipe4];
- #pragma mark - 手势监听方法
- #pragma mark 轻扫手势
- // 尽管轻扫手势也是连续手势,但是该手势是在手指离开屏幕才会被触发的
- // 因此,在编写代码时,不需要处理手势的状态
- - (void)swipe:(UISwipeGestureRecognizer *)recognizer
- {
- // 让图片朝手指不同的方向飞出屏幕,然后再复位
- CGRect frame = kImageInitFrame;
- if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {
- frame.origin.y -= 400;
- } else if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) {
- frame.origin.y += 400;
- } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {
- frame.origin.x -= 300;
- } else {
- frame.origin.x += 300;
- }
- [UIView animateWithDuration:1.0 animations:^{
- [self.imageView setFrame:frame];
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:1.0f animations:^{
- [self.imageView setFrame:kImageInitFrame];
- }];
- }];
- }
- #pragma mark 捏合手势
- - (void)pinch:(UIPinchGestureRecognizer *)recognizer
- {
- /**
- 变化过程中,放大缩小
- 结束后,恢复
- */
- if (UIGestureRecognizerStateChanged == recognizer.state) {
- [recognizer.view setTransform:CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)];
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 恢复初始位置,清除所有的形变效果
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
- }
- #pragma mark 旋转手势,至少两根手指
- - (void)rotation:(UIRotationGestureRecognizer *)recognizer
- {
- // recognizer中的rotation属性是基于图片的初始旋转弧度的
- /**
- 变化过程中,旋转
- 结束后,恢复
- */
- if (UIGestureRecognizerStateChanged == recognizer.state) {
- [recognizer.view setTransform:CGAffineTransformMakeRotation(recognizer.rotation)];
- /**
- 累加的形变
- */
- // [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation)];
- // // 把手势识别的rotation设置为0,在下一次触发时,以当前的旋转角度为基准继续旋转
- // recognizer.rotation = 0;
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 恢复初始位置,清除所有的形变效果
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
- }
- #pragma mark 拖动手势
- - (void)pan:(UIPanGestureRecognizer *)recognizer
- {
- NSLog(@"拖动");
- /*
- 拖动手势结束后,以动画的方式回到初始位置
- */
- // 在手势状态处于“变化”时处理图片的移动
- // UIGestureRecognizerStateChanged类似于touchesMoved方法,会不断地被调用
- if (UIGestureRecognizerStateChanged == recognizer.state) {
- // translationInView,判断在父视图中平移的位置,平移的偏移量始终以视图的初始位置为基础
- CGPoint deltaPoint = [recognizer translationInView:self.view];
- // 用形变参数来改变图像的位置
- [recognizer.view setTransform:CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y)];
- // CGRect targetRect = kImageInitFrame;
- // targetRect.origin.x += deltaPoint.x;
- // targetRect.origin.y += deltaPoint.y;
- //
- // [recognizer.view setFrame:targetRect];
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- [UIView animateWithDuration:0.5f animations:^{
- // 将图片复位
- // [recognizer.view setFrame:kImageInitFrame];
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- }
- }
- #pragma mark 长按手势
- - (void)longTap:(UILongPressGestureRecognizer *)recognizer
- {
- NSLog(@"长按");
- /*
- 旋转半圈的动画,动画完成后恢复
- 长按手势属于连续手势,是需要处理状态
- 因为长按通常只有一根手指,因此在Began状态下,长按手势就已经被识别了
- 针对长按的处理,最好放在UIGestureRecognizerStateBegan状态中实现
- */
- if (UIGestureRecognizerStateBegan == recognizer.state) {
- NSLog(@"长按");
- [UIView animateWithDuration:1.0f animations:^{
- [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
- } completion:^(BOOL finished) {
- // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
- [recognizer.view setTransform:CGAffineTransformIdentity];
- }];
- } else if (UIGestureRecognizerStateEnded == recognizer.state) {
- // [UIView animateWithDuration:1.0f animations:^{
- // [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
- // } completion:^(BOOL finished) {
- // // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
- // [recognizer.view setTransform:CGAffineTransformIdentity];
- // }];
- }
- }
- #pragma mark 点按手势
- - (void)tap:(UITapGestureRecognizer *)recognizer
- {
- NSLog(@"点我了");
- // 做一个动画效果
- // 向下移出屏幕,完成后再重新返回初始位置
- // recognizer.view 就是识别到手势的视图,也就是手势绑定到得视图
- CGRect initFrame = recognizer.view.frame;
- CGRect targetFrame = recognizer.view.frame;
- targetFrame.origin.y += 360;
- [UIView animateWithDuration:1.0f animations:^{
- // 自动反向重复动画
- [UIView setAnimationRepeatAutoreverses:YES];
- // 设置动画重复次数
- [UIView setAnimationRepeatCount:2];
- [recognizer.view setFrame:targetFrame];
- } completion:^(BOOL finished) {
- [recognizer.view setFrame:initFrame];
- }];
- }