UINavigationController-与桌面-Cocoa 类似吗?

发布于 2024-10-15 18:06:41 字数 118 浏览 3 评论 0原文

我目前正在开发一个应用程序,其用户界面非常类似于 Mac 版 Twitter(像 iOS 上一样推入/推出视图)。

有人为桌面 Cocoa 实现了 UIViewController 吗?这会节省我很多时间。

I'm currently developing an application with an user interface much like Twitter for Mac (Pushing in/out of views like on iOS).

Has anyone implemented a UIViewController for desktop Cocoa? This would save me many hours of work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-10-22 18:06:41

目前标准 AppKit 中还没有这个。你必须自己写。

如果您决定走这条路,这可能会有所帮助: http://parsekit.com/umekit/

UMEKit是 Cocoa 的一个小框架,它实现了 UIKit 类和 UI 组件的一些等效项。

There isn't one in standard AppKit at this time. You'll have to write your own.

This may help if you decide to go down that path: http://parsekit.com/umekit/

UMEKit is a little framework for Cocoa that implements some equivalents to UIKit classes and UI components.

半衬遮猫 2024-10-22 18:06:41

我花了很长时间寻找这个并开始编写自己的,然后找到 https://github.com/bfolder/BFNavigationController< /a>

由于某种原因 Google 并不知道这一点。

I spent ages looking for this and started to write my own, then found https://github.com/bfolder/BFNavigationController

For some reason Google doesn't know about it.

夏见 2024-10-22 18:06:41

这是我们如何在没有导航栏的情况下实现 NavigationController 的。使用导航项之间的转换来实现导航栏,类似于下面示例中视图之间的转换方式。

import AppKit

public class NavigationController: NSViewController {

   public private (set) var viewControllers: [NSViewController] = []

   open override func loadView() {
      view = NSView()
      view.wantsLayer = true
   }

   public init(rootViewController: NSViewController) {
      super.init(nibName: nil, bundle: nil)
      pushViewController(rootViewController, animated: false)
   }

   public required init?(coder: NSCoder) {
      fatalError()
   }
}

extension NavigationController {

   public var topViewController: NSViewController? {
      return viewControllers.last
   }

   public func pushViewControllerAnimated(_ viewController: NSViewController) {
      pushViewController(viewController, animated: true)
   }

   public func pushViewController(_ viewController: NSViewController, animated: Bool) {
      viewController.navigationController = self
      viewController.view.wantsLayer = true
      if animated, let oldVC = topViewController {
         embedChildViewController(viewController)
         let endFrame = oldVC.view.frame
         let startFrame = endFrame.offsetBy(dx: endFrame.width, dy: 0)
         viewController.view.frame = startFrame
         viewController.view.alphaValue = 0.85
         viewControllers.append(viewController)
         NSAnimationContext.runAnimationGroup({ context in
            context.duration = 0.2
            context.allowsImplicitAnimation = true
            context.timingFunction = .easeOut
            viewController.view.animator().frame = endFrame
            viewController.view.animator().alphaValue = 1
            oldVC.view.animator().alphaValue = 0.25
         }) {
            oldVC.view.alphaValue = 1
            oldVC.view.removeFromSuperview()
         }
      } else {
         embedChildViewController(viewController)
         viewControllers.append(viewController)
      }
   }

   @discardableResult
   public func popViewControllerAnimated() -> NSViewController? {
      return popViewController(animated: true)
   }

   @discardableResult
   public func popViewController(animated: Bool) -> NSViewController? {
      guard let oldVC = viewControllers.popLast() else {
         return nil
      }

      if animated, let newVC = topViewController {
         let endFrame = oldVC.view.frame.offsetBy(dx: oldVC.view.frame.width, dy: 0)
         view.addSubview(newVC.view, positioned: .below, relativeTo: oldVC.view)
         NSAnimationContext.runAnimationGroup({ context in
            context.duration = 0.23
            context.allowsImplicitAnimation = true
            context.timingFunction = .easeIn
            oldVC.view.animator().frame = endFrame
            oldVC.view.animator().alphaValue = 0.85
         }) {
            self.unembedChildViewController(oldVC)
         }
      } else {
         unembedChildViewController(oldVC)
      }
      return oldVC
   }

}

可重复使用的扩展:

extension NSViewController {

   private struct OBJCAssociationKey {
      static var navigationController = "com.mc.navigationController"
   }

   public var navigationController: NavigationController? {
      get {
         return ObjCAssociation.value(from: self, forKey: &OBJCAssociationKey.navigationController)
      } set {
         ObjCAssociation.setAssign(value: newValue, to: self, forKey: &OBJCAssociationKey.navigationController)
      }
   }
}

extension NSViewController {

   public func embedChildViewController(_ vc: NSViewController, container: NSView? = nil) {
      addChildViewController(vc)
      vc.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
      vc.view.autoresizingMask = [.height, .width]
      (container ?? view).addSubview(vc.view)
   }

   public func unembedChildViewController(_ vc: NSViewController) {
      vc.view.removeFromSuperview()
      vc.removeFromParentViewController()
   }
}

struct ObjCAssociation {

   static func value<T>(from object: AnyObject, forKey key: UnsafeRawPointer) -> T? {
      return objc_getAssociatedObject(object, key) as? T
   }

   static func setAssign<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_ASSIGN)
   }

   static func setRetainNonAtomic<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
   }

   static func setCopyNonAtomic<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_COPY_NONATOMIC)
   }

   static func setRetain<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN)
   }

   static func setCopy<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_COPY)
   }
}

Here how we did NavigationController, without navigation bar. Implementing navigation bar with transitions between navigation items similar to how transition between views made in example below.

import AppKit

public class NavigationController: NSViewController {

   public private (set) var viewControllers: [NSViewController] = []

   open override func loadView() {
      view = NSView()
      view.wantsLayer = true
   }

   public init(rootViewController: NSViewController) {
      super.init(nibName: nil, bundle: nil)
      pushViewController(rootViewController, animated: false)
   }

   public required init?(coder: NSCoder) {
      fatalError()
   }
}

extension NavigationController {

   public var topViewController: NSViewController? {
      return viewControllers.last
   }

   public func pushViewControllerAnimated(_ viewController: NSViewController) {
      pushViewController(viewController, animated: true)
   }

   public func pushViewController(_ viewController: NSViewController, animated: Bool) {
      viewController.navigationController = self
      viewController.view.wantsLayer = true
      if animated, let oldVC = topViewController {
         embedChildViewController(viewController)
         let endFrame = oldVC.view.frame
         let startFrame = endFrame.offsetBy(dx: endFrame.width, dy: 0)
         viewController.view.frame = startFrame
         viewController.view.alphaValue = 0.85
         viewControllers.append(viewController)
         NSAnimationContext.runAnimationGroup({ context in
            context.duration = 0.2
            context.allowsImplicitAnimation = true
            context.timingFunction = .easeOut
            viewController.view.animator().frame = endFrame
            viewController.view.animator().alphaValue = 1
            oldVC.view.animator().alphaValue = 0.25
         }) {
            oldVC.view.alphaValue = 1
            oldVC.view.removeFromSuperview()
         }
      } else {
         embedChildViewController(viewController)
         viewControllers.append(viewController)
      }
   }

   @discardableResult
   public func popViewControllerAnimated() -> NSViewController? {
      return popViewController(animated: true)
   }

   @discardableResult
   public func popViewController(animated: Bool) -> NSViewController? {
      guard let oldVC = viewControllers.popLast() else {
         return nil
      }

      if animated, let newVC = topViewController {
         let endFrame = oldVC.view.frame.offsetBy(dx: oldVC.view.frame.width, dy: 0)
         view.addSubview(newVC.view, positioned: .below, relativeTo: oldVC.view)
         NSAnimationContext.runAnimationGroup({ context in
            context.duration = 0.23
            context.allowsImplicitAnimation = true
            context.timingFunction = .easeIn
            oldVC.view.animator().frame = endFrame
            oldVC.view.animator().alphaValue = 0.85
         }) {
            self.unembedChildViewController(oldVC)
         }
      } else {
         unembedChildViewController(oldVC)
      }
      return oldVC
   }

}

Reusable extensions:

extension NSViewController {

   private struct OBJCAssociationKey {
      static var navigationController = "com.mc.navigationController"
   }

   public var navigationController: NavigationController? {
      get {
         return ObjCAssociation.value(from: self, forKey: &OBJCAssociationKey.navigationController)
      } set {
         ObjCAssociation.setAssign(value: newValue, to: self, forKey: &OBJCAssociationKey.navigationController)
      }
   }
}

extension NSViewController {

   public func embedChildViewController(_ vc: NSViewController, container: NSView? = nil) {
      addChildViewController(vc)
      vc.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
      vc.view.autoresizingMask = [.height, .width]
      (container ?? view).addSubview(vc.view)
   }

   public func unembedChildViewController(_ vc: NSViewController) {
      vc.view.removeFromSuperview()
      vc.removeFromParentViewController()
   }
}

struct ObjCAssociation {

   static func value<T>(from object: AnyObject, forKey key: UnsafeRawPointer) -> T? {
      return objc_getAssociatedObject(object, key) as? T
   }

   static func setAssign<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_ASSIGN)
   }

   static func setRetainNonAtomic<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
   }

   static func setCopyNonAtomic<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_COPY_NONATOMIC)
   }

   static func setRetain<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN)
   }

   static func setCopy<T>(value: T?, to object: Any, forKey key: UnsafeRawPointer) {
      objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_COPY)
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文