ios 添加 UIGestureRecognizer 以从左到右向左滑动我的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17540481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add UIGestureRecognizer to swipe left to right right to left my views
提问by baste
I have a UIStoryboard with different UIViewControllers
, I would like to add another UIViewController
(like a dashboard) that when the user swipe the ipad from left the dashboard will appear then when he swipe back the current view will be restored.
我有一个不同的 UIStoryboard UIViewControllers
,我想添加另一个UIViewController
(如仪表板),当用户从左侧滑动 ipad 时,仪表板将出现,然后当他向后滑动时,将恢复当前视图。
Is this possible? if yes any hint how to do it or any good tutorials for UIGestureRecognizer
?
这可能吗?如果是的话,有什么提示怎么做或有什么好的教程UIGestureRecognizer
吗?
thank you.
谢谢你。
回答by Jitendra
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
// SwipeRight
//向右滑动
UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
// Implement Gesture Methods
// 实现手势方法
-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
}
-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
}
Try this one.
试试这个。
Here is the swift version of above code.
这是上述代码的快速版本。
Left Swipe
向左滑动
var swipeleft = UISwipeGestureRecognizer(target: self, action: Selector("swipeleft:"))
swipeleft.direction = .left
view.addGestureRecognizer(swipeleft)
Right Swipe
向右滑动
var swiperight = UISwipeGestureRecognizer(target: self, action: Selector("swiperight:"))
swiperight.direction = .right
view.addGestureRecognizer(swiperight)
Method implementation...
方法实现...
@objc func swiperight(sender: UITapGestureRecognizer? = nil) {
// Do what u want here
}
@objc func swipeleft(sender: UITapGestureRecognizer? = nil) {
// Do what u want here
}