segmentController
为了设计《好书》的分类栏,需要用Segment来切换不同的View;这里我们用到了AKSegmentController第三方库:
let buttonArray1 = [
["image":"ledger","title":"文学","font":MY_FONT],
["image":"drama masks","title":"人文社科","font":MY_FONT],
["image":"aperture","title":"生活","font":MY_FONT],
]
self.segmentController1 = AKSegmentedControl(frame: CGRectMake(10,60,SCREEN_WIDTH-20,37))
self.segmentController1?.initButtonWithTitleandImage(buttonArray1)
self.view.addSubview(self.segmentController1!)
首先创建一个按钮数组,再初始化segmentController,并添加进view中。
由于项目中有两个controller,为了避免两个controller的按钮同时按的情况,需要如下代码:
func segmentControllerAction(segment:AKSegmentedControl){
let index = segment.selectedIndexes.firstIndex
print(index)
if segment == self.segmentController1{
self.segmentController2?.setSelectedIndex(3)
}else{
self.segmentController1?.setSelectedIndex(3)
}
}
DropDownMenu
项目中需要点击后可以选择的下拉分类,这里我们用的是IGLDropDownMenu;
先建立点击不同segment后的DropDownMenu分类:
switch (index){
case 0:
self.createDropMenu(self.literatureArray1, array2: self.literatureArray2)
break
case 1:
self.createDropMenu(self.humanitiesArray1, array2: self.humanitiesArray2)
break
case 2:
self.createDropMenu(self.livelihoodArray1, array2: self.livelihoodArray2)
break
case 3:
self.createDropMenu(self.economiesArray1, array2: self.economiesArray2)
break
case 4:
self.createDropMenu(self.technologyArray1, array2: self.technologyArray2)
break
case 5:
self.createDropMenu(self.NetworkArray1, array2: self.NetworkArray2)
break
default:
break
}
其次建立并初始化DropDownMenu:
func createDropMenu(array1:Array<NSDictionary>,array2:Array<NSDictionary>){
let dropDownItem1 = NSMutableArray()
for var i = 0;i<array1.count;i++ {
let dict = array1[i]
let item = IGLDropDownItem()
item.text = dict["title"] as? String
dropDownItem1.addObject(item)
}
let dropDownItem2 = NSMutableArray()
for var i = 0;i<array2.count;i++ {
let dict = array2[i]
let item = IGLDropDownItem()
item.text = dict["title"] as? String
dropDownItem2.addObject(item)
}
self.dropDownMenu1?.removeFromSuperview()
self.dropDownMenu1 = IGLDropDownMenu()
self.dropDownMenu1?.menuText = "点我,展开详细列表"
self.dropDownMenu1?.menuButton.textLabel.adjustsFontSizeToFitWidth = true
self.dropDownMenu1?.menuButton.textLabel.textColor = RGB(38, g: 82, b: 67)
self.dropDownMenu1?.paddingLeft = 15
self.dropDownMenu1?.delegate = self
self.dropDownMenu1?.type = .Stack
self.dropDownMenu1?.itemAnimationDelay = 0.1
self.dropDownMenu1?.gutterY = 5
self.dropDownMenu1?.dropDownItems = dropDownItem1 as [AnyObject]
self.dropDownMenu1?.frame = CGRectMake(20, 150, SCREEN_WIDTH/2-30, (SCREEN_HEIGHT-200)/7)
self.view.addSubview(self.dropDownMenu1!)
self.dropDownMenu1?.reloadView()
self.dropDownMenu2?.removeFromSuperview()
self.dropDownMenu2 = IGLDropDownMenu()
self.dropDownMenu2?.menuText = "点我,展开详细列表"
self.dropDownMenu2?.menuButton.textLabel.adjustsFontSizeToFitWidth = true
self.dropDownMenu2?.menuButton.textLabel.textColor = RGB(38, g: 82, b: 67)
self.dropDownMenu2?.paddingLeft = 15
self.dropDownMenu2?.delegate = self
self.dropDownMenu2?.type = .Stack
self.dropDownMenu2?.itemAnimationDelay = 0.1
self.dropDownMenu2?.gutterY = 5
self.dropDownMenu2?.dropDownItems = dropDownItem2 as [AnyObject]
self.dropDownMenu2?.frame = CGRectMake(SCREEN_WIDTH/2+10, 150, SCREEN_WIDTH/2-30, (SCREEN_HEIGHT-200)/7)
self.view.addSubview(self.dropDownMenu2!)
self.dropDownMenu2?.reloadView()
}
不仅如此,还需实现一个dropdownmenu的代理,用于点击以后两边的menubutton均是需要的值:
func dropDownMenu(dropDownMenu: IGLDropDownMenu!, selectedItemAtIndex index: Int) {
if dropDownMenu == self.dropDownMenu1 {
let item = self.dropDownMenu1?.dropDownItems[index] as? IGLDropDownItem
self.detailType = (item?.text)!
self.dropDownMenu2?.menuButton.text = self.detailType
}else{
let item = self.dropDownMenu2?.dropDownItems[index] as? IGLDropDownItem
self.detailType = (item?.text)!
self.dropDownMenu1?.menuButton.text = self.detailType
}
}
网友评论