iOS 手势之左右滑动

移动开发 iOS
本文作者为各位网友朋友们列出了一个iOS开发中左右滑动手势的应用例子代码,代码很实用简练,可以供各位网友参考学习之用。

今天想找些关于ios手势的资料, 结果看了几个都跑不起来, 后来东找找, 西找找终于可以弄了个跑起来的例子.......

下面例子主要做个左右滑动手势.

1. 先创建一个SingleView的项目.

2. 然后在主界面上放一个Label, 主要用于测试滑动是否起作用.

3. 给label增加一个Outlet. 取名为 "swipeLabel"

4. 在"ViewController.h"中增加两个手势property.

  1. @property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer; 
  2. @property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer; 

并synthesize到"ViewController.m"文件中.

5.在"ViewController.m"文件中的"ViewDidLoad"方法中增加如下代码:

  1. self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];  
  2.         self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];  
  3.         self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;  
  4.         self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;  
  5.         [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];  
  6.         [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];  

6.并在"ViewController.m"中增加如下方法;

  1. - (void)handleSwipes:(UISwipeGestureRecognizer *)sender 
  2.     { 
  3.         if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { 
  4.             CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y); 
  5.             self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height); 
  6.             self.swipeLabel.text = @"尼玛的, 你在往左边跑啊...."
  7.         } 
  8.         if (sender.direction == UISwipeGestureRecognizerDirectionRight) { 
  9.             CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y); 
  10.             self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height); 
  11.             self.swipeLabel.text = @"尼玛的, 你在往右边跑啊...."
  12.         } 
  13.     } 

7. 保存, 编译, 运行....

责任编辑:闫佳明 来源: oschina
相关推荐

2015-02-28 15:55:32

手势gesture过渡

2010-01-28 14:57:36

Android滑动手势

2015-02-12 13:08:42

左右滑动PagerAdapte分页

2021-05-29 20:47:00

微软Windows 10Windows

2011-06-28 09:53:43

iPhone诺基亚N9

2014-10-09 10:42:48

iOS手势识别

2013-09-23 09:54:28

2017-10-10 15:14:23

BUGiOS 11苹果

2015-07-22 10:34:59

手势密码源码

2019-04-16 09:47:42

iOS应用系统

2012-12-24 08:54:47

iOSUnity3D

2014-12-31 16:48:43

Touch touchevent多点触摸

2020-04-24 16:08:12

Android 11新版本功能

2019-05-16 12:17:21

AndroidiOS苹果

2013-07-18 18:14:26

UITableViewiOS长按手势UILongPress

2022-05-17 12:25:59

物联网智能建筑楼宇自控

2021-05-20 09:00:27

SwiftUI Swift TapGesture

2013-05-14 11:18:24

AIR AndroidSwipe手势

2015-01-22 16:04:06

iPhone

2011-03-28 14:04:10

SQL左连接右连接
点赞
收藏

51CTO技术栈公众号