我有一个不同UIViewControllers
的UIStoryboard,我想添加另一个UIViewController
(如仪表板),当用户从左侧滑动ipad时,仪表板会出现,然后当他向后滑动时当前视图将被恢复。
这可能吗?如果是的话有什么提示怎么做或UIGestureRecognizer
的任何好的教程?
答案 0 :(得分:125)
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];
//实施手势方法
-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
}
-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
}
试试这个。
以上是上述代码的快速版本。
左键滑动
var swipeleft = UISwipeGestureRecognizer(target: self, action: Selector("swipeleft:"))
swipeleft.direction = .left
view.addGestureRecognizer(swipeleft)
右键滑动
var swiperight = UISwipeGestureRecognizer(target: self, action: Selector("swiperight:"))
swiperight.direction = .right
view.addGestureRecognizer(swiperight)
方法实施......
@objc func swiperight(sender: UITapGestureRecognizer? = nil) {
// Do what u want here
}
@objc func swipeleft(sender: UITapGestureRecognizer? = nil) {
// Do what u want here
}