scrollView滑动到指定坐标点_skybeauty_新浪博客

function scrollViewToDestination(destPos,time)
local visibleSize = cc.Director:getInstance():getVisibleSize() --屏幕可视区域大小
local duration = time or 0 --运动时间
local isAction = false --移动的时候是否跑action
local scrollView = self.scrollView --当前要操作的scrollView对象
local viewSize = scrollView:getContentSize() --scrollView的可视size即:viewSize
local container = scrollView:getInnerContainer() --scrollView的容器
local containerSize = container:getContentSize() --容器size
local viewPos = container:convertToNodeSpace(cc.p(viewSize.width*0.5,viewSize.height*0.5)) --scrollView可视区域正中央位置在container坐标系中的映射
local curPos = cc.p(container:getPosition()) --container当前的坐标位置
local endPos = cc.p(0,0) --container最终位置
local percent = cc.p(0,0) --container移动的百分比
if destPos.x<=1 then
destPos.x = destPos.x*containerSize.width
end
if destPos.y<=1 then 
destPos.y = destPos.y*containerSize.width
end
if duration >0 then 
isAction = true
end

endPos.x = curPos.x + viewPos.x -destPos.x
endPos.y = curPos.y + viewPos.y -destPos.y

if endPos.x < viewSize.width - containerSize.width then 
endPos.x = viewSize.width-containerSize.width
elseif endPos.x >0 then 
endPos.x = 0
end

if endPos.y < viewSize.height - containerSize.height then 
endPos.y = viewSize.height - containerSize.height
elseif endPos.y >0 then 
endPos.y = 0
end

local subPos = cc.pSub(curPos,endPos)
if math.abs(subPos.x)<0.05 and math.abs(subPos.y)<0.05 then 
--滑动目的地和当前位置坐标的移动范围检测
return
end

percent.x = 100.0 * math.abs(endPos.x)/(containerSize.width-viewSize.width)
percent.y = 100.0 * math.abs(endPos.y)/(containerSize.height-viewSize.height)

if visibleSize.width>=containerSize.width then 
percent.x = 0
end

if visibleSize.height>= containerSize.height then 
percent.y = 0
end

if isAction then 
scrollView:scrollToPercentBothDirection(percent,duration,isAction)
else
scrollView:jumpToPercentBothDirection(percent)
end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值