如何使用 SWRevealViewController 禁用侧边栏菜单中的交互
How to Disable Interaction in Sidebar Menu using SWRevealViewController
我遵循了 AppCoda.com 的边栏菜单教程,但遇到了问题。当用户在菜单中时,我想禁用用户交互。现在,尽管在菜单中,用户仍然可以与主屏幕进行交互。
Link 问题截图:
http://i.imgur.com/1gld2bY.gifv
如果您使用 SWRevealViewController 作为侧边栏。使用下面的代码
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
let tagId = 4207868622
if revealController.frontViewPosition == FrontViewPosition.Right {
let lock = self.view.viewWithTag(tagId)
UIView.animateWithDuration(0.25, animations: {
lock?.alpha = 0
}, completion: {(finished: Bool) in
lock?.removeFromSuperview()
}
)
lock?.removeFromSuperview()
} else if revealController.frontViewPosition == FrontViewPosition.Left {
let lock = UIView(frame: self.view.bounds)
lock.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
lock.tag = tagId
lock.alpha = 0
lock.backgroundColor = UIColor.blackColor()
lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: "revealToggle:"))
self.view.addSubview(lock)
UIView.animateWithDuration(0.75, animations: {
lock.alpha = 0.333
}
)
}
}
注意: 不要忘记在视图控制器中添加self.revealViewController().delegate = self
,否则不会调用委托
将此代码放入您的 TableViewController,即菜单项控制器。本质上这是位于主视图后面的视图控制器。
这也会使您的主视图变暗,如果您不想这样,请将 alpha 分量设置为 0。
let darkView = UIView()
override func viewWillAppear(_ animated: Bool) {
darkView.addGestureRecognizer(revealViewController().tapGestureRecognizer())
darkView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
darkView.frame = self.revealViewController().frontViewController.view.bounds
self.revealViewController().frontViewController.view.addSubview(darkView)
}
override func viewWillDisappear(_ animated: Bool) {
darkView.removeFromSuperview()
}
我遵循了 AppCoda.com 的边栏菜单教程,但遇到了问题。当用户在菜单中时,我想禁用用户交互。现在,尽管在菜单中,用户仍然可以与主屏幕进行交互。
Link 问题截图: http://i.imgur.com/1gld2bY.gifv
如果您使用 SWRevealViewController 作为侧边栏。使用下面的代码
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
let tagId = 4207868622
if revealController.frontViewPosition == FrontViewPosition.Right {
let lock = self.view.viewWithTag(tagId)
UIView.animateWithDuration(0.25, animations: {
lock?.alpha = 0
}, completion: {(finished: Bool) in
lock?.removeFromSuperview()
}
)
lock?.removeFromSuperview()
} else if revealController.frontViewPosition == FrontViewPosition.Left {
let lock = UIView(frame: self.view.bounds)
lock.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
lock.tag = tagId
lock.alpha = 0
lock.backgroundColor = UIColor.blackColor()
lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: "revealToggle:"))
self.view.addSubview(lock)
UIView.animateWithDuration(0.75, animations: {
lock.alpha = 0.333
}
)
}
}
注意: 不要忘记在视图控制器中添加self.revealViewController().delegate = self
,否则不会调用委托
将此代码放入您的 TableViewController,即菜单项控制器。本质上这是位于主视图后面的视图控制器。
这也会使您的主视图变暗,如果您不想这样,请将 alpha 分量设置为 0。
let darkView = UIView()
override func viewWillAppear(_ animated: Bool) {
darkView.addGestureRecognizer(revealViewController().tapGestureRecognizer())
darkView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
darkView.frame = self.revealViewController().frontViewController.view.bounds
self.revealViewController().frontViewController.view.addSubview(darkView)
}
override func viewWillDisappear(_ animated: Bool) {
darkView.removeFromSuperview()
}