美文网首页
iOS开发--手势的上下左右滑动

iOS开发--手势的上下左右滑动

作者: 小緈福 | 来源:发表于2018-12-17 11:29 被阅读0次

    手势的应用代码示例

    如何为view添加手势以及对手势做出响应呢

    为view添加手势首先定义一个手势对象

    UISwipeGestureRecognizer * recognizer;

    2.给view添加上四种手势的代码如下:

    添加右滑手势:

    recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];    [self.view addGestureRecognizer:recognizer];

    添加左滑手势:

    recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];  [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];  [self.view addGestureRecognizer:recognizer];

    添加上滑手势:

    recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];    [self.view addGestureRecognizer:recognizer];

    添加下滑手势:

    recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];    [self.view addGestureRecognizer:recognizer];

    添加手势的响应事件:

    - (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer{if(recognizer.direction ==UISwipeGestureRecognizerDirectionDown) {NSLog(@"swipe down");    }if(recognizer.direction ==UISwipeGestureRecognizerDirectionUp) {NSLog(@"swipe up");    }if(recognizer.direction ==UISwipeGestureRecognizerDirectionLeft) {NSLog(@"swipe left");    }if(recognizer.direction ==UISwipeGestureRecognizerDirectionRight) {NSLog(@"swipe right");    }}

    # 作者邮件:zhangxmsy@163.com, 有问题联系。

    相关文章

      网友评论

          本文标题:iOS开发--手势的上下左右滑动

          本文链接:https://www.haomeiwen.com/subject/ziyqkqtx.html