手勢識別器

UITapGestureRecognizer是輕拍⼿手勢識別器,能識別輕拍操做函數

UILongPressGestureRecognizer是⻓長按⼿手勢識別器,能識別⻓長按操做。代理

UIRotationGestureRecognizer是旋轉⼿手勢識別器,能識別旋轉操做。orm

UIPinchGestureRecognizer是捏合⼿手勢識別器,能識別捏合操做。blog

UIPanGestureRecognizer是平移⼿手勢識別器,能識別拖拽操做。ip

UISwipeGestureRecognizer是輕掃⼿手勢識別器,能識別拖拽操做。get

UIScreenEdgePanGestureRecognizer是屏幕邊緣輕掃識別器,是iOS7中新增的⼿手勢。 同步

以輕拍、旋轉、捏合爲例it

#import "RootViewController.h"

@interface RootViewController () <UIGestureRecognizerDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 建立一個輕拍手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTap:)];
    [imageView addGestureRecognizer:tap];
    [tap release];
    
    // 建立一個旋轉手勢
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRotation:)];
    rotation.delegate = self;
    [imageView addGestureRecognizer:rotation];
    [rotation release];

    // 建立一個捏合手勢
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)];
    pinch.delegate = self;
    [imageView addGestureRecognizer:pinch];
    [pinch release];
}    

- (void)doTap:(UITapGestureRecognizer *)tap
{
    // 經過tap.view能夠獲取當前手勢放置在哪個view上
    NSLog(@"被點擊了,(⊙o⊙)哦,NO %@", tap.view);
}

- (void)doRotation:(UIRotationGestureRecognizer *)rotationGesture
{
    NSLog(@"轉啦,啊哈哈");
    // 在作旋轉的時候對應的有兩個函數,一個是CGAffineTransformRotate,還有一個是CGAffineTransformMakeRotation。這兩個函數的區別就在於第一個函數能夠在上次的基礎上繼續旋轉,第二個函數只能作一次旋轉
    // 1.在上次的基礎上作旋轉
    rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
    // 2.將手勢識別器的旋轉角度歸零,下次讓其在新的旋轉角度上進行旋轉,而不是累加。
    rotationGesture.rotation = 0;
}

- (void)doPinch:(UIPinchGestureRecognizer *)pinchGesture
{
    pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
    // 設置比例爲1,能夠讓大小的縮放和手指縮放同步
    pinchGesture.scale = 1;
}

// 想要在旋轉的過程當中也能捏合,須要實現代理中得這個方法,返回YES就能夠
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
相關文章
相關標籤/搜索