微信小程序 使用animation对象实现风水罗盘动画,任务需求: (1) 实现旋转动画效果; (2) 实现缩放动画效果; (3) 实现移动动画效果; (4) 实现倾斜动画效果; (5) 实现旋转和缩放动画同时效果; (6) 实现旋转后缩放动画效果。 实现手指触摸旋转罗盘,点击界面上的按钮来操纵罗盘

时间: 2023-11-18 12:04:18 浏览: 68
下面是实现手指触摸旋转罗盘,点击界面上的按钮来操纵罗盘的微信小程序示例代码: wxml文件: ``` <view class="container"> <view class="compass" animation="{{compassAnimation}}" bindtouchstart="startRotate" bindtouchmove="rotate" bindtouchend="stopRotate"> <image class="bg" src="/images/compass_bg.png"></image> <image class="pointer" src="/images/compass_pointer.png"></image> </view> <view class="control"> <button class="btn" bindtap="rotateLeft">左旋转</button> <button class="btn" bindtap="rotateRight">右旋转</button> <button class="btn" bindtap="rotateZoomIn">放大</button> <button class="btn" bindtap="rotateZoomOut">缩小</button> <button class="btn" bindtap="rotateMove">移动</button> <button class="btn" bindtap="rotateTilt">倾斜</button> <button class="btn" bindtap="rotateZoomAndRotate">旋转和缩放</button> <button class="btn" bindtap="rotateAfterZoom">旋转后缩放</button> </view> </view> ``` js文件: ``` Page({ data: { compassAnimation: {}, // 罗盘动画对象 startX: 0, // 手指起始位置的X坐标 startY: 0, // 手指起始位置的Y坐标 currentRotate: 0, // 当前罗盘的旋转角度 currentScale: 1, // 当前罗盘的缩放比例 currentTranslateX: 0, // 当前罗盘的X轴平移距离 currentTranslateY: 0, // 当前罗盘的Y轴平移距离 currentSkewX: 0, // 当前罗盘的X轴倾斜角度 currentSkewY: 0 // 当前罗盘的Y轴倾斜角度 }, // 开始旋转 startRotate(e) { this.setData({ startX: e.touches[0].clientX, startY: e.touches[0].clientY }); }, // 旋转 rotate(e) { let rotate = this.data.currentRotate + (e.touches[0].clientX - this.data.startX) / 2; this.setData({ compassAnimation: wx.createAnimation({ duration: 0, timingFunction: 'linear' }).rotate(rotate).export(), currentRotate: rotate, startX: e.touches[0].clientX, startY: e.touches[0].clientY }); }, // 停止旋转 stopRotate(e) { this.setData({ startX: 0, startY: 0 }); }, // 左旋转 rotateLeft() { let rotate = this.data.currentRotate - 30; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).rotate(rotate).export(), currentRotate: rotate }); }, // 右旋转 rotateRight() { let rotate = this.data.currentRotate + 30; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).rotate(rotate).export(), currentRotate: rotate }); }, // 放大 rotateZoomIn() { let scale = this.data.currentScale + 0.1; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).scale(scale).export(), currentScale: scale }); }, // 缩小 rotateZoomOut() { let scale = this.data.currentScale - 0.1; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).scale(scale).export(), currentScale: scale }); }, // 移动 rotateMove() { let translateX = this.data.currentTranslateX + 50; let translateY = this.data.currentTranslateY + 50; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).translate(translateX, translateY).export(), currentTranslateX: translateX, currentTranslateY: translateY }); }, // 倾斜 rotateTilt() { let skewX = this.data.currentSkewX + 30; let skewY = this.data.currentSkewY + 30; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).skew(skewX, skewY).export(), currentSkewX: skewX, currentSkewY: skewY }); }, // 旋转和缩放 rotateZoomAndRotate() { let rotate = this.data.currentRotate + 30; let scale = this.data.currentScale + 0.1; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).rotate(rotate).scale(scale).export(), currentRotate: rotate, currentScale: scale }); }, // 旋转后缩放 rotateAfterZoom() { let rotate = this.data.currentRotate + 30; let scale = this.data.currentScale + 0.1; this.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).rotate(rotate).export() }); let that = this; setTimeout(function() { that.setData({ compassAnimation: wx.createAnimation({ duration: 300, timingFunction: 'linear' }).scale(scale).export(), currentRotate: rotate, currentScale: scale }); }, 300); } }) ``` 上述示例代码实现了罗盘动画的旋转、缩放、移动、倾斜等效果,并且提供了按钮来操纵罗盘。同时,通过绑定触摸事件来实现手指触摸旋转罗盘的效果。

相关推荐

最新推荐

recommend-type

微信小程序 连续旋转动画(this.animation.rotate)详解

主要介绍了微信小程序 连续旋转动画(this.animation.rotate)详解的相关资料,需要的朋友可以参考下
recommend-type

微信小程序scroll-view组件实现滚动动画

主要为大家详细介绍了微信小程序scroll-view组件实现滚动动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

微信小程序支付之c#后台实现方法

主要介绍了微信小程序支付之c#后台实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
recommend-type

微信小程序实现翻牌抽奖动画

主要为大家详细介绍了微信小程序实现翻牌抽奖动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

微信小程序swiper组件实现抖音翻页切换视频功能的实例代码

主要介绍了微信小程序swiper组件实现抖音翻页切换视频功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。