AppKit 概览

本文概述了AppKit,它是构建和管理macOS图形用户界面的关键框架。内容涵盖AppKit的基本概念,包括保护用户隐私、将应用移植到Apple Silicon,以及详细讨论App结构、用户交互、图形绘制、文本显示等多个方面,旨在帮助开发者全面理解并有效地利用AppKit进行Mac应用开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AppKit 概览

文章目录


转载自:https://developer.apple.com/documentation/appkit


AppKit 简介

AppKit 用于构建和管理图形化的、事件驱动的 macOS 用户界面。

AppKit 包含 实现 macOS app 用户界面的所有对象,如 窗口,面板,按钮,目录,滚动条,输入框;它管理了所有展示在屏幕上的细节,与硬件设备和屏幕缓冲区通信,在绘制之前清理屏幕,并剪辑视图。

相关主题

一、Essentials

1、Protecting the User’s Privacy 保护用户隐私

https://developer.apple.com/documentation/uikit/protecting_the_user_s_privacy

加密用户数据,尊重用户 使用数据的偏好。


2、Porting Your macOS Apps to Apple Silicon将 macOS 应用移植到 Apple Silicon 芯片。

https://developer.apple.com/documentation/xcode/porting_your_macos_apps_to_apple_silicon
创建macOS应用,使其既可以在基于 Apple silicon 芯片的Mac 上运行,也可以在基于 Intel 的Mac 上运行。


二、 App 架构(App Structure)

1、 App and Environment

https://developer.apple.com/documentation/appkit/app_and_environment
了解和系统交互的对象。


相关类、结构体和协议

Life Cycle 生命周期

Environment 环境

Handoff

App Services

Services Functions


App Help

Errors

AppKit Errors

AppKit 中的错误常量,由 NSAttributedString 生成。


2、文档、数据和剪贴板 Documents, Data, and Pasteboard

https://developer.apple.com/documentation/appkit/documents_data_and_pasteboard

组织应用的数据和偏好设置,和 iCloud 中的剪切板共享数据。

Documents

Developing a Document-Based App Write an app that creates, manages, edits, and saves text documents.

User Preferences
Pasteboard
File Promises

支持基于 UTI 的拖放功能,包括drag flocking。若有可能,他们符合剪贴板 和 文件协调。

Object Editing

3、Cocoa 绑定 Cocoa Bindings

https://developer.apple.com/documentation/appkit/cocoa_bindings

使用 Cocoa Bindings 自动同步 数据模型和用户界面。


Core Controllers

Tree-Based Data

Array-Based Data

Key-Value Data

Data Placeholders

4、资源管理 Resource Management

https://developer.apple.com/documentation/appkit/resource_management

管理 storyboards 和 nib 文件,了解如何加载 资源文件夹中 存储的资源。


Storyboard 故事版
  • class NSStoryboard An encapsulation of the design-time view controller and window controller graph represented in an Interface Builder storyboard resource file.
  • class NSStoryboardSegue A transition or containment relationship between two scenes in a storyboard.
  • class NSSeguePerforming A set of methods that support the mediation of a custom segue.

Assets 资源

Nib Files
  • class NSNib An object wrapper, or container, for an Interface Builder nib file.

5、App 扩展 App Extensions

https://developer.apple.com/documentation/appkit/app_extensions

将你的app 的基本功能拓展到系统的其他部分。


Extension Support

Quick Actions

Quick Actions allow your app extension to appear in the Finder Preview pane, Quick Actions menu, and Touch Bar.


三、用户界面(User Interface)

让你的应用的用户界面 提供可视、可听、触控反馈给用户,让用户了解到app 正在处理什么。


1、视图和控制 Views and Controls

https://developer.apple.com/documentation/appkit/views_and_controls

在屏幕上显示内容,定义 与这部分内容 允许的交互。

View Fundamentals 基础视图
  • class NSView app 中绘制、打印 和 处理事件的基础。
  • class NSControl 控件基本行为的定义,这些控件是专门的视图,通过使用 目标动作设计模式 将相关事件通知给应用。
  • NSCell 在视图对象中 显示文字或图片的机制,无需使用完整的 NSView 子类开销。
  • NSActionCell 控件中的激活区域。

Container Views 容器视图

使用容器视图 来在你的界面中 组织视图,促进这些视图之间的导航。

  • {} Localization-Friendly Layouts in macOS 这些工程展示本地化优化的自动布局约束,使用 NSGridView 作为容器来达成本地化布局。
  • Grid View 将视图组织在一个弹性的网格中,并处理与这些试图关联的布局。
  • NSSplitView 在一个横向或纵向的线性栈里,管理两个或更多视图的视图。
  • {} Organize Your User Interface with a Stack View 将应用程序用户界面中的各个视图分组为可滚动的堆栈视图。
  • NSStackView 一个在水平或垂直方向排列一组视图的 视图,在窗口大小更改时更新他们的位置和大小。
  • NSTabView 一个包含多页界面的视图,一次显示一页。

Scroll View Provide an interface for navigating content that is too large to fit in the available space.


Content Views 内容视图

Use content views to organize and display your app’s data.

  • Browser View Provide a column-based interface for viewing and navigating hierarchical information.
  • Collection View Display one or more subviews in a highly configurable arrangement.
  • Outline View Display a list-based interface for hierarchical data, where each level of hierarchy is indented from the previous one.
  • Table View Display custom data in rows and columns.
  • NSTextView A view that draws text and handles user interactions with that text.
  • NSOpenGLView A view that displays OpenGL content in a view.

Deprecated


Controls 控件

Use controls to handle specific types of user interactions. Controls are specialized views that use the target-action design pattern to notify your app of interactions with their content.

  • class NSButton 在屏幕上定义可用于触发操作的区域的控件。同比 UIButton。
  • NSColorWell A control that displays a color value and lets the user change that color value.
  • Date Picker Display a calendar date and provide controls for editing the date value.
  • NSImageView A display of image data from an NSImage object in a frame.
  • NSLevelIndicator A visual representation of a level or quantity, using discrete values.
  • Path Control A display of a file system path or virtual path information.
  • NSPopUpButton A control for selecting an item from a list.
  • NSProgressIndicator An interface that provides visual feedback to the user about the status of an ongoing task.
  • NSRuleEditor An interface for configuring a rule-based list of options.
  • NSPredicateEditor A defined set of rules that allows the editing of predicate objects.
  • Search Field Provide a text field that is optimized for text-based search interfaces.
  • NSSegmentedControl Display one or more buttons in a single horizontal group.
  • Slider Display a range of values from which the user selects a single value.
  • NSStepper An interface with up and down arrow buttons for incrementing or decrementing a value.
  • Text Field Provide a simple interface for displaying and editing text, including support for password fields and secure forms of text entry.
  • Token Field Provide a text field whose text can be rendered in a visually distinct way so that users can recognize portions more easily.
  • Toolbar Provide a space for controls under a window’s title bar and above your custom content.
  • Combo Box Display a list of values in a pop-up menu that lets the user select a value or type in a custom value.
  • NSMatrix A legacy interface for grouping radio buttons or other types of cells together.
  • NSSwitch A control that offers a binary choice.

View Layout 视图布局

Visual Adornments 视觉装饰

Add purely decorative elements to your user interface.

  • NSVisualEffectView A view that adds translucency and vibrancy effects to the views in your interface.
  • NSBox A stylized rectangular box with an optional title.

UI Validation

Tool Tips

2、视图管理 View Management

https://developer.apple.com/documentation/appkit/view_management
管理你的用户界面,包含 在窗口中,视图的尺寸和位置。


Content Controllers 内容控制器

Split View Interface 分离试图界面
  • NSSplitViewController An object that manages an array of adjacent child views, and has a split view object for managing dividers between those views.
  • NSSplitView A view that arranges two or more views in a linear stack running horizontally or vertically.
  • NSSplitViewItem An item in a split view controller.

Stack View Interface
  • NSStackView A view that arranges an array of views horizontally or vertically and updates their placement and sizing when the window size changes.

Tab View Interface
  • NSTabViewController A container view controller that manages a tab view interface, which organizes multiple pages of content but displays only one page at a time.
  • NSTabView A multipage interface that displays one page at a time.
  • NSTabViewItem An item in a tab view.

Paged Interface 分页界面
  • NSPageController An object that controls swipe navigation and animations between views or view content.

Media Library Interface 媒体库界面

3、视图布局 View Layout

https://developer.apple.com/documentation/appkit/view_layout

使用 栈视图 或 自动布局 来设置视图的位置和尺寸。


Stack View
  • NSStackView A view that arranges an array of views horizontally or vertically and updates their placement and sizing when the window size changes.

Auto Layout Constraints
  • NSLayoutConstraint The relationship between two user interface objects that must be satisfied by the constraint-based layout system.

Layout Guides
  • NSLayoutGuide A rectangular area that can interact with Auto Layout.
  • NSLayoutDimension A factory class for creating size-based layout constraint objects using a fluent API.

Anchors
  • NSLayoutAnchor A factory class for creating layout constraint objects using a fluent API.
  • NSLayoutXAxisAnchor A factory class for creating horizontal layout constraint objects using a fluent API.
  • NSLayoutYAxisAnchor A factory class for creating vertical layout constraint objects using a fluent API.

4、自定义主题Appearance Customization

https://developer.apple.com/documentation/appkit/appearance_customization

给应用添加 黑暗模式支持,使用 appearance 协议来调整你的 UI。

Dark Mode 黑暗模式

Supporting Dark Mode in Your Interface Update colors, images, and behaviors so that your app adapts automatically when Dark Mode is active.


Appearance System

Apply standard themes to the views in your interface.

  • NSAppearance An object that manages standard appearance attributes for UI elements in an app.
  • NSAppearanceCustomization A set of methods for getting and setting the appearance attributes of a view.

5、动画 Animation

https://developer.apple.com/documentation/appkit/animation
对视图和其他内容 增加动画效果,给用户创造更吸引人的体验。


View-Based Animations 基于View 的动画
  • NSViewAnimation An animation of an app’s views, limited to changes in frame location and size, and to fade-in and fade-out effects.
  • NSAnimatablePropertyContainer A set of methods that defines a way to add animation to an existing class with a minimum of API impact.
  • NSAnimationContext An animation context, which contains information about environment and state.

typealias NSAnimation.Progress The animation progress, as a floating-point number between 0.0 and 1.0.


Presentations 显示

Custom Animations 自定义动画

6、Windows, Panels, and Screens 窗口、面板和屏幕

https://developer.apple.com/documentation/appkit/windows_panels_and_screens

整理视图层次结构,方便其在屏幕上显示。


Windows 窗口
  • class NSWindow 应用程序在屏幕上显示的窗口。
  • class NSPanel A special kind of window that typically performs a function that is auxiliary to the main window.
  • protocol NSWindowDelegate A set of optional methods that a delegate of NSWindow can implement to respond to events, such as window resizing, moving, exposing, and minimizing.
  • class NSWindowTab A tab associated with a window that is part of a tabbing group.
  • class NSWindowTabGroup A group of windows that display together as a single tabbed window.

Window Restoration

Screens 屏幕
  • clas NSScreen 描述计算机显示器或屏幕属性的一种对象。

Popovers 弹窗

Alerts 提示

Open and Save Panels 打开/保存面板

Print and PDF Panels 打印和PDF面板
  • class NSPDFPanel 一个保存或导出PDF 的面板,与macOS用户界面一致。
  • NSPrintPanelAccessorizing 一个打印面板对象可以用来从 打印附件控制器中 获取信息 的一组方法。

Color Panels

Font Panels

7、Sound, Speech, and Haptics 声音、演讲和触觉

https://developer.apple.com/documentation/appkit/sound_speech_and_haptics

在你的界面中 集成 播放音效、触控反馈,合并语音识别。


Sounds 声音
  • class NSSound 一个加载和播放音频文件的简单接口。

Speech 语音

Haptics 触控

8、语言国际化 Localizing Your App

https://developer.apple.com/documentation/xcode/localizing_your_app

通过适应多语言、地区和文化,让你的应用有国际化的受众。


9、Supporting Continuity Camera in Your Mac App 支持连续摄像

https://developer.apple.com/documentation/appkit/supporting_continuity_camera_in_your_mac_app

将用用户的iPhone、iPad或iPod touch拍摄的扫描文件和图片整合到你的Mac应用程序中。


四、用户交互(User Interactions)

管理

1 鼠标、键盘和触控板输入等事件 Mouse, Keyboard, and Trackpad

https://developer.apple.com/documentation/appkit/mouse_keyboard_and_trackpad


Responder Objects
  • NSResponder An abstract class that forms the basis of event and command processing in AppKit.

Mouse, Keyboard, and Touch Events
  • NSEvent An object that contains information about an input action, such as a mouse click or a key press.
  • NSTouch A snapshot of a particular touch at an instant in time.

Trackpad
  • NSPressureConfiguration An encapsulation of the behavior and progression of a Force Touch trackpad as it responds to specific events.
  • NSHapticFeedbackManager An object that provides access to the haptic feedback management attributes on a system with a Force Touch trackpad.

Constants

init(type: NSEvent.EventType) Returns the event mask for the specified type.


2 Menus, Cursors, and the Dock 目录、光标和 Dock

https://developer.apple.com/documentation/appkit/menus_cursors_and_the_dock

使用目录和光标,促进和 app 的交互;使用 app Dock tile 来传递更新信息。


Menus

Menu Validation

Menu Bar Items

Cursors 光标

The Dock
  • class NSDockTile 当你的应用程序的小窗口和图标出现在Dock时的 视觉表现。
  • protocol NSDockTilePlugIn 插件实现的一组方法,允许在应用程序不运行时定制应用程序的 Dock tile。

3 手势 Gestures

https://developer.apple.com/documentation/appkit/gestures
封装应用的 手势识别的 事件处理逻辑,让代码可以在app内贯穿重用。


Standard Gestures

Custom Gestures

4、 触控条 Touch Bar

https://developer.apple.com/documentation/appkit/touch_bar

在触控条展示交互内容和控件。


Essentials

Integrating a Toolbar and Touch Bar into Your App Build a toolbar in your app’s window, with a corresponding Touch Bar, for quick access to app features.

Creating and Customizing the Touch Bar Use and customize your app’s Touch Bar for macOS.


Touch Bar Items

Scrubber Items

Scrubber Layouts

5、拖放 Drag and Drop

https://developer.apple.com/documentation/appkit/drag_and_drop
使用拖放来支持直接操作app。


Drag Sources

Originate content from a drag source by creating items to represent that content.


Drop Targets

Receive dragged content in your app’s objects.


6、macOS 辅助功能 Accessibility for macOS

https://developer.apple.com/documentation/appkit/accessibility_for_macos
让你的应用 可以被 任何使用 macOS 的人 来使用。


Essentials

Integrating Accessibility into Your App Make your app more accessible to users with disabilities by adding accessibility features.

Accessibility Design for Mac Catalyst Improve navigation in your app by using keyboard shortcuts and accessibility containers.


AppKit Elements

Custom Elements

Accessibility Types

Convenience Functions

var description: String? Returns a standard description for an action.

static func post(element: Any, notification: NSAccessibility.Notification) Sends a notification to any observing assistive applications.

static func post(element: Any, notification: NSAccessibility.Notification, userInfo: [NSAccessibility.NotificationUserInfoKey : Any\]?) Sends a notification and an optional userInfo dictionary to any observing assistive applications.

static func raiseBadArgumentException(Any!, NSAccessibility.Attribute!, Any!) Raises an error if the parameter is the wrong type or has an illegal value

Deprecated

func description(with: NSAccessibility.Subrole?) -> String? Returns a standard description for a role and subrole.

static func description(for: Any) -> String? Returns a standard role description for a user interface element.

static func setMayContainProtectedContent(Bool) -> Bool Sets whether the app may have protected content.

static func unignoredChildren(from: [Any\]) -> [Any] Returns a list of unignored accessibility objects, descending the hierarchy if necessary.

static func unignoredChildrenForOnlyChild(from: Any) -> [Any\] Returns a list of unignored accessibility objects, descending the hierarchy if necessary.

static func unignoredDescendant(of: Any) -> Any? Returns an unignored accessibility object, descending the hierarchy if necessary.

static func unignoredAncestor(of: Any) -> Any? Returns an unignored accessibility object, ascending the hierarchy if necessary.

static func screenRect(fromView: NSView, rect: NSRect) -> NSRect Returns the frame in screen coordinates.

static func screenPoint(fromView: NSView, point: NSPoint) -> NSPoint Returns the point in screen coordinates.


Deprecated

protocol NSAccessibility A legacy, informal protocol that is not recommended for active use.


五、Graphics, Drawing, Color, and Printing


1、图片和 PDF Images and PDF

https://developer.apple.com/documentation/appkit/images_and_pdf
创建和管理位图、PDF 和 其他格式的图片。


Images 图片

Bitmap Formats 位图格式
  • NSBitmapImageRep 一种根据位图数据呈现图像的对象。
  • NSCIImageRep 可以从核心图像对象呈现图像的对象。
  • NSPICTImageRep 从版本1、版本2和扩展版本2的PICT格式数据流呈现图像的对象。

Vector Formats 矢量格式
  • NSPDFImageRep An object that can render an image from a PDF format data stream.
  • NSPDFInfo An object that stores information associated with the creation of a PDF file, such as its URL, tag names, page orientation, and paper size.
  • NSEPSImageRep An object that can render an image from encapsulated PostScript (EPS) code.

Custom Formats 自定义格式
  • NSCustomImageRep An object that uses a delegate object to render an image from a custom format. 使用代理对象,以自定义格式呈现图像的对象。

2、画图 Drawing

https://developer.apple.com/documentation/appkit/drawing
绘制图形、图片和其他内容。

Drawing Contexts
Shapes and Paths
Strings
Gradients
Shadows
OpenGL

OpenGL 已经过期,使用 MetalMetalKit 来代替


3、颜色 Color

https://developer.apple.com/documentation/appkit/color
使用内置或自定义的格式 展示颜色,给用户选项 来选择和使用颜色。

Colors
  • class NSColor An object that stores color data and sometimes opacity (alpha value). 一种存储颜色数据,有时不透明度(alpha值)的 对象。
  • class NSColorList 颜色对象的有序列表,由键标识。
  • class NSColorSpace 表示自定义颜色空间的对象。
Color Selection
Color Sampler
  • class NSColorSampler An object that displays the system’s color-sampling interface and returns the selected color to your app.

4、打印 Printing

https://developer.apple.com/documentation/appkit/printing
显示系统打印面板,管理打印过程。


Print Panels

Print Information

六、Text

1、 文字展示 Text Display

https://developer.apple.com/documentation/appkit/text_display
展示文字,检查拼写

Text Views
  • class NSTextField 用户可以选择或编辑的文本,并在用户按下返回键时 将其操作消息发送给目标。
  • protocol NSTextFieldDelegate textField 代理 可用于控制其 field 编辑器操作菜单 的协议。
  • class NSTextView 绘制文本并处理用户与该文本交互的视图。
  • protocol NSTextViewDelegate 文本视图代理 可用于管理选择、设置文本属性、使用拼写检查器等的一组可选方法。
  • class NSText 用于管理文本的对象的最通用编程接口。
    • protocol NSTextDelegate 一个 NSText 对象的代理使用的一组可选方法,用来编辑文本和修改文本格式。

Text Checking
Spell Checker

2、TextKit

https://developer.apple.com/documentation/appkit/textkit
管理文字储存,在视图中展示自定的文字内容。

Data Management 数据管理

Layout 布局
Formatted Content 格式化内容

Attachments 附件
  • NSTextAttachment The values for the attachment attributes of attributed strings and related objects.
  • NSTextAttachmentCell An object that implements the functionality of the text attachment cell protocol.
  • NSTextAttachmentCellProtocol A set of methods that declares the interface for objects that draw text attachment icons and handle mouse events on their icons.
  • NSTextAttachmentContainer A set of methods that defines the interface to text attachment objects from a layout manager.

Glyphs 符号

typealias NSGlyph The type used to specify glyphs.

Glyph Attributes Attributes that are used only inside the glyph generation machinery, but must also be shared between components.

func NSConvertGlyphsToPackedGlyphs(UnsafeMutablePointer, Int, NSMultibyteGlyphPacking, UnsafeMutablePointer) -> Int Prepares a set of glyphs for processing by character-based routines.

Deprecated


Input Management 输入管理
  • NSTextInput A set of methods that text views must implement to interact properly with the text input management system.
  • NSTextInputClient A set of methods that text views must implement to interact properly with the text input management system.
  • NSTextInputContext An object that represents the Cocoa text input system.
  • NSTextAlternatives A list of alternative strings for a piece of text.

3、字体 Fonts

https://developer.apple.com/documentation/appkit/fonts
管理显示文字的字体。


Font Data

Management

三、其它

过期 Deprecated

Avoid using deprecated classes and protocols in your apps.

Deprecated Symbols Review symbols that are no longer supported, and find the replacements to use instead.


伊织 2020-10-02

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值