【问题标题】:corona sdk pinch to zoom电晕sdk捏缩放
【发布时间】:2015-04-24 23:35:31
【问题描述】:

我正在使用包含在 corona 示例代码中的函数来进行缩放。但由于我正在开发一个应用程序,我需要让每个感兴趣的地方都可以缩放。我是否必须为每张图片复制和粘贴非常长的功能,或者我可以将每张图片重定向到一个功能上?我是 lua 新手,这里是示例代码中包含的捏缩放功能:

function background:touch( event )
local result = true

local phase = event.phase

local previousTouches = self.previousTouches

local numTotalTouches = 1
if ( previousTouches ) then
    -- add in total from previousTouches, subtract one if event is already in the array
    numTotalTouches = numTotalTouches + self.numPreviousTouches
    if previousTouches[event.id] then
        numTotalTouches = numTotalTouches - 1
    end
end

if "began" == phase then
    -- Very first "began" event
    if ( not self.isFocus ) then
        -- Subsequent touch events will target button even if they are outside the contentBounds of 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

        -- initialize to distance between two touches
        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
            -- must be at least 2 touches remaining to pinch/zoom
            self.distance = nil
        else
            -- previousTouches is empty so no more fingers are touching the screen
            -- Allow touch events to be sent normally to the objects they "hit"
            display.getCurrentStage():setFocus( nil )

            self.isFocus = false
            self.distance = nil
            self.xScaleOriginal = nil
            self.yScaleOriginal = nil

            -- reset array
            self.previousTouches = nil
            self.numPreviousTouches = nil
        end
    end
end

return result

结束

【问题讨论】:

    标签: lua coronasdk pinch


    【解决方案1】:

    假设您的所有兴趣点都相似,您或许可以使用closure 来完成此操作。

    例子:

    local function createPOI(properties)
        local pointOfInterest = createPointOfInterest() -- code to create your object
        --set properties based on argument
        function pointOfInterest:touch(e)
            --do stuff
        end
        pointOfInterest:addEventListner("touch")
        return pointOfInterest
    end
    

    Corona 有一些代码here 可能有用。检查“多次触摸”部分。

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 1970-01-01
      • 2013-04-12
      • 2016-03-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      相关资源
      最近更新 更多