iOS UIGestureRecognizer(手势、单击、拖拽 、拖拽、旋转、轻扫 、长按、边缘滑动,功能介绍)~demo

 

    联系人:石虎 QQ:1224614774  昵称: 嗡嘛呢叭咪哄

                        QQ群:807236138  群称: iOS 技术交流学习群

 

#import "ViewController.h"

 

@interface ViewController ()

{

    UITapGestureRecognizer *_tap;     //单击

    UIPanGestureRecognizer *_pan;    //拖拽

    UIPinchGestureRecognizer *_pinch;//拖拽

    UIRotationGestureRecognizer *_rotation;    //旋转

    UISwipeGestureRecognizer *_swipe;         //轻扫

    UILongPressGestureRecognizer *_longpress;//长按

    UIScreenEdgePanGestureRecognizer *_edgePan;//边缘滑动手势

}

@property (nonatomic,weak)IBOutletUIView *testView;

@end

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [selfaddTapGesture];

    [selfaddPanGesture];

    [selfaddPinchGesture];

    [selfaddRotationGesture];

    [selfaddSwipeGesture];

    [selfaddLongpressGesture];

    [selfaddEdgePanGesture];

    //手势谦让

    [selfgestureHumility];

}

 

#pragma mark - 手势

//单击

- (void)addTapGesture{

    _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    _tap.numberOfTapsRequired = 1;

    [_testView addGestureRecognizer:_tap];

}

 

//拖拽

- (void)addPanGesture{

    _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

    _pan.minimumNumberOfTouches = 1;

    [_testView addGestureRecognizer:_pan];

}

 

//拖拽

- (void)addPinchGesture{

    _pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];

    [_testView addGestureRecognizer:_pinch];

}

 

//旋转

- (void)addRotationGesture{

    _rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];

    [_testView addGestureRecognizer:_rotation];

}

 

//轻扫

- (void)addSwipeGesture{

    _swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];

    //指定扫动方向

    _swipe.direction = UISwipeGestureRecognizerDirectionDown;

    [_testView addGestureRecognizer:_swipe];

}

 

//长按

- (void)addLongpressGesture{

    _longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressAction:)];

    _longpress.minimumPressDuration = 1.0;

    [_testView addGestureRecognizer:_longpress];

}

 

//边缘滑动手势

- (void)addEdgePanGesture{

    _edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanAction:)];

    _edgePan.edges = UIRectEdgeLeft;

    [self.view addGestureRecognizer:_edgePan];

}

 

#pragma mark - 动作

//单击

- (void)tapAction:(UITapGestureRecognizer *)tap{

    NSLog(@"单击");

}

 

//拖拽

- (void)panAction:(UIPanGestureRecognizer *)pan{

    NSLog(@"拖拽");

    CGPoint point = [pantranslationInView:pan.view];

    //    pan.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);

    pan.view.transform =CGAffineTransformTranslate(pan.view.transform, point.x, point.y);

    [pan setTranslation:CGPointZeroinView:pan.view];

}

 

//捏合

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{

    NSLog(@"捏合");

    pinch.view.transform =CGAffineTransformMakeScale(pinch.scale, pinch.scale);

    

}

 

//旋转

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{

    NSLog(@"旋转");

    rotation.view.transform =CGAffineTransformMakeRotation(rotation.rotation);

}

 

//轻扫

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

    NSLog(@"向下轻扫");

}

 

//长按

- (void)longpressAction:(UILongPressGestureRecognizer *)longpress{

    NSLog(@"长按");

}

 

//边缘滑动手势

- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)edgePan{

    NSLog(@"左边缘滑动");

    UIColor *random = [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0blue:arc4random()%256/255.0alpha:1.0];

    self.view.backgroundColor = random;

}

 

#pragma mark - privatemethods//手势谦让

- (void)gestureHumility{

    [_pan requireGestureRecognizerToFail:_swipe];

}

@end

 

谢谢!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值