添加 UIGestureRecognizer 以从左到右向左滑动我的视图 [英] Add UIGestureRecognizer to swipe left to right right to left my views

查看:28
本文介绍了添加 UIGestureRecognizer 以从左到右向左滑动我的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这可能吗?如果是的话,有没有关于如何去做的提示或 UIGestureRecognizer 的任何好的教程?

Is this possible? if yes any hint how to do it or any good tutorials for UIGestureRecognizer?

谢谢.

推荐答案

UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

//向右滑动

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
}

试试这个.

这是上述代码的 swift 版本.

Here is the swift version of above code.

左滑

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
    }

这篇关于添加 UIGestureRecognizer 以从左到右向左滑动我的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆