index.uts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { UIButton, UIControl } from "UIKit"
  2. import { INativeButtonContext, CreateNativeButtonContext } from "../interface.uts"
  3. import { CGFloat } from 'CoreFoundation';
  4. export class NativeButton {
  5. element : UniNativeViewElement;
  6. button : UIButton | null;
  7. constructor(element : UniNativeViewElement) {
  8. // 接收组件传递过来的UniNativeViewElement
  9. this.element = element;
  10. super.init()
  11. this.bindView();
  12. }
  13. // element 绑定原生view
  14. bindView() {
  15. // 初始化原生 UIButton
  16. this.button = new UIButton(type=UIButton.ButtonType.system)
  17. // 构建方法选择器
  18. const method = Selector("buttonClickAction")
  19. // button 绑定点击回调方法
  20. button?.addTarget(this, action = method, for = UIControl.Event.touchUpInside)
  21. // UniNativeViewElement 绑定原生 view
  22. this.element.bindIOSView(this.button!);
  23. }
  24. updateText(text : string) {
  25. // 更新 button 显示文字
  26. this.button?.setTitle(text, for = UIControl.State.normal)
  27. }
  28. /**
  29. * 按钮点击回调方法
  30. * 在 swift 中,所有target-action (例如按钮的点击事件,NotificationCenter 的通知事件等)对应的 action 函数前面都要使用 @objc 进行标记。
  31. */
  32. @objc buttonClickAction() {
  33. //构建自定义 UniNativeViewEvent 对象
  34. let event = new UniNativeViewEvent("customClick")
  35. //触发自定义事件
  36. this.element.dispatchEvent(event)
  37. }
  38. destroy() {
  39. // 释放 UTS 实例对象,避免内存泄露
  40. UTSiOS.destroyInstance(this)
  41. }
  42. }
  43. function getNativeViewElemet(element: UniElement | null): UniNativeViewElement | null {
  44. if (element == null) {
  45. return null;
  46. }
  47. if (element instanceof UniNativeViewElement) {
  48. return element as UniNativeViewElement
  49. }
  50. for (item in element!.children) {
  51. let tmp = getNativeViewElemet(item)
  52. if (tmp != null) {
  53. return tmp
  54. }
  55. }
  56. return null
  57. }
  58. export const createNativeButtonContext: CreateNativeButtonContext = function (id : string, component ?: ComponentPublicInstance | null) : INativeButtonContext | null {
  59. let element : UniNativeViewElement | null = null;
  60. let e: UniElement | null = null;
  61. if (component == null) {
  62. e = uni.getElementById(id)
  63. } else {
  64. e = component?.$el as UniElement | null;
  65. }
  66. if (e instanceof UniNativeViewElement) {
  67. element = e as UniNativeViewElement | null
  68. }else {
  69. element = getNativeViewElemet(e)
  70. }
  71. if (element == null) return null;
  72. return new NativeButtonContextImpl(element!);
  73. }
  74. class NativeButtonContextImpl implements INativeButtonContext {
  75. btn: UIButton | null
  76. constructor(element : UniNativeViewElement) {
  77. let view = element.getIOSView()
  78. if (view != null) {
  79. this.btn = view as UIButton
  80. }
  81. }
  82. updateText(title: string) : void {
  83. this.btn?.setTitle(title, for = UIControl.State.normal)
  84. }
  85. }