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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值