Swift 類似網(wǎng)易新聞的滑動菜單

gif.gif

gif 可以右鍵在新的標(biāo)簽頁中打開查看

簡單實現(xiàn)類似這種的 滑動菜單 新手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, 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.setTitleColor(UIColor.whiteColor(), forState: .Normal)
                
            }
            button.titleLabel?.font = UIFont.systemFontOfSize(15.0 * WIDTH)
            button.setTitle(titlesArray[i] as? String, forState: .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.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(lineView)
        
    }
    func click (btn: UIButton) {
        
        for btt in buttonsArray {
            
            btt.titleLabel?!.font = UIFont.systemFontOfSize(15)
            btt.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        }
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(16 * WIDTH)
        btn.setTitleColor(UIColor.whiteColor(), forState: .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), 38 * HEIGTH, UIScreen.mainScreen().bounds.width / 5.0 - 30 * WIDTH, 2)
            
            }) {(Bool) -> Void in
                
            }
    }
    
    func initCollectionView() {
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
        flowLayout.itemSize = CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 88 * HEIGTH)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
        collectionView = UICollectionView.init(frame: CGRectMake(0, 40 * HEIGTH, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 88 * HEIGTH), collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.pagingEnabled = true
        collectionView.scrollToItemAtIndexPath(NSIndexPath.init(forRow: 2, inSection: 0), atScrollPosition: .None, animated: true)
        self.view.addSubview(collectionView)
        collectionView.registerClass(HomeCollectionViewCell.classForKeyedArchiver(), forCellWithReuseIdentifier: "hCell")
        collectionView.registerClass(KindCollectionViewCell.classForKeyedArchiver(), forCellWithReuseIdentifier: "kCell")
        collectionView.registerClass(BrandCollectionViewCell.classForKeyedArchiver(), forCellWithReuseIdentifier: "bCell")
        collectionView.registerClass(SubjectCollectionViewCell.classForKeyedArchiver(), forCellWithReuseIdentifier: "sCell")
        collectionView.registerClass(GiftCollectionViewCell.classForKeyedArchiver(), forCellWithReuseIdentifier: "gCell")
        
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 5
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        if indexPath.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!.navigationController?.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.iliangcang.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.urlStr = url.stringByReplacingOccurrencesOfString("$", withString: content_id)
                self.navigationController?.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.titleLabel?!.font = UIFont.systemFontOfSize(15)
            btt.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        }
       
        UIView.animateWithDuration(0.3) { () -> Void in
            
        self.lineView.frame = CGRectMake(15 * WIDTH + (UIScreen.mainScreen().bounds.width / 5.0) * CGFloat (self.collectionView.contentOffset.x / (UIScreen.mainScreen().bounds.width)), 38 * HEIGTH, UIScreen.mainScreen().bounds.width / 5.0 - 30 * WIDTH, 2)
        }
        
        let btn = buttonsArray.objectAtIndex(Int(self.collectionView.contentOffset.x / UIScreen.mainScreen().bounds.width))
        
        btn.titleLabel?!.font = UIFont.systemFontOfSize(16 * WIDTH)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市隘马,隨后出現(xiàn)的幾起案子名段,更是在濱河造成了極大的恐慌乏苦,老刑警劉巖抹沪,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件说铃,死亡現(xiàn)場離奇詭異归粉,居然都是意外死亡共螺,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門葛假,熙熙樓的掌柜王于貴愁眉苦臉地迎上來障陶,“玉大人,你說我怎么就攤上這事桐款。” “怎么了夷恍?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵魔眨,是天一觀的道長媳维。 經(jīng)常有香客問我,道長遏暴,這世上最難降的妖魔是什么侄刽? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮朋凉,結(jié)果婚禮上州丹,老公的妹妹穿的比我還像新娘。我一直安慰自己杂彭,他們只是感情好墓毒,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著亲怠,像睡著了一般所计。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上团秽,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天主胧,我揣著相機與錄音,去河邊找鬼习勤。 笑死踪栋,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的图毕。 我是一名探鬼主播夷都,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吴旋!你這毒婦竟也來了损肛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤荣瑟,失蹤者是張志新(化名)和其女友劉穎治拿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體笆焰,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡劫谅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了嚷掠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捏检。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖不皆,靈堂內(nèi)的尸體忽然破棺而出贯城,到底是詐尸還是另有隱情,我是刑警寧澤霹娄,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布能犯,位于F島的核電站鲫骗,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏踩晶。R本人自食惡果不足惜执泰,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望渡蜻。 院中可真熱鬧术吝,春花似錦、人聲如沸茸苇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽税弃。三九已至纪岁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間则果,已是汗流浹背幔翰。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留西壮,地道東北人遗增。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像款青,于是被迫代替她去往敵國和親做修。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫抡草、插件饰及、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評論 4 62
  • 終于見到谷應(yīng)老師燎含。 意料之中,意料之外腿短。 什么也不用說屏箍,她坐在那里,微微一笑橘忱,世界就安靜了赴魁。 看她從容優(yōu)雅仿佛冰肌...
    時光蜜糖閱讀 601評論 5 50
  • 劉備是個什么樣的人物,一直是大家所爭議的钝诚。- 就連魯迅也說演義上描述的劉備“似偽”颖御。- 于是又有很多人就以為抓住了...
    明鴛閱讀 459評論 1 3
  • 二娃去WWA
    bullock閱讀 194評論 0 1