提交 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)
})
})
......@@ -2,8 +2,9 @@
<scroll-view style="flex: 1">
<page-head title="拖拽图标测试相关事件"></page-head>
<view class="container">
<view class="view-box">
<image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel" @touchmove="onTouchMove" @touchend="onTouchEnd"></image>
<view class="view-box" @touchstart="onViewTouchStart">
<image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel"
@touchmove="onTouchMove" @touchend="onTouchEnd"></image>
</view>
</view>
......@@ -30,41 +31,55 @@
lastX: 0,
lastY: 0,
touchEvent: null as TouchEvent | null,
icon: null as UniElement | null
icon: null as UniElement | null,
touchTargets: "",
touchTargetsCount: 0,
iconRect : null as domRect | null
}
},
onReady() {
this.icon = uni.getElementById("icon")
this.iconRect = this.icon.getBoundingClientRect()
// 加上导航栏及状态栏高度
this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
},
methods: {
onTouchStart(e: TouchEvent) {
onViewTouchStart(e : TouchEvent) {
this.touchTargets += e.target?.tagName + e.currentTarget?.tagName
this.touchTargetsCount++
console.log(this.touchTargets, this.touchTargetsCount)
},
onTouchStart(e : TouchEvent) {
this.touchTargetsCount++
this.touchTargets += e.target?.tagName + e.currentTarget?.tagName
this.touchEvent = e
if(!this.move) {
if (!this.move) {
this.move = true
this.posX = e.touches[0].screenX
this.posY = e.touches[0].screenY
}
},
onTouchMove(e: TouchEvent) {
onTouchMove(e : TouchEvent) {
e.preventDefault()
this.touchEvent = e
let p = e.touches[0]
if(p.screenX == this.lastX && p.screenY == this.lastY){
if (p.screenX == this.lastX && p.screenY == this.lastY) {
return
}
let x = p.screenX-this.posX
let y = p.screenY-this.posY
let x = p.screenX - this.posX
let y = p.screenY - this.posY
this.lastX = p.screenX
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) {
if(e.touches.length == 0) {
onTouchEnd(e : TouchEvent) {
if (e.touches.length == 0) {
this.resetIcon()
this.touchEvent = null
}
},
onTouchCancel(_: TouchEvent) {
onTouchCancel(_ : TouchEvent) {
this.resetIcon()
this.touchEvent = null
},
......@@ -79,7 +94,6 @@
</script>
<style>
.container {
width: 100%;
flex-direction: column;
......@@ -112,5 +126,4 @@
margin-top: 5px;
font-size: 16px;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册