添加UIGestureRecognizer从左到右向左滑动以查看我的视图

时间:2013-07-09 05:18:13

标签: ios objective-c uiviewcontroller uigesturerecognizer swipe-gesture

我有一个不同UIViewControllers的UIStoryboard,我想添加另一个UIViewController(如仪表板),当用户从左侧滑动ipad时,仪表板会出现,然后当他向后滑动时当前视图将被恢复。

这可能吗?如果是的话有什么提示怎么做或UIGestureRecognizer的任何好的教程?

谢谢。

1 个答案:

答案 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
    }