Lua Corona SDK中的捏合缩放比例限制

我已经在“各处”搜索了,但无法找到解决方案,因此在这里寻求帮助,我试图限制此代码的最大/最小缩放比例。目前你可以无限缩放,我想设置一个限制,限制用户可以缩放的程度,那么我该如何在这个代码中添加呢?

-- 激活多点触控功能
system.activate("multitouch")

-- 在屏幕上添加背景图片
local background = display.newImage("aquariumbackgroundIPhone.jpg", 0, 0)

local function calculateDelta(previousTouches, event)
    local id, touch = next(previousTouches)
    if event.id == id then
        id, touch = next(previousTouches, id)
        assert(id ~= event.id)
    end
    
    local dx = touch.x - event.x
    local dy = touch.y - event.y
    return dx, dy
end

-- 创建一个用于 bkgd 图像的表监听器对象
function background:touch(event)
    local result = true
    
    local phase = event.phase
    
    local previousTouches = self.previousTouches
    
    local numTotalTouches = 1
    if (previousTouches) then
        -- 将 previousTouches 中的触摸总量加上 numTotalTouches,如果事件已经存在于数组中,则减去 1
        numTotalTouches = numTotalTouches + self.numPreviousTouches
        if previousTouches[event.id] then
            numTotalTouches = numTotalTouches - 1
        end
    end
    
    if "began" == phase then
        -- 非常首要的“began”事件
        if (not self.isFocus) then
            -- 即使它们在 button 的 stageBounds 之外,后续的触摸事件仍将针对 button
            display.getCurrentStage():setFocus(self)
            self.isFocus = true
            
            previousTouches = {}
            self.previousTouches = previousTouches
            self.numPreviousTouches = 0
        elseif (not self.distance) then
            local dx, dy
            
            if previousTouches and (numTotalTouches) >= 2 then
                dx, dy = calculateDelta(previousTouches, event)
            end
            
            -- 初始化两个触摸之间的距离
            if (dx and dy) then
                local d = math.sqrt(dx * dx + dy * dy)
                if (d > 0) then
                    self.distance = d
                    self.xScaleOriginal = self.xScale
                    self.yScaleOriginal = self.yScale
                    print("distance = " .. self.distance)
                end
            end
        end
        
         if not previousTouches[event.id] then
            self.numPreviousTouches = self.numPreviousTouches + 1
         end
         previousTouches[event.id] = event
    
    elseif self.isFocus then
        if "moved" == phase then
            if (self.distance) then
                local dx, dy
                if previousTouches and (numTotalTouches) >= 2 then
                    dx, dy = calculateDelta(previousTouches, event)
                end
                
                if (dx and dy) then
                    local newDistance = math.sqrt(dx * dx + dy * dy)
                    local scale = newDistance / self.distance
                    print("newDistance(" .. newDistance .. ") / distance(" .. self.distance .. ") = scale(".. scale ..")")
                    if (scale > 0) then
                        self.xScale = self.xScaleOriginal * scale
                        self.yScale = self.yScaleOriginal * scale
                    end
                end
            end
            
            if not previousTouches[event.id] then
                self.numPreviousTouches = self.numPreviousTouches + 1
            end
            previousTouches[event.id] = event
            
        elseif "ended" == phase or "cancelled" == phase then
            if previousTouches[event.id] then
                self.numPreviousTouches = self.numPreviousTouches - 1
                previousTouches[event.id] = nil
            end
            
            if (#previousTouches > 0) then
                -- 必须至少有两个触摸点来进行捏/缩放
                self.distance = nil
            else
                -- previousTouches 为空,因此没有手指触摸屏幕
                -- 允许将触摸事件正常发送到它们“击中”的对象
                display.getCurrentStage():setFocus(nil)
                
                self.isFocus = false
                self.distance = nil
                self.xScaleOriginal = nil
                self.yScaleOriginal = nil
                
                -- 重置数组
                self.previousTouches = nil
                self.numPreviousTouches = nil
            end
        end
    end
    
    return result
end

-- 确定是否在 Corona 模拟器上运行
local isSimulator = "simulator" == system.getInfo("environment")

-- 在模拟器上不支持多点触控
if isSimulator then
    msg = display.newText("Multitouch not supported on Simulator!", 0, 20, "Verdana-Bold", 14)
    msg.x = display.contentWidth / 2 --将标题居中
    msg.y = display.contentHeight / 2 --将标题居中
    msg:setTextColor(255, 255, 0)
end

-- 注册表监听器
background:addEventListener("touch", background)

原文链接 https://stackoverflow.com/questions/6578706

点赞
stackoverflow用户400715
stackoverflow用户400715

你在这里写逻辑:

if ( scale > 0 ) then self.xScale = self.xScaleOriginal * scale self.yScale = self.yScaleOriginal * scale end

这里将原始的缩放比例乘以新的缩放比例。所以你需要做如下处理:

local xScale = self.xScaleOriginal * scale

local yScale = self.yScaleOriginal * scale

// 设置上限

xScale = math.min(ZOOMMAX, xScale)

yScale = math.min(ZOOMMAX, yScale)

// 设置下限

self.xScale = math.max(ZOOMMIN, xScale)

self.yScale = math.max(ZOOMMIN, yScale)`

2011-07-08 05:18:02