Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
369b7e9c
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
369b7e9c
编写于
9月 02, 2024
作者:
DCloud_iOS_XHY
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
合并 touch-events 示例
上级
22adf9e0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
171 addition
and
114 deletion
+171
-114
pages/component/global-events/touch-events.test.js
pages/component/global-events/touch-events.test.js
+44
-0
pages/component/global-events/touch-events.uvue
pages/component/global-events/touch-events.uvue
+127
-114
未找到文件。
pages/component/global-events/touch-events.test.js
0 → 100644
浏览文件 @
369b7e9c
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
)
})
})
pages/component/global-events/touch-events.uvue
浏览文件 @
369b7e9c
<template>
<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>
</view>
<view v-if="touchEvent !== null">
<text class="title1">touches: </text>
<template v-for="(touch, index) in touchEvent!.touches" :key="index">
<text class="title2">touch[{{ index }}]:</text>
<text>identifier: {{touch.identifier}}</text>
<text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text>
<text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text>
<text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text>
</template>
</view>
</scroll-view>
</template>
<script>
export default {
data() {
return {
move: false,
posX: 0,
posY: 0,
lastX: 0,
lastY: 0,
touchEvent: null as TouchEvent | null,
icon: null as UniElement | null
}
},
onReady() {
<template>
<scroll-view style="flex: 1">
<page-head title="拖拽图标测试相关事件"></page-head>
<view class="container">
<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>
<view v-if="touchEvent !== null">
<text class="title1">touches: </text>
<template v-for="(touch, index) in touchEvent!.touches" :key="index">
<text class="title2">touch[{{ index }}]:</text>
<text>identifier: {{touch.identifier}}</text>
<text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text>
<text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text>
<text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text>
</template>
</view>
</scroll-view>
</template>
<script>
export default {
data() {
return {
move: false,
posX: 0,
posY: 0,
lastX: 0,
lastY: 0,
touchEvent: null as TouchEvent | null,
icon: null as UniElement | null,
touchTargets: "",
touchTargetsCount: 0,
iconRect : null as domRect | null
}
},
onReady() {
this.icon = uni.getElementById("icon")
},
methods: {
onTouchStart(e: TouchEvent) {
this.touchEvent = e
if(!this.move) {
this.move = true
this.posX = e.touches[0].screenX
this.posY = e.touches[0].screenY
}
},
onTouchMove(e: TouchEvent) {
e.preventDefault()
this.touchEvent = e
let p = e.touches[0]
if(p.screenX == this.lastX && p.screenY == this.lastY){
return
}
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)')
},
onTouchEnd(e: TouchEvent) {
if(e.touches.length == 0) {
this.resetIcon()
this.touchEvent = null
}
},
onTouchCancel(_: TouchEvent) {
this.resetIcon()
this.touchEvent = null
},
resetIcon() {
this.move = false;
this.posX = 0;
this.posY = 0;
this.icon?.style?.setProperty('transform', 'translate(0px,0px)')
}
}
}
</script>
<style>
.container {
width: 100%;
flex-direction: column;
align-items: center;
}
.view-box {
width: 300px;
height: 300px;
align-items: center;
justify-content: center;
border-style: solid;
}
.icon {
width: 100px;
height: 100px;
}
.msg-text {
margin-bottom: 100px;
}
.title1 {
margin-top: 10px;
font-size: 18px;
}
.title2 {
margin-top: 5px;
font-size: 16px;
}
this.iconRect = this.icon.getBoundingClientRect()
// 加上导航栏及状态栏高度
this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
},
methods: {
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) {
this.move = true
this.posX = e.touches[0].screenX
this.posY = e.touches[0].screenY
}
},
onTouchMove(e : TouchEvent) {
e.preventDefault()
this.touchEvent = e
let p = e.touches[0]
if (p.screenX == this.lastX && p.screenY == this.lastY) {
return
}
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)')
},
onTouchEnd(e : TouchEvent) {
if (e.touches.length == 0) {
this.resetIcon()
this.touchEvent = null
}
},
onTouchCancel(_ : TouchEvent) {
this.resetIcon()
this.touchEvent = null
},
resetIcon() {
this.move = false;
this.posX = 0;
this.posY = 0;
this.icon?.style?.setProperty('transform', 'translate(0px,0px)')
}
}
}
</script>
<style>
.container {
width: 100%;
flex-direction: column;
align-items: center;
}
.view-box {
width: 300px;
height: 300px;
align-items: center;
justify-content: center;
border-style: solid;
}
.icon {
width: 100px;
height: 100px;
}
.msg-text {
margin-bottom: 100px;
}
.title1 {
margin-top: 10px;
font-size: 18px;
}
.title2 {
margin-top: 5px;
font-size: 16px;
}
</style>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录