gpt4 book ai didi

ios - 使用 UIPanGestureRecognizer 在 Swift 中进行左侧导航

转载 作者:行者123 更新时间:2023-11-28 06:59:15 24 4
gpt4 key购买 nike

我正在使用 Raywenderlich 教程实现没有库的导航:

http://www.raywenderlich.com/78568/create-slide-out-navigation-panel-swift

我尝试对代码进行一些修改,使其没有右侧导航,但是当我滑动时,我得到了一个黑色背景,它正在打开。

所以我认为问题可能来自 UIPanGestureRecognizer 并尝试使用 UISwipeGestureRecognizer 但我不知道该怎么做或者它是否是好方法:

enum SlideOutState{
case BothCollapsed
case LeftPanelExpanded
}

class ContainerViewController: UIViewController {

var centerNavigationController: UINavigationController!
var centerViewController: CenterViewController!
var currentState: SlideOutState = .BothCollapsed
var leftViewController: SidePanelViewController?
let centerPanelExpandedOffset: CGFloat = 60

override func viewDidLoad() {
super.viewDidLoad()

centerViewController = UIStoryboard.centerViewController()
centerViewController.delegate = self

centerNavigationController = UINavigationController(rootViewController: centerViewController)
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)

centerNavigationController.didMoveToParentViewController(self)

/*
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
*/

var swipeRight = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
}
}

// MARK: CenterViewController delegate


extension ContainerViewController : CenterViewControllerDelegate{

func toggleLeftPanel() {
let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

if notAlreadyExpanded {
addLeftPanelViewController()
}

animateLeftPanel(shouldExpand: notAlreadyExpanded)
}

func collapseSidePannels() {
switch (currentState) {
case .LeftPanelExpanded:
toggleLeftPanel()
default:
break
}
}

func addLeftPanelViewController() {
if (leftViewController == nil) {
leftViewController = UIStoryboard.leftViewController()
leftViewController!.menus = Menu.allMenu()

addChildSidePanelController(leftViewController!)
}
}

func addChildSidePanelController(sidePanelController: SidePanelViewController) {
sidePanelController.delegate = centerViewController
view.insertSubview(sidePanelController.view, atIndex: 0)

addChildViewController(sidePanelController)
sidePanelController.didMoveToParentViewController(self)
}


func animateLeftPanel(#shouldExpand: Bool) {
if (shouldExpand) {
currentState = .LeftPanelExpanded

animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerNavigationController.view.frame) - centerPanelExpandedOffset)
} else {
animateCenterPanelXPosition(targetPosition: 0) { finished in
self.currentState = .BothCollapsed

self.leftViewController!.view.removeFromSuperview()
self.leftViewController = nil;
}
}
}

func animateCenterPanelXPosition(#targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
self.centerNavigationController.view.frame.origin.x = targetPosition
}, completion: completion)
}

}

/*
extension ContainerViewController: UIGestureRecognizerDelegate {
// MARK: Gesture recognizer

func handlePanGesture(recognizer: UIPanGestureRecognizer) {
let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)

switch(recognizer.state) {
case .Began:
if (currentState == .BothCollapsed) {
if (gestureIsDraggingFromLeftToRight) {
addLeftPanelViewController()
}
}
case .Changed:
recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
case .Ended:
if (leftViewController != nil) {
// animate the side panel open or closed based on whether the view has moved more or less than halfway
let hasMovedGreaterThanHalfway = recognizer.view!.center.x > view.bounds.size.width
animateLeftPanel(shouldExpand: hasMovedGreaterThanHalfway)
}
default:
break
}
}
}
*/

extension ContainerViewController: UIGestureRecognizerDelegate {
func handleSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
switch (gesture.state){
case UISwipeGestureRecognizerDirection.Right:
addLeftPanelViewController()
default:
break
}
}
}
}

private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }

class func leftViewController() -> SidePanelViewController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("LeftViewController") as? SidePanelViewController
}

class func centerViewController() -> CenterViewController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? CenterViewController
}

}

最佳答案

只需更改 .Changed 大小写:

if (((recognizer.view!.center.x + recognizer.translationInView(view).x) > view.center.x || gestureIsDraggingFromLeftToRight) && (recognizer.view!.center.x >= view.center.x && recognizer.velocityInView(view).x > 0 || recognizer.view!.center.x > view.center.x && recognizer.velocityInView(view).x < 0) && recognizer.view!.center.x + recognizer.translationInView(view).x > view.center.x) {
recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
recognizer.setTranslation(CGPointZero, inView: view)
}

关于ios - 使用 UIPanGestureRecognizer 在 Swift 中进行左侧导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32473643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com