ios 滑动手势 怎么判断左右滑动
2个回答
展开全部
iOS 滑动手势要判断左右滑动,可以通过UISwipeGestureRecognizer这个类里的direction来判断。
首先,创建一个UISwipeGestureRecognizer的对象swipeGestureRecognizer.
然后写一个if语句来判断滑动的方向,如下所示:
if(swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
//在这里写右滑的操作
}else{
//在这里写左滑的操作
}
展开全部
1. 先创建一个SingleView的项目.
2. 然后在主界面上放一个Label, 主要用于测试滑动是否起作用.
3. 给label增加一个Outlet. 取名为 "swipeLabel"
4. 在"ViewController.h"中增加两个手势property.
?
1
2
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
并synthesize到"ViewController.m"文件中.
5.在"ViewController.m"文件中的"ViewDidLoad"方法中增加如下代码:
?
1
2
3
4
5
6
7
8
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
6.并在"ViewController.m"中增加如下方法;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y);
self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);
self.swipeLabel.text = @"尼玛的, 你在往左边跑啊....";
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y);
self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);
self.swipeLabel.text = @"尼玛的, 你在往右边跑啊....";
}
}
7. 保存, 编译, 运行....
2. 然后在主界面上放一个Label, 主要用于测试滑动是否起作用.
3. 给label增加一个Outlet. 取名为 "swipeLabel"
4. 在"ViewController.h"中增加两个手势property.
?
1
2
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
并synthesize到"ViewController.m"文件中.
5.在"ViewController.m"文件中的"ViewDidLoad"方法中增加如下代码:
?
1
2
3
4
5
6
7
8
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
6.并在"ViewController.m"中增加如下方法;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y);
self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);
self.swipeLabel.text = @"尼玛的, 你在往左边跑啊....";
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y);
self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);
self.swipeLabel.text = @"尼玛的, 你在往右边跑啊....";
}
}
7. 保存, 编译, 运行....
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询