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

查看:203
本文介绍了添加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];

// SwipeRight

// 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
}

试试这个。

以下是上述代码的快速版本。

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)

方法实现......

Method implementation...

 @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天全站免登陆