From cff302a983e433f71afce60665a4b458b8f9c5ef Mon Sep 17 00:00:00 2001 From: shutao Date: Wed, 23 Oct 2024 11:48:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=98=E5=8E=9Fe5e9c00902ab61c447434fea413d3?= =?UTF-8?q?02f81b1563c=E5=90=88=E5=B9=B6=E6=97=B6=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/time-picker/time-picker.uvue | 73 ++++++++++++++++ uni_modules/native-time-picker/package.json | 83 ++++++++++++++++++ uni_modules/native-time-picker/readme.md | 1 + .../utssdk/app-android/config.json | 3 + .../utssdk/app-ios/config.json | 3 + .../utssdk/app-ios/index.uts | 85 +++++++++++++++++++ 6 files changed, 248 insertions(+) create mode 100644 uni_modules/native-time-picker/components/time-picker/time-picker.uvue create mode 100644 uni_modules/native-time-picker/package.json create mode 100644 uni_modules/native-time-picker/readme.md create mode 100644 uni_modules/native-time-picker/utssdk/app-android/config.json create mode 100644 uni_modules/native-time-picker/utssdk/app-ios/config.json create mode 100644 uni_modules/native-time-picker/utssdk/app-ios/index.uts diff --git a/uni_modules/native-time-picker/components/time-picker/time-picker.uvue b/uni_modules/native-time-picker/components/time-picker/time-picker.uvue new file mode 100644 index 00000000..50fa1e61 --- /dev/null +++ b/uni_modules/native-time-picker/components/time-picker/time-picker.uvue @@ -0,0 +1,73 @@ + + + + + diff --git a/uni_modules/native-time-picker/package.json b/uni_modules/native-time-picker/package.json new file mode 100644 index 00000000..9d5b19a4 --- /dev/null +++ b/uni_modules/native-time-picker/package.json @@ -0,0 +1,83 @@ +{ + "id": "uni-time-picker", + "displayName": "uni-time-picker", + "version": "1.0.0", + "description": "uni-time-picker", + "keywords": [ + "uni-time-picker" +], + "repository": "", + "engines": { + "HBuilderX": "^3.1.0" + }, + "dcloudext": { + "type": "component-vue", + "sale": { + "regular": { + "price": "0.00" + }, + "sourcecode": { + "price": "0.00" + } + }, + "contact": { + "qq": "" + }, + "declaration": { + "ads": "", + "data": "", + "permissions": "" + }, + "npmurl": "" + }, + "uni_modules": { + "dependencies": [], + "encrypt": [], + "platforms": { + "cloud": { + "tcb": "u", + "aliyun": "u", + "alipay": "u" + }, + "client": { + "Vue": { + "vue2": "u", + "vue3": "u" + }, + "App": { + "app-vue": "u", + "app-nvue": "u", + "app-uvue": "u" + }, + "H5-mobile": { + "Safari": "u", + "Android Browser": "u", + "微信浏览器(Android)": "u", + "QQ浏览器(Android)": "u" + }, + "H5-pc": { + "Chrome": "u", + "IE": "u", + "Edge": "u", + "Firefox": "u", + "Safari": "u" + }, + "小程序": { + "微信": "u", + "阿里": "u", + "百度": "u", + "字节跳动": "u", + "QQ": "u", + "钉钉": "u", + "快手": "u", + "飞书": "u", + "京东": "u" + }, + "快应用": { + "华为": "u", + "联盟": "u" + } + } + } + } +} \ No newline at end of file diff --git a/uni_modules/native-time-picker/readme.md b/uni_modules/native-time-picker/readme.md new file mode 100644 index 00000000..c119a2a8 --- /dev/null +++ b/uni_modules/native-time-picker/readme.md @@ -0,0 +1 @@ +# uni-time-picker \ No newline at end of file diff --git a/uni_modules/native-time-picker/utssdk/app-android/config.json b/uni_modules/native-time-picker/utssdk/app-android/config.json new file mode 100644 index 00000000..bf959256 --- /dev/null +++ b/uni_modules/native-time-picker/utssdk/app-android/config.json @@ -0,0 +1,3 @@ +{ + "minSdkVersion": "21" +} \ No newline at end of file diff --git a/uni_modules/native-time-picker/utssdk/app-ios/config.json b/uni_modules/native-time-picker/utssdk/app-ios/config.json new file mode 100644 index 00000000..33128280 --- /dev/null +++ b/uni_modules/native-time-picker/utssdk/app-ios/config.json @@ -0,0 +1,3 @@ +{ + "deploymentTarget": "12" +} diff --git a/uni_modules/native-time-picker/utssdk/app-ios/index.uts b/uni_modules/native-time-picker/utssdk/app-ios/index.uts new file mode 100644 index 00000000..90abdc15 --- /dev/null +++ b/uni_modules/native-time-picker/utssdk/app-ios/index.uts @@ -0,0 +1,85 @@ +import { UIDatePicker, UIControl, UIDatePickerStyle } from "UIKit" +import { DateFormatter } from "CoreFoundation" + +export class NativeTimePicker { + + element : UniNativeViewElement + timePicker : UIDatePicker + h : number + m : number + + constructor(element : UniNativeViewElement, 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 UniNativeViewEvent("timechanged", detail) + this.element.dispatchEvent(event) + } + + destroy() { + UTSiOS.destroyInstance(this) + } +} -- GitLab