標籤:
總共有六種手勢識別:
輕擊手勢(TapGestureRecognizer),
撥動手勢 (SwipeGestureRecognizer),
長按手勢(LongPressGestureRecognizer),
拖動手勢(PanGestureRecognizer),
捏合手勢(PinchGestureRecognizer),
旋轉手勢(RotationGestureRecognizer);
1,輕擊手勢(TapGestureRecognizer)
//建立tap手勢
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//設定點擊次數和點擊手指數
tapGesture.numberOfTapsRequired = 1;
// 點擊次數
tapGesture.numberOfTouchesRequired = 1;
// 點擊手指數
[self.view addGestureRecognizer:tapGesture]; // 添加
//輕擊手勢觸發方法
-(void)tapGesture:(id)sender {
//輕擊後要做的事情
}
2,長按手勢(LongPressGestureRecognizer)
//添加長摁手勢
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//設定長按時間
longPressGesture.minimumPressDuration = 0.5;
//(2秒)
[self.view addGestureRecognizer:longPressGesture];
//常摁手勢觸發方法
-(void)longPressGesture:(id)sender
{
UILongPressGestureRecognizer *longPress = sender;
if
(longPress.state == UIGestureRecognizerStateBegan)
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@
"提示"
message:@
"長按觸發"
delegate:nil cancelButtonTitle:@
"取 消"
otherButtonTitles: nil];
[alter show];
}
}
說明:手勢的常用狀態如下
開始:UIGestureRecognizerStateBegan
改變:UIGestureRecognizerStateChanged
結束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失敗:UIGestureRecognizerStateFailed
3,撥動手勢(SwipeGestureRecognizer)
//添加撥動手勢
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設定輕掃的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
//預設向右
[self.view addGestureRecognizer:swipeGesture];
//添加撥動手勢
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設定輕掃的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//預設向右
[self.view addGestureRecognizer:swipeGestureLeft];
//撥動手勢觸發方法
-(void)swipeGesture:(id)sender
{
UISwipeGestureRecognizer *swipe = sender;
if
(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
//向左輕掃做的事情
}
if
(swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
//向右輕掃做的事情
}
}
4,捏合手勢(PinchGestureRecognizer)
//添加捏合手勢
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
////捏合手勢觸發方法
-(void) pinchGesture:(id)sender
{
UIPinchGestureRecognizer *gesture = sender;
//手勢改變時
if
(gesture.state == UIGestureRecognizerStateChanged)
{
//捏合手勢中scale屬性記錄的縮放比例
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
}
//結束後恢複
if
(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformIdentity;
//取消一切形變
}];
}
}
5,拖動手勢(PanGestureRecognizer)
//添加拖動手勢
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
//拖動手勢
-(void) panGesture:(id)sender {
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//做你想做的事兒
}
6,旋轉手勢(RotationGestureRecognizer)
//添加旋轉手勢
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋轉手勢
-(void)rotationGesture:(id)sender {
UIRotationGestureRecognizer *gesture = sender;
if
(gesture.state==UIGestureRecognizerStateChanged)
{
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
if
(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:1 animations:^{
_imageView.transform=CGAffineTransformIdentity;
//取消形變
}];
}
} iOS中常用的手勢,可自行吸收~
iOS開發之手勢識別