手势识别
一、1.点按
UITapGestureRecognizer
2.长按
UILongPressGestureRecognizer
if (UIGestureRecognizerStateBegan == recongnizer.state) {//如果自己的状态等于开始
[recongnizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];//就设置transform旋转属性为180;
}else if(UIGestureRecognizerStateEnded == recongnizer.state){
[recongnizer.view setTransform:CGAffineTransformIdentity];
}
3.拖动
UIPanGestureRecognizer
if (recongnizer.state == UIGestureRecognizerStateChanged) {
CGPoint deltaPoint = [recongnizer translationInView:self.view];//判断在父视图平移的位置
recongnizer.view.transform = CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y);
}else if(recongnizer.state == UIGestureRecognizerStateEnded){
recongnizer.view.transform = CGAffineTransformIdentity;
}
4.捏合
UIPinchGestureRecognizer
if (recongnizer.state == UIGestureRecognizerStateChanged) {
recongnizer.view.transform = CGAffineTransformMakeScale(recongnizer.scale, recongnizer.scale);
}else if(recongnizer.state == UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.5 animations:^{
recongnizer.view.transform = CGAffineTransformIdentity;
}];
}
5.清扫
UISwipeGestureRecognizer
if (recongnizer.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上");
}else if (recongnizer.direction == UISwipeGestureRecognizerDirectionDown){
NSLog(@"向下");
}else if (recongnizer.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左");
}else if (recongnizer.direction == UISwipeGestureRecognizerDirectionRight){
NSLog(@"向右");
}
6.旋转
UIRotationGesturecognizer
if (recongnizer.state == UIGestureRecognizerStateChanged) {
recongnizer.view.transform = CGAffineTransformMakeRotation(M_PI);
}
二、综上所述:发现
1>捏合,拖动,旋转,都需要判断状态改变和状态结束
2>清扫需要设置方向,并且判断方向,默认是向右清扫
3>长按需要判断状态开始,和状态结束
4>其他可按需求判断