View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • AI 与机器学习
    • App Intents
    • Apple 智能
    • 游戏
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App 审核帮助》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 代码
  • 为 Mac App 引入触控功能

    探索如何为你的 Mac App 添加触控功能。了解如何从传统的鼠标跟踪 API 过渡到现代手势识别器和原生触控控件。了解如何优化触控目标、实现多点触控操作以及管理系统手势,从而提供出色的触控体验。为充分利用本讲座,请先观看《针对 Mac 设计触控体验》。

    章节

    • 0:00 - Introduction
    • 0:30 - Touch across frameworks
    • 1:37 - Use control events
    • 3:05 - Adopt gesture recognizers
    • 5:14 - Expand hit targets
    • 6:24 - Customize touch scrolling
    • 6:57 - Manage gesture exclusivity
    • 8:17 - Disable system gestures
    • 9:39 - Additional touch tips
    • 11:56 - Next steps

    资源

      • 高清视频
      • 标清视频

    相关视频

    Tech Talks

    • 针对 Mac 设计触控体验
  • 搜索此视频…
    • 2:08 - Custom button that overrides mouseDown

      // Custom button to detect start and end of tracking
      class IgnitionButton: NSButton {
          var delegate: IgnitionButtonDelegate?
      
          override func mouseDown(with event: NSEvent) {
              delegate?.buttonDidStartTracking()
              super.mouseDown(with: event)
              delegate?.buttonDidEndTracking()
          }
      }
    • 2:24 - Use NSControl events to detect tracking

      // Use NSControl events to detect start and end of tracking
      ignitionButton.addTarget(
          self,
          action: #selector(buttonDidStartTracking),
          for: [.trackingBegan, .trackingEntered]
      )
      
      ignitionButton.addTarget(
          self,
          action: #selector(buttonDidEndTracking),
          for: [.trackingExited, .trackingEndedInside]
      )
      
      ignitionButton.addTarget(
          self,
          action: #selector(buttonCancelledTracking),
          for: .trackingCancelled
      )
    • 3:13 - Mouse tracking loop

      let eventMask: NSEvent.EventTypeMask = [.leftMouseDragged, .leftMouseUp]
      while true {
          let nextEvent = NSApp.nextEvent(
              matching: eventMask,
              until: .distantFuture,
              inMode: .eventTracking,
              dequeue: true)
          if let nextEvent, nextEvent.type == .leftMouseUp {
              break
          }
      }
    • 4:11 - Refactored tracking functions

      // Refactored tracking functions
      func startTracking(at viewLocation: NSPoint) { /* ... */ }
      
      func continueTracking(at viewLocation: NSPoint) { /* ... */ }
      
      func endTracking(at viewLocation: NSPoint) { /* ... */ }
      
      func cancelTracking() { /* ... */ }
    • 4:24 - Add a touch-only pan gesture recognizer

      // Add touch-only pan gesture recognizer
      let touchRecognizer = NSPanGestureRecognizer(
          target: self,
          action: #selector(touchPan)
      )
      
      touchRecognizer.buttonMask = 0x0
      touchRecognizer.allowedTouchTypes = .direct
      
      wiringPanelView.addGestureRecognizer(touchRecognizer)
    • 4:52 - Pan gesture recognizer action

      // Pan gesture recognizer action function
      @objc func touchPan(_ recognizer: NSPanGestureRecognizer) {
          switch recognizer.state {
          case .began:
              startTracking(at: recognizer.location(in: wiringPanelView))
          case .changed:
              continueTracking(at: recognizer.location(in: wiringPanelView))
          case .ended:
              endTracking(at: recognizer.location(in: wiringPanelView))
          case .cancelled:
              cancelTracking()
          default:
              break
          }
      }
    • 5:37 - Expand hit targets for touch

      // Refactored tracking functions with expanded hit targets
      func startTracking(at viewLocation: NSPoint, expandHitRect: Bool = false) {
          for node in nodes {
              if point(viewLocation, in: node.frame, expandForTouch: expandHitRect) {
                  trackingNode = node
                  trackingLocation = viewLocation
                  return
              }
          }
      }
      
      func point(_ point: NSPoint, in rect: NSRect, expandForTouch: Bool = false) -> Bool {
          if expandForTouch {
              // Expand to minimum size of 36 points on each axis
          } else {
              // Nothing to do for mouse
          }
          return NSPointInRect(point, rect)
      }
      
      @objc func touchPan(_ recognizer: NSPanGestureRecognizer) {
          switch recognizer.state {
          case .began:
              startTracking(at: recognizer.location(in: wiringPanelView), expandHitRect: true)
          // ...
          default:
              break
          }
      }
    • 6:40 - Require two touches for scrolling

      // Modify touch scrolling properties
      decoratingView.enclosingScrollView?.minimumNumberOfTouchesForScrolling = 2
    • 7:48 - Allow simultaneous slider interaction

      // Simultaneous sliders
      stoveControlsView.exclusiveGestureBehavior = .notExclusive
    • 8:25 - Disable system gestures

      // Disabling system gestures
      stoveControlsView.window?.disablesDirectTouchSystemGestures = true
      
      // Mac Catalyst API
      extension UIViewController {
          var prefersDirectTouchSystemGesturesDisabled: Bool { get }
      }
      
      // SwiftUI API
      View.directTouchSystemGesturesDisabled()
    • 11:47 - Declare touch-native support in Info.plist

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
          <!-- ... -->
          <key>NSIsTouchNative</key>
          <true/>
          <!-- ... -->
      </dict>
      </plist>

开发者页脚

  • 视频
  • Tech Talks
  • 为 Mac App 引入触控功能
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • AI 与机器学习
    • Apple 智能
    • 音频与视频
    • 增强现实
    • 商务
    • 设计
    • 分发
    • 教育
    • 游戏
    • 健康与健身
    • Apple App 内购买
    • 本地化
    • 地图与位置
    • 安全性
    • Safari 浏览器与网页
    打开菜单 关闭菜单
    • 文档
    • 下载
    • 示例代码
    • 视频
    • 文档归档
    打开菜单 关闭菜单
    • 帮助指南与文章
    • 联系我们
    • 论坛
    • 反馈与错误报告
    • 系统状态
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件
    • “反馈助理”
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • 安全赏金计划
    • Security Research Device Program
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Academy
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App,并在 bilibili 和微信上关注我们。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则