標籤:
1.點選手勢:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
//設定點擊次序,識別手勢的時候使用
tapGesture.numberOfTapsRequired = 2;
2.長按手勢:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)];
//設定按壓最短期間,預設0.5s
longPress.minimumPressDuration = 1;
3.滑動手勢:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
[view addGestureRecognizer:panGesture];
4.捏合手勢:
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate = self;
5.旋轉手勢:
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];
rotationGesture.delegate = self;
#pragma mark -
#pragma mark Handle Gesture Recoginzer
- (void)handleRotationGesture:(UIRotationGestureRecognizer*)gesture{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//旋轉的弧度
CGFloat rotation = gesture.rotation;
view.transform = CGAffineTransformRotate(view.transform, rotation);
//注意有累加
gesture.rotation = 0.0;
}
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//得到縮放的比例,然後設定transform的縮放參數
//gesture.scale 捏合時候的縮放比例
CGFloat scale = gesture.scale;
//設定view.transform scale 代表縮放
//第一個參數,是基準值,在此基礎上進行縮放
//第二個參數,x 座標方向的縮放比例
//第三個參數,y 座標方向的縮放比例
view.transform = CGAffineTransformScale(view.transform, scale, scale);
//由於手勢效果會累積,所以需要給他複位
gesture.scale = 1.0;
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
//手勢識別成功,並且手勢持續進行中
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//手勢在self.view上移動的多少
CGPoint offset = [gesture translationInView:self.view];
CGPoint newCenter = CGPointMake(view.center.x+offset.x, view.center.y+offset.y);
view.center = newCenter;
//手勢的效果會累加,translation一直累加
//重新初始化,去除原來的累加效果
[gesture setTranslation:CGPointZero inView:self.view];
}
}
//點選手勢處理
- (void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
//gesture.view; 代表手勢發生在哪一個view上
NSLog(@"點擊了2次,手勢識別成功");
}
//手勢識別的過程,首先接受到touchbegin的事件,開始啟動手勢識別,手勢的狀態
//UIGestureRecognizerStatePossible,然後繼續接收並分析觸摸事件,如果手勢識別成功,手勢的狀態變為UIGestureRecognizerStateBegan
//如果手勢繼續進行,手勢的狀態變為UIGestureRecognizerStateChanged
//如果手勢結束,狀態變為UIGestureRecognizerStateEnded
//如果手勢識別失敗 UIGestureRecognizerStateFailed
- (void)handleLongPressGesture:(UILongPressGestureRecognizer*)gestrue
{
if (gestrue.state == UIGestureRecognizerStateBegan || gestrue.state == UIGestureRecognizerStateChanged) {
NSLog(@"長按手勢識別成功");
}
if (gestrue.state == UIGestureRecognizerStateEnded) {
NSLog(@"手勢識別結束");
}
}
#pragma mark -
#pragma mark UIGestureReconigerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer.view == otherGestureRecognizer.view) {
return YES;
}
return NO;
}
iOS的基本手勢