只有多嘗試,多寫才能更好。
// 手勢識別器
// 1.輕拍手勢
// 手勢需要在定義時綁定一個觸發方法(SEL)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 輕拍的設置
// 需要輕拍兩次 才響應事件
tap.numberOfTapsRequired = 2;
// 需要用幾個手指去拍
tap.numberOfTouchesRequired = 2;
// 給view添加手勢
[imageView addGestureRecognizer:tap];
[tap release];
// 2.長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 長按 觸發方法 所需要的事件
longPress.minimumPressDuration = 3;
// 長按 允許用戶移動手指的距離
longPress.allowableMovement = 100;
[imageView addGestureRecognizer:longPress];
[longPress release];
// 3.輕掃手勢 (swipe)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 設置 輕掃 的方向 (一個對象只能一個方向 要不左右 要不上下)!!!
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipe];
// 4. 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
// 5.旋轉
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(roration:)];
[imageView addGestureRecognizer:rotation];
// 6.捏合手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
// 7.屏幕邊緣拖拽
UIScreenEdgePanGestureRecognizer *sceenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(sceenEdgeAction:)];
sceenEdge.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:sceenEdge];
// 將UIImageView的用戶交互打開,使他能響應輕拍
[imageView setUserInteractionEnabled:YES];
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"輕拍");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 當長按手勢開始觸發時
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按");
}
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"輕掃");
}
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"拖拽");
// 通過手勢的view屬性 獲取當前手勢添加到的 view
UIImageView *imageView = (UIImageView *) pan.view;
// 獲取到 當前手指接觸的點
CGPoint point = [pan translationInView:imageView];
// 讓view變形
imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:imageView];
}
- (void)roration:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋轉");
UIImageView *imageView = (UIImageView *) rotation.view;
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合");
UIImageView *imageView = (UIImageView *) pinch.view;
// 在x,y軸方向 放大/縮小
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
- (void)sceenEdgeAction:(UIScreenEdgePanGestureRecognizer *)sceen
{
NSLog(@"屏幕邊緣");
}