如何阻止MapView上的任何交互?

时间:2016-02-29 10:01:27

标签: ios swift viewcontroller

由于tutorial,我正在设计幻灯片导航面板。

一切正常。现在我想进一步研究一些UX问题。 这就是我的应用程序现在的样子。但我仍然可以操纵MapView。我的目标是做两件事:

1)防止与MapView的所有交互,例如缩放和移动

2)关闭导航面板,同时点击带有地图的viewController。

enter image description here

控制这两个控制器的主文件是containerViewController。以下是它的源代码:

    import UIKit
import QuartzCore

enum SlideOutState {
    case BothCollapsed
    case LeftPanelExpanded
}

class ContainerViewController: UIViewController {

    var centerNavigationController: UINavigationController!
    var centerViewController: ViewController!

    var currentState: SlideOutState = .BothCollapsed {
        didSet {
            let shouldShowShadow = currentState != .BothCollapsed
            showShadowForCenterViewController(shouldShowShadow)
        }
    }

    var leftViewController: SidePanelViewController?

  override func viewDidLoad() {
    super.viewDidLoad()

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

    // wrap the centerViewController in a navigation controller, so we can push views to it
    // and display bar button items in the navigation bar
    centerNavigationController = UINavigationController(rootViewController: centerViewController)
    view.addSubview(centerNavigationController.view)
    addChildViewController(centerNavigationController)

    centerNavigationController.didMoveToParentViewController(self)

  }

}

let centerPanelExpandedOffset: CGFloat = 60

extension ContainerViewController: CenterViewControllerDelegate {

    func toggleLeftPanel() {

        let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

        if notAlreadyExpanded {
            addLeftPanelViewController()
        }

        animateLeftPanel(shouldExpand: notAlreadyExpanded)

    }



    func addLeftPanelViewController() {

        if (leftViewController == nil) {
            leftViewController = UIStoryboard.leftViewController()
            leftViewController!.categories = Category.allCats()

            addChildSidePanelController(leftViewController!)
        }

    }

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

        addChildViewController(sidePanelController)
        sidePanelController.didMoveToParentViewController(self)
    }


    func animateLeftPanel(shouldExpand 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 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)
    }

    func showShadowForCenterViewController(shouldShowShadow: Bool) {
        if (shouldShowShadow) {
            centerNavigationController.view.layer.shadowOpacity = 0.8
        } else {
            centerNavigationController.view.layer.shadowOpacity = 0.0
        }
    }

}


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() -> ViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? ViewController
  }

}

我的想法是创建代表活动侧面板的变量并编写一个函数,这将解决我的问题:

我看到类似的东西:

func blockCenterView() {

    let AlreadyExpanded = (currentState = .LeftPanelExpanded)

    if AlreadyExpanded {
        //no idea what to write here
    }
}

我是对的,还是存在解决该问题的其他变体?

2 个答案:

答案 0 :(得分:0)

添加此代码,以便停止与mapView的交互

mapView.userInteractionEnabled = false

要启用它,

mapView.userInteractionEnabled = true
  

注意:mapView是MKMapView的一个实例

<强> PS

<强> SWRevealViewController

来源:Github

Step by Step教程:More Flexible Slide View

这是我能够使用这个很棒的框架创建的

enter image description here

App Link:AirMate on AppStore

答案 1 :(得分:0)

尝试将以下属性设置为false

mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteraction = false

同时检查Interface Builder以获取用户交互:enter image description here