提交 369b7e9c 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

合并 touch-events 示例

上级 22adf9e0
const PAGE_PATH = '/pages/component/global-events/touch-events'
describe('touch-events-test', () => {
// 先屏蔽 android 及 web 平台
if (process.env.uniTestPlatformInfo.startsWith('android') || process.env.uniTestPlatformInfo.startsWith('web')) {
it('other platform', () => {
expect(1).toBe(1)
})
return
}
let page
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor(500);
})
it('touchStart-tagName-touchCount', async () => {
let iconRect = await page.data('iconRect')
let x = iconRect.x + iconRect.width / 2.0
let y = iconRect.y + iconRect.height / 2.0
// 点击图片
await program.tap({
x: x,
y: y,
duration: 100
})
await page.waitFor(1500);
const touchTargets = await page.data('touchTargets')
const touchTargetsCount = await page.data('touchTargetsCount')
console.log('touchTargets', touchTargets)
console.log('touchTargetsCount', touchTargetsCount)
expect(touchTargets).toBe("IMAGEIMAGEIMAGEVIEW")
expect(touchTargetsCount).toBe(2)
})
})
<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"> <view class="view-box" @touchstart="onViewTouchStart">
<image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel" @touchmove="onTouchMove" @touchend="onTouchEnd"></image> <image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel"
</view> @touchmove="onTouchMove" @touchend="onTouchEnd"></image>
</view>
</view>
<view v-if="touchEvent !== null"> </view>
<text class="title1">touches: </text> <view v-if="touchEvent !== null">
<template v-for="(touch, index) in touchEvent!.touches" :key="index"> <text class="title1">touches: </text>
<text class="title2">touch[{{ index }}]:</text> <template v-for="(touch, index) in touchEvent!.touches" :key="index">
<text>identifier: {{touch.identifier}}</text> <text class="title2">touch[{{ index }}]:</text>
<text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text> <text>identifier: {{touch.identifier}}</text>
<text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text> <text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text>
<text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text> <text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text>
</template> <text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text>
</view> </template>
</scroll-view> </view>
</template> </scroll-view>
</template>
<script>
export default { <script>
data() { export default {
return { data() {
move: false, return {
posX: 0, move: false,
posY: 0, posX: 0,
lastX: 0, posY: 0,
lastY: 0, lastX: 0,
touchEvent: null as TouchEvent | null, lastY: 0,
icon: null as UniElement | null touchEvent: null as TouchEvent | null,
} icon: null as UniElement | null,
}, touchTargets: "",
onReady() { touchTargetsCount: 0,
iconRect : null as domRect | null
}
},
onReady() {
this.icon = uni.getElementById("icon") this.icon = uni.getElementById("icon")
}, this.iconRect = this.icon.getBoundingClientRect()
methods: { // 加上导航栏及状态栏高度
onTouchStart(e: TouchEvent) { this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
this.touchEvent = e },
if(!this.move) { methods: {
this.move = true onViewTouchStart(e : TouchEvent) {
this.posX = e.touches[0].screenX this.touchTargets += e.target?.tagName + e.currentTarget?.tagName
this.posY = e.touches[0].screenY this.touchTargetsCount++
} console.log(this.touchTargets, this.touchTargetsCount)
}, },
onTouchMove(e: TouchEvent) { onTouchStart(e : TouchEvent) {
e.preventDefault() this.touchTargetsCount++
this.touchEvent = e this.touchTargets += e.target?.tagName + e.currentTarget?.tagName
let p = e.touches[0]
if(p.screenX == this.lastX && p.screenY == this.lastY){ this.touchEvent = e
return if (!this.move) {
} this.move = true
let x = p.screenX-this.posX this.posX = e.touches[0].screenX
let y = p.screenY-this.posY this.posY = e.touches[0].screenY
this.lastX = p.screenX }
this.lastY = p.screenY },
this.icon?.style?.setProperty('transform', 'translate('+x+'px,'+y+'px)') onTouchMove(e : TouchEvent) {
}, e.preventDefault()
onTouchEnd(e: TouchEvent) { this.touchEvent = e
if(e.touches.length == 0) { let p = e.touches[0]
this.resetIcon() if (p.screenX == this.lastX && p.screenY == this.lastY) {
this.touchEvent = null return
} }
}, let x = p.screenX - this.posX
onTouchCancel(_: TouchEvent) { let y = p.screenY - this.posY
this.resetIcon() this.lastX = p.screenX
this.touchEvent = null this.lastY = p.screenY
}, this.icon?.style?.setProperty('transform', 'translate(' + x + 'px,' + y + 'px)')
resetIcon() { },
this.move = false; onTouchEnd(e : TouchEvent) {
this.posX = 0; if (e.touches.length == 0) {
this.posY = 0; this.resetIcon()
this.icon?.style?.setProperty('transform', 'translate(0px,0px)') this.touchEvent = null
} }
} },
} onTouchCancel(_ : TouchEvent) {
</script> this.resetIcon()
this.touchEvent = null
<style> },
resetIcon() {
.container { this.move = false;
width: 100%; this.posX = 0;
flex-direction: column; this.posY = 0;
align-items: center; this.icon?.style?.setProperty('transform', 'translate(0px,0px)')
} }
}
.view-box { }
width: 300px; </script>
height: 300px;
align-items: center; <style>
justify-content: center; .container {
border-style: solid; width: 100%;
} flex-direction: column;
align-items: center;
.icon { }
width: 100px;
height: 100px; .view-box {
} width: 300px;
height: 300px;
.msg-text { align-items: center;
margin-bottom: 100px; justify-content: center;
} border-style: solid;
}
.title1 {
margin-top: 10px; .icon {
font-size: 18px; width: 100px;
} height: 100px;
}
.title2 {
margin-top: 5px; .msg-text {
font-size: 16px; margin-bottom: 100px;
} }
.title1 {
margin-top: 10px;
font-size: 18px;
}
.title2 {
margin-top: 5px;
font-size: 16px;
}
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册