UIGestureRecognizer:
1.locationinView 獲取手勢在某個檢視裡面的座標位置
2.delegate監聽手勢的行為
3.state狀態
開始:UIGestureRecognizerStateBegan 手勢達到要求
識別:UIGestureRecognizerStateRecognized 手勢識別的最後一刻
改變:UIGestureRecognizerStateChanged 手勢過程當中
結束:UIGestureRecognizerStateEnded 手勢結束
取消:UIGestureRecognizerStateCancelled
失敗:UIGestureRecognizerStateFailed
1.單擊
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
singleFingerOne.numberOfTouchesRequired = 1; 手指數
singleFingerOne.numberOfTapsRequired = 1; tap次數
2.長按
UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
longpressGesutre.minimumPressDuration=1; 最少長按時間
longpressGesutre.allowableMovement=15; 最大15畫素的運動是手勢識別所允許的
3.滑動
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft; 不設定是右 多個方向用|或
4.捏
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(PinchGesture:)];
-(void) pinchGesture:(id)sender
{
UIPinchGestureRecognizer *gesture = sender;
//手勢改變時
if
(gesture.state == UIGestureRecognizerStateChanged)
{
//捏合手勢中scale屬性記錄的縮放比例
_imageView.transform = CGAffineTransformScale(_imageView.transform,gesture.scale, gesture.scale);
}
//結束後恢復
if
(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformIdentity;
//取消一切形變
}];
}
}
-(void)rotationGesture:(id)sender
{
UIRotateGestureRecognizer *gesture = sender;
if
(gesture.state==UIGestureRecognizerStateChanged)
{
_imageView.transform=CGAffineTransform=Rotation(_imageView.transform,gesture.rotation);
}
if
(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:1 animations:^{
_imageView.transform=CGAffineTransformIdentity;
//取消形變
}];
}
}
6.多手勢
ios開發中,預設是在同一時間只能有一個手勢被執行,要實現多個手勢同時進行,須實現UIGestureRecognizerDelegate,並重寫函式
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}