【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)
    }
}

效果

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

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

相关推荐
songgeb2 天前
Concurrency in Swift学习笔记-初识
ios·swift
杂雾无尘3 天前
2025 年了,是否该全面拥抱 Swift 6?
ios·swift·客户端
杂雾无尘4 天前
用高斯公式优化 Swift 代码,让运行速度飞跃数十万倍!
ios·swift·apple
杂雾无尘5 天前
解决 Xcode 烦人错误:"Build input file cannot be found" 一招搞定!
ios·swift·客户端
水木姚姚6 天前
图书管理软件iOS(iPhone)
macos·ios·iphone·xcode·swift
东坡肘子6 天前
F1:电影很好看,赛事很挣钱 | 肘子的 Swift 周报 #094
swiftui·swift·apple
YungFan7 天前
iOS26适配指南之动画
ios·swift
瓜子三百克7 天前
Swift6.1 - 基础知识1: 简单值、控制流、函数和闭包
开发语言·swift
songgeb8 天前
Currying and Partial application
swift·函数式编程
Keya10 天前
lipo 命令行指南
ios·xcode·swift