Vue实现苹果手表
Vue 实现苹果手表 UI
使用 Vue 实现苹果手表(Apple Watch)风格的 UI 可以通过以下步骤完成。重点在于模拟 Apple Watch 的圆形界面、数字表冠交互以及表盘动画效果。
创建圆形表盘组件
在 Vue 中创建一个圆形表盘组件,使用 CSS 实现圆形容器并添加动态时间显示:
<template>
<div class="watch-face">
<div class="time">{{ currentTime }}</div>
</div>
</template>
<script>
export default {
data() {
return {
currentTime: ''
}
},
mounted() {
this.updateTime()
setInterval(this.updateTime, 1000)
},
methods: {
updateTime() {
const now = new Date()
this.currentTime = now.toLocaleTimeString()
}
}
}
</script>
<style>
.watch-face {
width: 300px;
height: 300px;
border-radius: 50%;
background: #000;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 2rem;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
</style>
添加数字表冠交互
通过鼠标滚轮事件模拟 Apple Watch 的数字表冠滚动交互:
<template>
<div class="watch-container" @wheel="handleScroll">
<!-- 表盘内容 -->
</div>
</template>
<script>
export default {
methods: {
handleScroll(event) {
event.preventDefault()
const delta = Math.sign(event.deltaY)
// 根据滚动方向执行操作,如切换应用或调整设置
console.log('Digital Crown rotated:', delta)
}
}
}
</script>
实现应用网格布局
Apple Watch 的应用图标采用独特的蜂窝网格布局,可以通过 CSS Grid 实现:
.app-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 15px;
place-items: center;
}
.app-icon {
width: 50px;
height: 50px;
border-radius: 10px;
background: rgba(255,255,255,0.1);
display: flex;
justify-content: center;
align-items: center;
transition: transform 0.3s;
}
.app-icon:hover {
transform: scale(1.1);
}
添加触觉反馈
使用浏览器振动 API 模拟 Apple Watch 的触觉反馈:
methods: {
simulateHapticFeedback() {
if ('vibrate' in navigator) {
navigator.vibrate(50) // 50ms 振动
}
}
}
实现表盘切换动画
为不同表盘样式添加过渡动画:
.watch-face {
transition: transform 0.5s, opacity 0.5s;
}
.watch-face-changing {
transform: scale(0.9);
opacity: 0;
}
响应式设计考虑
确保 UI 在不同尺寸屏幕上都能保持圆形比例:
.watch-face {
width: 80vmin;
height: 80vmin;
max-width: 400px;
max-height: 400px;
}
完整组件示例
<template>
<div class="apple-watch-ui">
<div class="watch-face" :class="{ 'watch-face-changing': changing }">
<div class="time">{{ currentTime }}</div>
<div class="app-grid" v-if="showApps">
<div class="app-icon" v-for="(app, index) in apps" :key="index">
{{ app.name }}
</div>
</div>
</div>
<button @click="toggleApps">显示应用</button>
</div>
</template>
<script>
export default {
data() {
return {
currentTime: '',
changing: false,
showApps: false,
apps: [
{ name: '消息' },
{ name: '电话' },
{ name: '邮件' },
{ name: '健身' },
{ name: '音乐' },
{ name: '天气' }
]
}
},
mounted() {
this.updateTime()
setInterval(this.updateTime, 1000)
},
methods: {
updateTime() {
const now = new Date()
this.currentTime = now.toLocaleTimeString()
},
toggleApps() {
this.changing = true
setTimeout(() => {
this.showApps = !this.showApps
this.changing = false
}, 500)
}
}
}
</script>
通过以上方法,可以在 Vue 中创建一个基本的 Apple Watch UI 模拟界面,包含时间显示、应用网格和交互效果。

