local SlideView = class("SlideView", cc.UIView) function SlideView:ctor(data) SlideView.super.ctor(self) local ui = loadUI("res/ui/ui_common/SlideView.ui") self.ui = ui self:addChild(ui) --外部数据 self.data = data or {} --初始化数据(在前) self:initData() --初始化界面(在后) self:initView() end function SlideView:onEnter() SlideView.super.onEnter(self) --开启自滑动 self:timeToSlide() end function SlideView:initData() --当前item self.curItem = nil --排序值 self.maxIdx = 0 --items列表 self.items = {} --默认开启循环滑动 self.loopSlide = self.data.loopSlide == nil and true or self.data.loopSlide --默认开启自滑动 self.autoSlide = self.data.autoSlide == nil and true or self.data.autoSlide --默认隐藏左右箭头 self.leftOrRight = self.data.leftOrRight == nil and false or self.data.leftOrRight --点击按钮的回调 self.callBack = self.data.callBack end function SlideView:initView() --隐藏item self.ui.Items.Layout_CheckBox_Item:setVisible(false) --默认不显示背景 self.ui.Items.Image_ContentBg:setVisible(false) --注册触摸事件 self:registerTouch() --左右按钮 self:setLeftOrRight(self.leftOrRight) self.ui.Items.Button_Left:registerClick(handler(self, self.onClickLeft)) self.ui.Items.Button_Right:registerClick(handler(self, self.onClickRight)) end function SlideView:addItem(imagePath) --添加item local item = cc.ImageView:createNode() item:setAnchorPoint(cc.p(0, 0.5)) item:loadTexture(imagePath) self.ui.Items.Layout_Content:addChild(item, self.maxIdx) self.ui.Items.Layout_Content:requestDoLayout() self.ui.Items.Layout_Content:doLayout() --设置数据 item.imageIdx = self.maxIdx self.maxIdx = self.maxIdx + 1 table.insert(self.items, item) --更新区域大小 if #self.items > 0 then self.ui.Items.Layout_Info:setSize(self.items[1]:getSize()) --checkBox位置 self:setCheckBoxPercentagePos(50, 10) --左右按钮位置 self:setLeftRightPercentagePos(20, 50) end self:updateItems() end function SlideView:updateItems() local xDiff = 0 --更新显示 self.checkBoxs = {} self.ui.Items.Layout_CheckBox:removeAllChildren() for k,v in ipairs(self.items) do --当前显示 self.curItem = self.curItem or v --保证显示 v:setVisible(true) --CheckBox local checkBox = self.ui.Items.Layout_CheckBox_Item:getCopied() checkBox.Items = getUIItems(checkBox) self.ui.Items.Layout_CheckBox:addChild(checkBox) table.insert(self.checkBoxs, checkBox) checkBox.itemData = v end self.ui.Items.Layout_CheckBox:requestDoLayout() self.ui.Items.Layout_CheckBox:doLayout() self:runToCurItem(true) end function SlideView:updateCheckBox() if self.curItem then for _,v in ipairs(self.checkBoxs) do v.Items.CheckBox:setSelectedState(self.curItem.imageIdx == v.itemData.imageIdx) end end end function SlideView:updateItemsPos() if self.curItem then self.curItem:setPosition(cc.p(0, 0)) self.ui.Items.Layout_Content:setPosition(cc.p(0, 0)) local pos = self.curItem:getPosition() local ave = math.floor(#self.items / 2) if ave > 0 then local infoSize = self.ui.Items.Layout_Info:getSize() for _,v in ipairs(self.items) do local diff = v.imageIdx - self.curItem.imageIdx if diff > ave then diff = diff - #self.items end if diff < -ave then diff = diff + #self.items end v:setPosition(cc.p(pos.x + diff * infoSize.width, infoSize.height / 2)) end end end end function SlideView:registerTouch() --触摸开始 local onTouchBegan = function(touch) self:stopAllActions() if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then return end self.sPos = touch:getLocation() self.ui.Items.Layout_Content:stopAllActions() self.beganPos = self.ui.Items.Layout_Content:getPosition() end --触摸移动 local onTouchMove = function(touch) self:stopAllActions() if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 or not self.sPos then return end local mPos = touch:getLocation() local diff = mPos.x - self.sPos.x self.ui.Items.Layout_Content:stopAllActions() local infoSize = self.ui.Items.Layout_Info:getSize() local contentSize = self.ui.Items.Layout_Content:getSize() --每次滑动不能超过一张图 if diff > infoSize.width then diff = infoSize.width end if diff < -infoSize.width then diff = -infoSize.width end --是否循环滑动 if not self.loopSlide and self.curItem then if self.curItem.imageIdx == 0 and diff > 0 then diff = 0 end if self.curItem.imageIdx == self.maxIdx - 1 and diff < 0 then diff = 0 end end self.ui.Items.Layout_Content:setPositionX(self.beganPos.x + diff) end --触摸结束(或者取消) local onTouchEnd = function(touch) self:stopAllActions() if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 or not self.sPos then return end --判断滑动的距离 local ePos = touch:getLocation() local diff = ePos.x - self.sPos.x local infoSize = self.ui.Items.Layout_Info:getSize() --滑动超过10% if self.curItem and math.abs(diff) > infoSize.width * 0.1 then local checkIdx = nil if diff > 0 then if self.loopSlide or self.curItem.imageIdx > 0 then checkIdx = (self.curItem.imageIdx - 1 + self.maxIdx) % self.maxIdx end else if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx end end if checkIdx then self.curItem = self.items[checkIdx + 1] end else local checkIdx = self.curItem.imageIdx + 1 if checkIdx then if self.callBack then self.callBack(checkIdx) end end end self:runToCurItem() self:timeToSlide() end --注册事件 self.ui.Items.Layout_Info:registerTouchEvent(onTouchBegan, onTouchMove, onTouchEnd, onTouchEnd) end function SlideView:setCheckBoxPercentagePos(xp, yp) if xp and yp then local layoutSize = self.ui.Items.Layout_Info:getSize() self.ui.Items.Layout_CheckBox:setPosition(cc.p(layoutSize.width * xp / 100, layoutSize.height * yp / 100)) end end function SlideView:setCheckBoxImage(selectImage, unselectImage, xDiff) --选中图片 self.ui.Items.CheckBox:loadTextureFrontCross(selectImage) --背景图图片 self.ui.Items.CheckBox:loadTextureBackGround(unselectImage) local checkBoxSize = self.ui.Items.CheckBox:getSize() self.ui.Items.Layout_CheckBox_Item:setSize(cc.size(xDiff or (checkBoxSize.width + 10), checkBoxSize.height)) self:updateItems() end function SlideView:setAutoSlide(bAuto) if bAuto then self.autoSlide = true if self:getNumberOfRunningActions() == 0 then self:timeToSlide() end else self.autoSlide = false self:stopAllActions() end end function SlideView:setLoopSlide(bLoop) if bLoop then self.loopSlide = true else self.loopSlide = false end end function SlideView:setLeftRightPercentagePos(xp, yp) if xp and yp then local layoutSize = self.ui.Items.Layout_Info:getSize() self.ui.Items.Button_Left:setPosition(cc.p(layoutSize.width * xp / 100, layoutSize.height * yp / 100)) self.ui.Items.Button_Right:setPosition(cc.p(layoutSize.width * (100 - xp) / 100, layoutSize.height * yp / 100)) end end function SlideView:setLeftRightImage(leftImage, rightImage) --左 self.ui.Items.Button_Left:loadTextureNormal(leftImage) --右 self.ui.Items.Button_Right:loadTextureNormal(rightImage) end function SlideView:updateLeftOrRight() if self.loopSlide then self.ui.Items.Button_Left:setVisible(true) self.ui.Items.Button_Right:setVisible(true) else self.ui.Items.Button_Left:setVisible(self.curItem and self.curItem.imageIdx > 0) self.ui.Items.Button_Right:setVisible(self.curItem and self.curItem.imageIdx < self.maxIdx - 1) end end function SlideView:onClickLeft() playBtnEffect() self:stopAllActions() if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then return end local checkIdx = nil if self.loopSlide or self.curItem.imageIdx > 0 then checkIdx = (self.curItem.imageIdx - 1 + self.maxIdx) % self.maxIdx end if checkIdx then self.curItem = self.items[checkIdx + 1] end self:runToCurItem() self:timeToSlide() end function SlideView:onClickRight() playBtnEffect() self:stopAllActions() if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then return end local checkIdx = nil if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx end if checkIdx then self.curItem = self.items[checkIdx + 1] end self:runToCurItem() self:timeToSlide() end function SlideView:setLeftOrRight(bLeftOrRight) if bLeftOrRight then self.leftOrRight = true self:updateLeftOrRight() else self.leftOrRight = false end self.ui.Items.Layout_LeftOrRight:setVisible(self.leftOrRight) end function SlideView:timeToSlide() if not self.autoSlide then return end self:runAction( cc.Sequence:create( cc.DelayTime:create(2.0), cc.CallFunc:create(function() if self.maxIdx == 0 then return end local checkIdx = nil if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx end if checkIdx then self.curItem = self.items[checkIdx + 1] self:runToCurItem() self:timeToSlide() end end) ) ) end function SlideView:runToCurItem(immediately) if self.curItem then local contentPos = self.ui.Items.Layout_Content:getPosition() self.ui.Items.Layout_Content:stopAllActions() local rePos = self.ui.Items.Layout_Info:convertToNodeSpace(self.curItem:getWorldPosition()) self.ui.Items.Layout_Content:runAction( cc.Sequence:create( cc.MoveTo:create(immediately and 0 or 0.15, cc.p(contentPos.x - rePos.x, contentPos.y)), cc.CallFunc:create(function() self:updateItemsPos() end) ) ) self:updateCheckBox() self:updateLeftOrRight() end end return SlideView