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;
}