uni-app图片 设置浏览功能 实现双指放大缩小 拖动移动

首先,点击触发图片浏览界面

在浏览界面去实现图片移动和放缩

 

图片设置双指放大缩小这个功能相对来说比较简单的,是用于官方文档的路径(组件->视图容器->movable-area),movable-area可以做双指放大缩小,并且放大的同时可以左右移动图片(页面),至于怎么做呢?

用HBuilder X编辑器创建个uni-app项目

html代码

<template>
    <view>
        <view class="uni-padding-wrap uni-common-mt">
            <movable-area scale-area>
                <movable-view direction="all" @scale="onScale" scale="true" scale-min="1" scale-max="4" :scale-value="scale">
                    <image src="图片路径" mode="widthFix"></image>
                </movable-view>
            </movable-area>
        </view>
</view>
</template>

需要说一下movable-view标签里的 scale-min=“1” scale-max="4"是干嘛用的,这两个设置相对来说也比较重要
scale-min 拿图片来说,如果 scale-min=“0.5” 的话那图片就会显示50%,不会完全100%显示,所以就让他初始化等于 1
scale-max 他的意思是双指放大可以放大几倍,比如 scale-max=“4” 那么双指放大4倍
movable-area是不需要写任何js代码的

onScale是空函数

 

css代码

movable-view {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height:100%;
}

movable-area {
	height: 100%;
	width: 100%;
	position:fixed;
	overflow: hidden;
}
	
movable-view image{
	width:100%;
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在uni-app实现图片双指或双击放大缩小功能,你可以使用uni-app提供的`uni.createSelectorQuery()`和`canvas` API来实现。 具体实现步骤如下: 1. 在`<template>`标签中放置一个`<canvas>`标签,用于显示图片; 2. 在`<script>`标签中,使用`uni.createSelectorQuery()`方法获取`<canvas>`标签的上下文`canvasContext`; 3. 使用`canvasContext.drawImage()`方法绘制图片; 4. 监听`<canvas>`标签的`touchstart`、`touchmove`、`touchend`事件,分别处理双指缩放和双击放大的逻辑; 5. 在事件处理函数中,使用`canvasContext.scale()`方法实现双指缩放,使用`canvasContext.translate()`和`canvasContext.scale()`方法实现双击放大。 以下是示例代码: ``` <template> <canvas style="width:100%;height:100%;" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas> </template> <script> export default { data() { return { canvasContext: null, // canvas上下文 imgWidth: 0, // 图片宽度 imgHeight: 0, // 图片高度 imgScale: 1, // 图片缩放比例 lastTouchDistance: 0, // 上一次双指触摸距离 lastTouchTime: 0, // 上一次触摸时间 lastTouchX: 0, // 上一次触摸x坐标 lastTouchY: 0 // 上一次触摸y坐标 }; }, mounted() { // 获取canvas上下文 uni.createSelectorQuery().select('canvas').fields({ node: true, size: true }).exec((res) => { this.canvasContext = res[0].node.getContext('2d'); }); // 加载图片 let img = new Image(); img.src = '图片地址'; img.onload = () => { // 计算图片宽高和缩放比例 this.imgWidth = img.width; this.imgHeight = img.height; this.imgScale = Math.min(uni.upx2px(750) / this.imgWidth, uni.upx2px(750) / this.imgHeight); // 绘制图片 this.canvasContext.drawImage(img, 0, 0, this.imgWidth * this.imgScale, this.imgHeight * this.imgScale); }; }, methods: { touchStart(e) { if (e.touches.length === 2) { // 双指触摸,记录初始触摸距离 this.lastTouchDistance = Math.sqrt(Math.pow(e.touches[1].pageX - e.touches[0].pageX, 2) + Math.pow(e.touches[1].pageY - e.touches[0].pageY, 2)); } else if (e.touches.length === 1) { // 单指触摸,记录触摸时间和坐标 let currentTime = new Date().getTime(); if (currentTime - this.lastTouchTime < 300 && Math.abs(e.touches[0].pageX - this.lastTouchX) < 30 && Math.abs(e.touches[0].pageY - this.lastTouchY) < 30) { // 双击触摸,放大图片 this.canvasContext.translate((e.touches[0].pageX - this.imgWidth * this.imgScale / 2) * (1 - 1.5), (e.touches[0].pageY - this.imgHeight * this.imgScale / 2) * (1 - 1.5)); this.canvasContext.scale(1.5, 1.5); this.imgScale *= 1.5; this.lastTouchTime = 0; } else { // 单击触摸,记录触摸时间和坐标 this.lastTouchTime = currentTime; this.lastTouchX = e.touches[0].pageX; this.lastTouchY = e.touches[0].pageY; } } }, touchMove(e) { if (e.touches.length === 2) { // 双指触摸,计算触摸距离差值 let touchDistance = Math.sqrt(Math.pow(e.touches[1].pageX - e.touches[0].pageX, 2) + Math.pow(e.touches[1].pageY - e.touches[0].pageY, 2)); let distanceDelta = touchDistance - this.lastTouchDistance; if (distanceDelta !== 0) { // 缩放图片 let scaleDelta = distanceDelta / uni.upx2px(750) * 2; this.canvasContext.translate((e.touches[0].pageX + e.touches[1].pageX) / 2 - this.imgWidth * this.imgScale / 2, (e.touches[0].pageY + e.touches[1].pageY) / 2 - this.imgHeight * this.imgScale / 2); this.canvasContext.scale(1 + scaleDelta, 1 + scaleDelta); this.canvasContext.translate(this.imgWidth * this.imgScale / 2 - (e.touches[0].pageX + e.touches[1].pageX) / 2, this.imgHeight * this.imgScale / 2 - (e.touches[0].pageY + e.touches[1].pageY) / 2); this.imgScale *= 1 + scaleDelta; this.lastTouchDistance = touchDistance; } } }, touchEnd(e) { this.lastTouchDistance = 0; } } }; </script> ``` 注意,以上代码仅供参考,具体实现还需要根据实际情况进行调整。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值