提交 8e478b6e 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

提交iOS平台 native-button 示例

上级 abe2492b
<template>
<view>
<native-button class="native-button" style="width: 200px; height: 100px;" :text="buttonText" @tap="ontap"
<native-button class="native-button" style="width: 200px; height: 100px;" :text="buttonText" @buttonTap="ontap"
@load="onload"></native-button>
<time-picker class="native-time-picker" :hour=2 :minute=3 @changed="onChanged"></time-picker>
......
<template>
<object @init="onObjectInit" @click="onclick"></object>
<object @init="onObjectInit" @customClick="onclick"></object>
</template>
......@@ -39,11 +39,14 @@
this.$emit("load")
},
onclick(e: UniObjectCustomEvent) {
this.$emit("tap", e)
this.$emit("buttonTap", e)
}
},
unmounted() {
// #ifdef APP-IOS
// iOS平台需要主动释放 uts 实例
this.button.destroy()
// #endi
}
}
</script>
......
......@@ -18,7 +18,7 @@ export class NativeButton {
this.button?.setOnClickListener(_ => {
const detail = {}
//构建自定义UniObjectCustomEvent返回对象
const event = new UniObjectCustomEvent("click", detail)
const event = new UniObjectCustomEvent("customClick", detail)
//响应分发原生Button的点击事件
$element.dispatchEvent(event)
})
......
import { UIButton, UIControl,ButtonType } from "UIKit"
export class NativeButton {
element : UniObjectElement;
button : UIButton;
constructor(element : UniObjectElement) {
this.element = element;
this.button = new UIButton(type=UIButton.ButtonType.system)
super.init()
// 在 swift target-action 对应的方法需要以OC的方式来调用,那么OC语言中用Selector来表示一个方法的名称(又称方法选择器),创建一个Selector可以使用 Selector("functionName") 的方式。
const method = Selector("buttonClickAction")
// button 添加点击回调
button.addTarget(this, action = method, for = UIControl.Event.touchUpInside)
this.bind();
}
// element 绑定原生view
bind() {
this.element.bindIOSView(this.button);
}
// 更新button文字
updateText(text : string) {
this.button.setTitle(text, for = UIControl.State.normal)
}
/**
* 按钮点击回调方法
* 在 swift 中,所有target-action (例如按钮的点击事件,NotificationCenter 的通知事件等)对应的 action 函数前面都要使用 @objc 进行标记。
*/
@objc buttonClickAction() {
// 发送事件
let event = new UniObjectCustomEvent("customClick")
this.element.dispatchEvent(event)
}
destroy() {
UTSiOS.destroyInstance(self)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册