gif 可以右键在新的标签页中打开查看
简单实现类似这种的 滑动菜单 新手Swift新手 大神就别看了
import UIKit
class HomeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate {
let WIDTH = UIScreen.mainScreen().bounds.width / 375 let HEIGTH = UIScreen.mainScreen().bounds.height / 667 let SCREENWIDTH = UIScreen.mainScreen().bounds.width let SCREENHEIGTH = UIScreen.mainScreen().bounds.height var collectionView: UICollectionView!
var buttonsArray: NSMutableArray!
var titlesArray: NSMutableArray!
var lineView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.navigationController?.automaticallyAdjustsScrollViewInsets = false
self.initButtons()
self.initCollectionView()
}
func initButtons() {
buttonsArray = NSMutableArray()
titlesArray = ["分类","品牌","首页","专题","品牌"]
for var i = 0;i<titlesArray.count;i++ { let button = UIButton.init(type: .System)
button.frame = CGRectMake(SCREENWIDTH * CGFloat (i) / 5.0,SCREENWIDTH / 5.0,40 * HEIGTH)
button.tag = i
button.backgroundColor = UIColor.blackColor()
button.setTitleColor(UIColor.lightGrayColor(),forState: .Normal)
if i == 2 {
button.titleLabel?.font = UIFont.systemFontOfSize(16.0 * WIDTH)
button.whiteColor(),0)">.Normal)
}
button.systemFontOfSize(15.0 * WIDTH)
button.setTitle(titlesArray[i] as? String,0)">.Normal)
button.addTarget(self,action: "click:",forControlEvents: .TouchUpInside)
self.view.addSubview(button)
buttonsArray.addObject(button)
}
lineView = UIView.init(frame: CGRectMake(15 * WIDTH + (SCREENWIDTH / 5.0) * 2,38 * HEIGTH,SCREENWIDTH / 5.0 - 30 * WIDTH,2 * HEIGTH))
lineView.whiteColor()
self.addSubview(lineView)
}
func click (btn: UIButton) {
for btt in buttonsArray {
btt.titleLabel?!.systemFontOfSize(15)
btt.Normal)
}
btn.systemFontOfSize(16 * WIDTH)
btn.Normal)
self.collectionView.scrollToItemAtIndexPath(NSIndexPath.init(forItem: btn.tag,inSection: 0),atScrollPosition: .None,animated: true)
UIView.animateWithDuration(0.3,animations: { () -> Void in
self.lineView.frame = CGRectMake(15 * WIDTH + (UIScreen.mainScreen().bounds.width / 5.0) * CGFloat (btn.tag),UIScreen.width / 5.0 - 30 * WIDTH,2)
}) {(Bool) -> Void in
}
}
func initCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .Horizontal
flowLayout.itemSize = CGSizeMake(UIScreen.width,0)">.height - 88 * HEIGTH)
flowLayout.minimumLineSpacing = 0
flowLayout.minimumInteritemSpacing = 0
collectionView = UICollectionView.init(frame: CGRectMake(0,40 * HEIGTH,0)">.height - 88 * HEIGTH),collectionViewLayout: flowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.pagingEnabled = true
collectionView.init(forRow: 2,animated: true)
self.addSubview(collectionView)
collectionView.registerClass(HomeCollectionViewCell.classForKeyedArchiver(),forCellWithReuseIdentifier: "hCell")
collectionView.registerClass(KindCollectionViewCell.registerClass(BrandCollectionViewCell.registerClass(SubjectCollectionViewCell.registerClass(GiftCollectionViewCell.item == 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("kCell",forIndexPath: indexPath) as! KindCollectionViewCell
weak var weakSelf = self
cell.initClosure({ (str) -> Void? in
// let url = "http://mobile.iliangcang.com/goods/goodsShare?a=b&page=1&count=10&coverId=1&cat_code=$&app_key=iPhone&v=3.0.0&sig=97E9576F-B96F-48FD-BE0E-CD84610BC975"
let common = CommonViewController()
// common.urlStr = url.stringByReplacingOccurrencesOfString("$",withString: str)
common.urlStr = str
common.hidesBottomBarWhenPushed = true
weakSelf!.pushViewController(common,animated: true)
return nil
})
return cell
}else if indexPath.item == 1 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("bCell",forIndexPath: indexPath) as! BrandCollectionViewCell
return cell
}else if indexPath.item == 2 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("hCell",forIndexPath: indexPath) as! HomeCollectionViewCell
cell.myClosure = { (content_id: String) -> Void in
let shopVC = ShopViewController()
let url = "http://mobile.com/goods/goodsList?a=b&list_id=$&count=10&page=1&app_key=iPhone&v=3.0.0&sig=97E9576F-B96F-48FD-BE0E-CD84610BC975"
shopVC.pushViewController(shopVC,animated: true)
}
return cell
}else if indexPath.item == 3 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("sCell",forIndexPath: indexPath) as! SubjectCollectionViewCell
return cell
}else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gCell",forIndexPath: indexPath) as! GiftCollectionViewCell
return cell
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
for btt in buttonsArray {
btt.Normal)
}
UIView.animateWithDuration(0.3) { () -> Void in
self.width / 5.0) * CGFloat (self.contentOffset.x / (UIScreen.width)),2)
}
let btn = buttonsArray.objectAtIndex(Int(self.x / UIScreen.width))
btn.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
原文链接:https://www.f2er.com/swift/321588.html