第一:触摸事件
一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象
UIEvent:称为事件对象,记录事件产生的时刻和类型
两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象;两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象;所以可以根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸
我们可以touch时间来监听view的点击
属性
-
(CGPoint)locationInView:(UIView *)view;返回值表示触摸在view上的位置这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
-
(CGPoint)previousLocationInView:(UIView *)view;该方法记录了前一个触摸点的位置
常用方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event //注 由于touches是NSSet,无须集合,因此我们可以利用下面的方法来获得UITouch对象以及相关触摸的位置坐标 UITouch *touch = [touches anyObject]; //当前触摸点 CGPoint current = [touch locationInView:self]; //上一个触摸点 CGPoint previous = [touch previousLocationInView:self];
UIView不接收触摸事件的三种情况
1,userInteractionEnabled = NO
2,hidden = YES
3,alpha = 0.0 ~ 0.01
注:UIImageView的userInteractionEnabled默认就是NO.
第二:手势识别器:UIGestureRecognizer
UIGestureRecognizer,能轻识别用户在某个view上面做的一些常见手势,但是UIGestureRecognizer是一个抽象类,使用它的子类才能处理具体的手势
//常见子类 UITapGestureRecognizer(敲击) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽) UISwipeGestureRecognizer(轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按)
手势识别器的相关状态
UIGestureRecognizerStateBegan,// 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateChanged,// 手势状态改变 UIGestureRecognizerStateEnded,// 手势完成
使用
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)]; tap.delegate = self;//不设置代理也可以监听/执行监听方法,但是默认是不能进行多手势识别,或者我们想实现代理方法的时候,就要设置代理 [self.iconView addGestureRecognizer:tap];
//手势进行缩放的注意点:
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)]; pinch.delegate = self; [self.iconView addGestureRecognizer:pinch]; - (void)pinchView:(UIPinchGestureRecognizer *)pinch { pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1; // 由于每次进行缩放的时候都是相对于初始位置,因此我们每次缩放后把缩放比设为1,也就是让下一次缩放相对于想在的位置 }
//手势进行旋转的注意点:
UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; recognizer.delegate = self; [self.iconView addGestureRecognizer:recognizer];
- (void)rotateView:(UIRotationGestureRecognizer *)rotate { rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation); recognizer.rotation = 0; //由于每次进行旋转的时候都是相对于初始位置,因此我们每次缩放后把旋转角度比设为0,也就是让下一次缩放相对于想在的位置 }
//拖拽,移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)]; [self.purpleView addGestureRecognizer:pan]; - (void)panView:(UIPanGestureRecognizer *)pan { // 1.在view上面挪动的距离 CGPoint translation = [pan translationInView:pan.view]; CGPoint center = pan.view.center; center.x += translation.x; center.y += translation.y; pan.view.center = center; // 2.清空移动的距离 [pan setTranslation:CGPointZero inView:pan.view];//同上,每次移动后我们要进行清零操作 }