iOS中常用的六种手势

        iOS中常用的六种手势分别为点击,长按,清扫,捏合,旋转,拖拽,手势类继承于UIController父类,所以有触发事件.所有控件都可以添加手势,但是UIImageView和UILable的用户交互是默认关闭的,只有把他们的userInteractionEnable属性设置为YES才可以触发事件;


1 点击:点击手势就是点击一下,然后就会执行触发事件;

创建手势的语句为

    UITapGestureRecognizer  *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];

    [view addGestureRecognizer:tap];

    [tap release];

其中self为创建手势所在的类的虚拟对象,tap:为执行点击手势后触发的事件,下面的语句是把手势给视图view;

- (void)tap:(UITapGestureRecognizer *)tap

{

    

}

这是手势tap的触发事件;


2 长按:长按顾名思义长时间按压

创建语句为:

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(press:)];

    [view addGestureRecognizer:longPress];

    [longPress release];

触事件为:

- (void)longPress:(UILongPressGestureRecognizer *)press

{

    

}


3 清扫:清扫手势就是常用的上下左右滑动,但是一个手势只能给一个方向

创建语句:

    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipAction:)];

    // 滑动方向

    swip.direction =UISwipeGestureRecognizerDirectionRight;

    [view addGestureRecognizer:swip];

    [swip release];

触发事件为:

- (void)swipAction:(UISwipeGestureRecognizer *)swip

{  

    // 打印是由那个方法调用的什么方法

    NSLog(@"%s", __func__);

}


4 捏合:捏合即常用的放缩小

创建语句:

    UIPinchGestureRecognizer  *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];

    [view addGestureRecognizer:pinch];

    [pinch release];

触发事件:

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    NSLog(@"%s", __func__);

    // 通过tag值传值

    UIView *view = [self.viewviewWithTag:100];

    // 设定放大倍数,让它长宽等比例放大

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

    // 每次放大后还原放大倍数,

    pinch.scale = 1;

}

5 旋转:可以把视图旋转

创建语句:

    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotateAction:)];

    [view addGestureRecognizer:rotate];

    [rotate release];

触发事件:

- (void)rotateAction:(UIRotationGestureRecognizer *)rotate

{

    NSLog(@"%s", __func__);

    UIView *view = [self.viewviewWithTag:100];

    // 设定旋转角度

    view.transform =CGAffineTransformRotate(view.transform, rotate.rotation);

    // 每次旋转玩之后旋转角度清零

    rotate.rotation = 0;

    

}

6 拖拽:可以移动视图

创建语句:

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

    [view addGestureRecognizer:pan];

    [pan release];


触发事件:

- (void)panAction:(UIPanGestureRecognizer *)pan

{

    NSLog(@"%s", __func__);

    UIView *view = [self.viewviewWithTag:100];

    // 获取当前视图的偏移量

    CGPoint point = [pan translationInView:view];

    // 设置偏移量

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

    // 偏移量清零

    [pan setTranslation:CGPointMake(0, 0)inView:view];

}

以上就是常见的六种手势及其用法;



















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值