iOS UIGestureRecognizer手勢總結

UIGestureRecognizer這個類 是一個抽象類 自己本身不實現什麼具體功能
具體功能都是由其子類去完成的 下面就通過對圖片的一下操作介紹一下 UIGestureRecognizer 這個類的常用方法 可以自己建一個demo 把代碼粘貼過去 看一下效果

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 300)]];
    UIImage *image = [UIImage imageNamed:@"南京夫子廟.jpg"];
    // UIImageView 打開交互
    imageView.userInteractionEnabled = YES;
    imageView.image = image;
    [self.view addSubview:imageView];

  // 先添加手勢  在觸發方法中選擇操作 可在自己工程中粘貼代碼 觀看效果
    // 輕拍手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
    // 用幾根手指 輕拍
    tap.numberOfTouchesRequired = 2;
    // 輕拍次數
    tap.numberOfTapsRequired = 1;
    // 添加手勢;
    [imageView addGestureRecognizer:tap];

    // 長按手勢
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)];
    // 最小長按的時間
    longPress.minimumPressDuration = 1;
    // 添加
    [imageView addGestureRecognizer:longPress];

    // 旋轉手勢
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(actionRotation:)];
    [imageView addGestureRecognizer:rotation];

    // 捏合手勢
    UIPinchGestureRecognizer *pinch =  [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(actionPinch:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];


    // 平移手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)];
    [imageView addGestureRecognizer:pan];

    // 輕掃手勢
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
    // 設置左右掃
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipe];

    // 邊緣掃
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreenEdgePan:)];
    // 設置從屏幕邊緣左邊掃
    screenEdgePan.edges = UIRectEdgeLeft;
    [imageView addGestureRecognizer:screenEdgePan];

}

#pragma mark - 觸發方法
// 輕拍手勢觸發的方法
- (void)actionTap:(UITapGestureRecognizer *)tap {
    NSLog(@"輕拍");
}

// 相當於控制狀態
int number = 0;
// 長按手勢的方法
- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress {
// 長按換張圖片
// 當按下按鈕時觸發 當鬆開的時候還觸發這個方法 所以要對他的狀態進行判斷
       if (number == 0 && longPress.state == UIGestureRecognizerStateBegan) {
        UIImage *image = [UIImage imageNamed:@"北京北大1.jpg"];
        UIImageView *imageView = (UIImageView *)longPress.view;
        imageView.image = image;
        number = 1;
    } else if (number == 1 && longPress.state == UIGestureRecognizerStateBegan){
        UIImage *image = [UIImage imageNamed:@"南京夫子廟.jpg"];
        UIImageView *imageView = (UIImageView *)longPress.view;
        imageView.image = image;
        number = 0;
    }
}

 // 旋轉的方法
- (void)actionRotation:(UIRotationGestureRecognizer *)rotation {
    // transform 形變屬性
    // 描述一下這個方法法
    // 第一個方法 傳入要創建的那個視圖的形變屬性
    // 第二個參數 傳入 手勢的弧度
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); 
    // 把弧度重置
    rotation.rotation = 0;
}

// 捏合手勢
- (void)actionPinch:(UIPinchGestureRecognizer *)pinch {
    // 形變屬性控制捏合
    // 第二個參數 按捏的刻度比例 形變
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
   // 重置比例
    pinch.scale = 1;
}

// 平移觸發
- (void)actionPan:(UIPanGestureRecognizer *)pan {
    // 這個點 以手指觸摸到屏幕那一刻 即爲(0.0)點
    CGPoint p = [pan translationInView:pan.view];
     NSLog(@"%@", NSStringFromCGPoint(p));
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
    // 重置點 讓他認爲每次都是剛開始觸發
    [pan setTranslation:CGPointMake(0,0) inView:pan.view];
}

// 左右掃觸發
- (void)actionSwipe:(UISwipeGestureRecognizer *)swip {
    NSLog(@"左掃了");
}

// 邊緣掃觸發  邊緣掃不加也有
- (void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
    // 剛一掃就觸發 判斷狀態
    if (screenEdgePan.state == UIGestureRecognizerStateBegan) {
         NSLog(@"掃一掃");
    }
}

@end


----------
// 下面是上面用到的自己寫的一個UIView工具類目 方便實現對視圖佈局重點內容的操作

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface UIView (WLFrame)

//  高度
@property (nonatomic,assign) CGFloat height;
//  寬度
@property (nonatomic,assign) CGFloat width;

//  Y
@property (nonatomic,assign) CGFloat top;
//  X
@property (nonatomic,assign) CGFloat left;

//  Y + Height
@property (nonatomic,assign) CGFloat bottom;
//  X + width
@property (nonatomic,assign) CGFloat right;

@end

#import "UIView+WLFrame.h"

@implementation UIView (WLFrame)

//  返回高度
- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)new height {
    CGRect newframe = self.frame;
    newframe.size.height = newheight;
    self.frame = newframe;
}

//  返回寬度
- (CGFloat)width {
    return self.frame.size.width;
}

//  設置寬度
- (void)setWidth:(CGFloat)new width {
    CGRect newframe = self.frame;
    newframe.size.width = newwidth;
    self.frame = newframe;
}

//  返回Y
- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)newton {
    CGRect newframe = self.frame;
    newframe.origin.y = newtop;
    self.frame = newframe;
}

//  返回X
- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat) new left {
    CGRect newframe = self.frame;
    newframe.origin.x = newleft;
    self.frame = newframe;
}

//  返回Y + Height
- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)new bottom {
    CGRect newframe = self.frame;
    newframe.origin.y = newbottom - self.frame.size.height;
    self.frame = newframe;
}

//  返回X + width
- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)new right {
    CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;
}


@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章