手势一共有七种,分别是轻拍,轻扫,旋转,拖拽,捏合,长按,以及平移。
设置轻拍次数:
添加轻扫手势:
添加旋转手势:
添加平移手势:
添加长按手势:
每个手势添加的时候都有其回调方法;
UIImageView *headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 02, 100, 100)];
[headImageView setCenter:self.view.center];
UIImage *headImage = [UIImage imageNamed:@"Icon.png"];
headImageView.image = headImage;
[headImageView setUserInteractionEnabled:YES];
首先初始化一个view来测试手势。
添加轻拍手势:
UITapGestureRecognizer
/**
轻拍手势
- parameter hiddenKeyBoard: 回调方法
*/
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenKeyBoard:)];
设置轻拍次数:
tapGesture.numberOfTouchesRequired = 2;
添加轻扫手势:
UISwipeGestureRecognizer
UISwipeGestureRecognizer *swipeTouche = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//扫动方向
[swipeTouche setDirection:(UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown)];
添加旋转手势:
UIRotationGestureRecognizer
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
rotation.delegate = self;
[headImageView addGestureRecognizer:rotation];
添加缩放手势:
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
pinch.delegate = self;
[headImageView addGestureRecognizer:pinch];
添加平移手势:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
pan.delegate = self;
[headImageView addGestureRecognizer:pan];
添加长按手势:
UILongPressGestureRecognizer *longPan = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPanAction:)];
longPan.delegate = self;
CFTimeInterval minTime = 1.00;
longPan.minimumPressDuration = minTime;
[headImageView addGestureRecognizer:longPan];
每个手势添加的时候都有其回调方法;
手势的代理:
<UIGestureRecognizerDelegate>
在代理方法里可以实现多手势的识别
#pragma mark - 代理方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}