【iOS小组件】可交互小组件

可交互小组件


注意:

  • 需要 iOS 17及以上版本

  • 可交互的组件仅支持 ButtonToggle 组件,其他控件不支持。

AppIntent意图

要想实现可点击交互的按钮需要继承 AppIntent 并实现对应的方法,AppIntentAppIntents 库中,需要先引入 import AppIntents

swift 复制代码
import AppIntents

// 全局存储结果
struct NumberManager {
    static var number = 0
}

struct MyCalculateIntent: AppIntent {
    // 标题
    static var title: LocalizedStringResource = "My Calculate Task"
    // 描述
    static var description: IntentDescription = IntentDescription("My Calculate Number Task")
    
    // 定义一个变量value
    @Parameter(title: "value")
    var value: Int
    
    init() { }
    init(value: Int) {
       self.value = value
    }
    
    // 计算结果,结果存储到全局
    func perform() async throws -> some IntentResult {
        NumberManager.number += value
        return .result()
    }
}

MyCalculateIntent 结构体遵守 AppIntent 协议并实现了对应的协议内容:titledescription ,并添加了一个 value 属性用来存储传入的数字,最后实现了 perform() 方法。

最终 MyCalculateIntent 会作用在可交互的按钮上,当点击按钮的时候就会调用 perform() 方法进行计算并将计算结果返回。

Widget

可交互小组件只支持 ButtonToggle , 点到组件我们可以看到在 iOS 17 上新增了支持传入 AppIntent 协议的初始化方法。

Widget代码

less 复制代码
struct MyWidgetMiddleEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
        VStack(spacing: 10) {
            HStack {
                 Text("Time:")
                 Text(entry.date, style: .time)
             }

            Text("结果: \(NumberManager.number)")
            
            HStack {
                Button(intent: MyCalculateIntent(value: 10)) {
                    Text("加 10")
                }
                Button(intent: MyCalculateIntent(value: 20)) {
                    Text("加 20")
                }
                Button(intent: MyCalculateIntent(value: -10)) {
                    Text("减 10")
                }
                Button(intent: MyCalculateIntent(value: -20)) {
                    Text("减 20")
                }
            }
            Text("中号组件类型")
        }
        .widgetBackground(Color.white)
    }
}
less 复制代码
struct MyWidgetMiddleEntryView : View {
    var entry: Provider.Entry
    @State var isOn: Bool = true
    
    var body: some View {
        VStack(spacing: 10) {
            HStack {
                 Text("Time:")
                 Text(entry.date, style: .time)
             }

            Text("结果: \(NumberManager.number)")
            
            HStack {
                Toggle(isOn: NumberManager.number % 2 == 0 ? false : true, intent: MyCalculateIntent(value: 1)) {
                    Text("状态发生改变加1");
                }.padding()
            }
        }
        .widgetBackground(Color.white)
    }
}

效果

最好运行看效果,预览模式可能不响应

本文同步自微信公众号 "程序员小溪" ,这里只是同步,想看及时消息请移步我的公众号,不定时更新我的学习经验。

相关推荐
云逸_2 天前
Cinder:基于 Swift Macro 与 Mach-O Section 的解耦注册机制
ios·swift
末代iOS程序员华仔2 天前
从 Figma 到上架:使用 Codex 与 MCP 全流程实现 AI 聊天 App
flutter·ios·swift
霍霍哈嗨3 天前
swiftUI框架基础
ios·swiftui·swift
末代iOS程序员华仔3 天前
iOS 开发到上架 App Store 全流程详解
ios·objective-c·swift
东坡肘子6 天前
当每一次写入都有了新价格 -- 肘子的 Swift 周报 #144
人工智能·swiftui·swift
zzb15806 天前
Zed 配置 Swift / iOS 开发
开发语言·ios·swift
Geek-Chow7 天前
Mobile App Certificate Pinning: Underlying Principle and a Swift Example
开发语言·ios·swift·安全架构
碎_浪9 天前
Mac 壁纸被 MDM 锁住?我做了个 2MB 的开源方案
macos·开源·swift
小牛不牛的程序员9 天前
开源一个 macOS 剪贴板「意图识别」工具——不只是存储,更是理解你复制了什么
macos·swift
apihz11 天前
经纬度制作高清卫星图片免费 API 接口详解
开发语言·ios·swift