1,在cpp下创建一个view分组,然后在里面创建一个MyButton.lua文件:
local MyButton={}
print("nihao")
function MyButton.new(params)
print("nihao-----")
local listener=params.listener
local button
params.listener=function(sender)
if params.preapare then
params.prepare()
end
local function zoom1(offset,time,onComplete)
local x,y=button:getPosition()
local size=cc.size(193,81)
local scaleX=button:getScaleX()*(size.width+offset)/size.width
local scaleY=button:getScaleY()*(size.height-offset)/size.height
transition.moveTo(button,{y=y-offset,time=time})
transition.scaleTo(button,{
scaleX=scaleX,
scaleY=scaleY,
time=time,
onComplete=onComplete,
})
end
local function zoom2(offset,time,onComplete)
local x,y=button:getPosition()
local size=cc.size(193,81)
transition.moveTo(button,{y=y+offset,time=time/2})
transition.scaleTo(button,{
scaleX=1.0,
scaleY=1.0,
time=time,
onComplete=onComplete,
})
end
-- button:getParent():setEnabled(false)--先关闭父类的button功能
print("button")
zoom1(40,0.08,function()
zoom2(40,0.09,function()
zoom1(20,0.10,function()
zoom2(20,0.1,function()
print("button")
-- button:getParent():setEnabled(true)
local tag=button:getTag()
listener(tag)
end)
end)
end)
end)
end
button=cc.ui.UIPushButton:new();
button:setButtonImage(cc.ui.UIPushButton.NORMAL, params.image, false)
button:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
if event.name=="ended" then
params.listener()
end
return true
end)
return button
end
return MyButton
2:引用自定义的Button:
local Button=require("app.view.MyButton")
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
function MainScene:ctor()
cc.ui.UILabel.new({
UILabelType = 2, text = "Hello, World", size = 64})
:align(display.CENTER, display.cx, display.cy)
:addTo(self)
local button=Button.new({
image="anniu-xuanzhong.png",
listener=function()
print("click")
end,
x=display.cx,
y=display.cy
})
button:setPosition(cc.p(320, 480))
self:addChild(button)
end
function MainScene:onEnter()
end
function MainScene:onExit()
end
return MainScene