From f0b90df01e323ee1e799969d4956abd1fc231ecb Mon Sep 17 00:00:00 2001 From: XHY Date: Fri, 26 Jul 2024 15:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4iOS=E5=B9=B3=E5=8F=B0=20time-?= =?UTF-8?q?pick=20=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/component/object/object.uvue | 6 +- .../utssdk/app-ios/config.json | 3 + .../uni-time-picker/utssdk/app-ios/index.uts | 84 +++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 uni_modules/uni-time-picker/utssdk/app-ios/config.json create mode 100644 uni_modules/uni-time-picker/utssdk/app-ios/index.uts diff --git a/pages/component/object/object.uvue b/pages/component/object/object.uvue index 8b73b108..7c05c5c4 100644 --- a/pages/component/object/object.uvue +++ b/pages/component/object/object.uvue @@ -1,10 +1,8 @@ diff --git a/uni_modules/uni-time-picker/utssdk/app-ios/config.json b/uni_modules/uni-time-picker/utssdk/app-ios/config.json new file mode 100644 index 00000000..33128280 --- /dev/null +++ b/uni_modules/uni-time-picker/utssdk/app-ios/config.json @@ -0,0 +1,3 @@ +{ + "deploymentTarget": "12" +} diff --git a/uni_modules/uni-time-picker/utssdk/app-ios/index.uts b/uni_modules/uni-time-picker/utssdk/app-ios/index.uts new file mode 100644 index 00000000..d4d21990 --- /dev/null +++ b/uni_modules/uni-time-picker/utssdk/app-ios/index.uts @@ -0,0 +1,84 @@ +import { UIDatePicker, UIControl, UIDatePickerStyle } from "UIKit" +import { DateFormatter } from "CoreFoundation" + +export class NativeTimePicker { + + element : UniObjectElement + timePicker : UIDatePicker + h : number + m : number + + constructor(element : UniObjectElement, hour : number, minute : number) { + this.element = element + this.timePicker = new UIDatePicker() + this.h = hour + this.m = minute + super.init() + + // 在 swift target-action 对应的方法需要以OC的方式来调用,那么OC语言中用Selector来表示一个方法的名称(又称方法选择器),创建一个Selector可以使用 Selector("functionName") 的方式。 + const method = Selector("timeChange") + // 监听时间变化回调 + this.timePicker.addTarget(this, action = method, for = UIControl.Event.valueChanged) + + // 设置为时间选择模式 + this.timePicker.datePickerMode = UIDatePicker.Mode.time + + // 设置外观样式为 wheels + if (UTSiOS.available("iOS 13.4, *")) { + this.timePicker.preferredDatePickerStyle = UIDatePickerStyle.wheels + } + + this.updateTime() + this.bindView(hour, minute) + } + + // element 绑定原生view + bindView(hour : number, minute : number) { + this.element.bindIOSView(this.timePicker); + } + + // 设置时 + setHour(hour : number) { + this.h = hour + this.updateTime() + } + + // 设置分 + setMinute(minute : number) { + this.m = minute + this.updateTime() + } + + updateTime() { + let formatter = DateFormatter() + formatter.dateFormat = "HH:mm" + let date = formatter.date(from = `${this.h}:${this.m}`) + if (date != null) { + this.timePicker.date = date! + } + } + + /** + * 按钮点击回调方法 + * 在 swift 中,所有target-action (例如按钮的点击事件,NotificationCenter 的通知事件等)对应的 action 函数前面都要使用 @objc 进行标记。 + */ + @objc timeChange() { + // 发送事件 + console.log("timeChange") + + let formatter = DateFormatter() + formatter.dateFormat = "HH" + let hourString = formatter.string(from = this.timePicker.date) + + formatter.dateFormat = "mm" + let minuteString = formatter.string(from = this.timePicker.date) + + const detail = { "hour": hourString, "minute": minuteString } + const event = new UniObjectCustomEvent("timechanged", detail) + this.element.dispatchEvent(event) + } + + destroy() { + UTSiOS.destroyInstance(self) + } +} \ No newline at end of file -- GitLab