提交 5b6f9356 编写于 作者: 雪洛's avatar 雪洛

fix: 修复类型错误、格式化代码

上级 04f55315
<template> <template>
<scroll-view style="flex: 1"> <scroll-view style="flex: 1">
<page-head title="拖拽图标测试相关事件"></page-head> <page-head title="拖拽图标测试相关事件"></page-head>
<view class="container"> <view class="container">
<view class="view-box" @touchstart="onViewTouchStart"> <view class="view-box" @touchstart="onViewTouchStart">
<image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel" <image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel"
@touchmove="onTouchMove" @touchend="onTouchEnd"></image> @touchmove="onTouchMove" @touchend="onTouchEnd"></image>
</view> </view>
</view> </view>
<view v-if="touchEvent !== null"> <view v-if="touchEvent !== null">
<text class="title1">touches: </text> <text class="title1">touches: </text>
<template v-for="(touch, index) in touchEvent!.touches" :key="index"> <template v-for="(touch, index) in touchEvent!.touches" :key="index">
<text class="title2">touch[{{ index }}]:</text> <text class="title2">touch[{{ index }}]:</text>
<text>identifier: {{touch.identifier}}</text> <text>identifier: {{touch.identifier}}</text>
<text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text> <text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text>
<text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text> <text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text>
<text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text> <text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text>
</template> </template>
</view> </view>
</scroll-view> </scroll-view>
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
move: false, move: false,
posX: 0, posX: 0,
posY: 0, posY: 0,
lastX: 0, lastX: 0,
lastY: 0, lastY: 0,
touchEvent: null as TouchEvent | null, touchEvent: null as TouchEvent | null,
icon: null as UniElement | null, icon: null as UniElement | null,
touchTargets: "", touchTargets: "",
touchTargetsCount: 0, touchTargetsCount: 0,
iconRect : null as DOMRect | null iconRect : null as DOMRect | null
} }
}, },
onReady() { onReady() {
this.icon = uni.getElementById("icon") this.icon = uni.getElementById("icon")
// #ifdef APP-IOS // #ifdef APP-IOS
this.iconRect = this.icon?.getBoundingClientRect() this.iconRect = this.icon?.getBoundingClientRect()
// 加上导航栏及状态栏高度 // 加上导航栏及状态栏高度
this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44 this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
// #endif // #endif
}, },
methods: { methods: {
onViewTouchStart(e : TouchEvent) { onViewTouchStart(e : TouchEvent) {
this.touchTargets += e.target?.tagName + e.currentTarget?.tagName this.touchTargets += e.target!.tagName + e.currentTarget!.tagName
this.touchTargetsCount++ this.touchTargetsCount++
console.log(this.touchTargets, this.touchTargetsCount) console.log(this.touchTargets, this.touchTargetsCount)
}, },
onTouchStart(e : TouchEvent) { onTouchStart(e : TouchEvent) {
this.touchTargetsCount++ this.touchTargetsCount++
this.touchTargets += e.target?.tagName + e.currentTarget?.tagName this.touchTargets += e.target!.tagName + e.currentTarget!.tagName
this.touchEvent = e this.touchEvent = e
if (!this.move) { if (!this.move) {
this.move = true this.move = true
this.posX = e.touches[0].screenX this.posX = e.touches[0].screenX
this.posY = e.touches[0].screenY this.posY = e.touches[0].screenY
} }
}, },
onTouchMove(e : TouchEvent) { onTouchMove(e : TouchEvent) {
e.preventDefault() e.preventDefault()
this.touchEvent = e this.touchEvent = e
let p = e.touches[0] let p = e.touches[0]
if (p.screenX == this.lastX && p.screenY == this.lastY) { if (p.screenX == this.lastX && p.screenY == this.lastY) {
return return
} }
let x = p.screenX - this.posX let x = p.screenX - this.posX
let y = p.screenY - this.posY let y = p.screenY - this.posY
this.lastX = p.screenX this.lastX = p.screenX
this.lastY = p.screenY this.lastY = p.screenY
this.icon?.style?.setProperty('transform', 'translate(' + x + 'px,' + y + 'px)') this.icon?.style?.setProperty('transform', 'translate(' + x + 'px,' + y + 'px)')
}, },
onTouchEnd(e : TouchEvent) { onTouchEnd(e : TouchEvent) {
if (e.touches.length == 0) { if (e.touches.length == 0) {
this.resetIcon() this.resetIcon()
this.touchEvent = null this.touchEvent = null
} }
}, },
onTouchCancel(_ : TouchEvent) { onTouchCancel(_ : TouchEvent) {
this.resetIcon() this.resetIcon()
this.touchEvent = null this.touchEvent = null
}, },
resetIcon() { resetIcon() {
this.move = false; this.move = false;
this.posX = 0; this.posX = 0;
this.posY = 0; this.posY = 0;
this.icon?.style?.setProperty('transform', 'translate(0px,0px)') this.icon?.style?.setProperty('transform', 'translate(0px,0px)')
} }
} }
} }
</script> </script>
<style> <style>
.container { .container {
width: 100%; width: 100%;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
} }
.view-box { .view-box {
width: 300px; width: 300px;
height: 300px; height: 300px;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
border-style: solid; border-style: solid;
} }
.icon { .icon {
width: 100px; width: 100px;
height: 100px; height: 100px;
} }
.msg-text { .msg-text {
margin-bottom: 100px; margin-bottom: 100px;
} }
.title1 { .title1 {
margin-top: 10px; margin-top: 10px;
font-size: 18px; font-size: 18px;
} }
.title2 { .title2 {
margin-top: 5px; margin-top: 5px;
font-size: 16px; font-size: 16px;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册