Swift學(xué)習(xí)筆記(0.1---循環(huán)按鈕 ScrollView控制器的聯(lián)動(dòng))

循環(huán)按鈕+ScrollView處理點(diǎn)擊事件

目的:控制器的scroll聯(lián)動(dòng)上方按鈕的

3EE620DD-1708-4798-857E-B8AC14C3FFEF.png

按鈕的創(chuàng)建,這個(gè)按鈕的多少取決與需求科侈,可滑動(dòng)那就搞個(gè)ScrollView,多個(gè)聯(lián)動(dòng),不多說直接添代碼

fileprivate func setupTitleView() {
        var currentHeight: CGFloat = 64
        if view.frame.height == iPhoneXHeight {
            currentHeight += iPhoneXTopHeight
        }
        let titleView = UIView(frame: CGRect(x: 0, y: currentHeight, width: view.frame.width, height: 35))
        titleView.backgroundColor = UIColor.init(white: 1.0, alpha: 0.7)
        view.addSubview(titleView)
        
        for i in 0..<titles.count {
            let button = UIButton(type: .custom)
            button.tag = 100 + i
            button.setTitle(titles[i], for: .normal)
            button.setTitleColor(UIColor.darkGray, for: .normal)
            button.setTitleColor(UIColor.red, for: .disabled)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.addTarget(self, action: #selector(clickTitleView(_:)), for: .touchUpInside)
            let width = titleView.frame.width / CGFloat(titles.count)
            let height = titleView.frame.height
            button.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: height - 2)
//            button.layoutIfNeeded() //不能立即獲取button的label的frame 需要強(qiáng)制布局
            titleView.addSubview(button)
            if i == 0 {
                button.titleLabel?.sizeToFit()
                self.bottomView.frame = CGRect(x: 0, y: 0, width: button.titleLabel!.frame.width, height: 2)
                self.bottomView.center = CGPoint(x: button.center.x, y:button.frame.height + 1)
                titleView.addSubview(bottomView)
                clickTitleView(button)
            }
        }
    }

@objc fileprivate func clickTitleView(_ sender: UIButton) {
        currentButton.isEnabled = true
        currentButton = sender
        currentButton.isEnabled = false
        
        UIView.animate(withDuration: 0.25) {
            self.bottomView.frame = CGRect(x: 0, y: 0, width: self.currentButton.titleLabel!.frame.width, height: 2)
            self.bottomView.center = CGPoint(x: self.currentButton.center.x, y:self.currentButton.frame.height + 1)
        }
        
        var offSet = contentView.contentOffset
        offSet.x = CGFloat(sender.tag - 100) * contentView.frame.width
        contentView.setContentOffset(offSet, animated: true)
    }

UIScrollViewDelegate

extension ZYDEssenceViewController: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollViewDidEndScrollingAnimation(scrollView)
        let index = scrollView.contentOffset.x / scrollView.frame.width
        clickTitleView(view.viewWithTag(100 + Int(index)) as! UIButton)
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let index = scrollView.contentOffset.x / scrollView.frame.width
        let vc = childViewControllers[Int(index)]
        vc.view.frame = CGRect(x: index * contentView.frame.width, y: 0, width: contentView.frame.width, height: contentView.frame.height)
        contentView.addSubview(vc.view)
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末闭翩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子迄埃,更是在濱河造成了極大的恐慌疗韵,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,222評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件侄非,死亡現(xiàn)場離奇詭異蕉汪,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)逞怨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,455評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門者疤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人叠赦,你說我怎么就攤上這事驹马。” “怎么了除秀?”我有些...
    開封第一講書人閱讀 157,720評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵糯累,是天一觀的道長。 經(jīng)常有香客問我册踩,道長泳姐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,568評(píng)論 1 284
  • 正文 為了忘掉前任暂吉,我火速辦了婚禮胖秒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘慕的。我一直安慰自己扒怖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,696評(píng)論 6 386
  • 文/花漫 我一把揭開白布业稼。 她就那樣靜靜地躺著盗痒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上俯邓,一...
    開封第一講書人閱讀 49,879評(píng)論 1 290
  • 那天骡楼,我揣著相機(jī)與錄音,去河邊找鬼稽鞭。 笑死鸟整,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的朦蕴。 我是一名探鬼主播篮条,決...
    沈念sama閱讀 39,028評(píng)論 3 409
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吩抓!你這毒婦竟也來了涉茧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,773評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤疹娶,失蹤者是張志新(化名)和其女友劉穎伴栓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雨饺,經(jīng)...
    沈念sama閱讀 44,220評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡钳垮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,550評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了额港。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片饺窿。...
    茶點(diǎn)故事閱讀 38,697評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖移斩,靈堂內(nèi)的尸體忽然破棺而出肚医,到底是詐尸還是另有隱情,我是刑警寧澤叹哭,帶...
    沈念sama閱讀 34,360評(píng)論 4 332
  • 正文 年R本政府宣布忍宋,位于F島的核電站痕貌,受9級(jí)特大地震影響风罩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舵稠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,002評(píng)論 3 315
  • 文/蒙蒙 一超升、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧哺徊,春花似錦室琢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,782評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春巢钓,著一層夾襖步出監(jiān)牢的瞬間病苗,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,010評(píng)論 1 266
  • 我被黑心中介騙來泰國打工症汹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留硫朦,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,433評(píng)論 2 360
  • 正文 我出身青樓背镇,卻偏偏與公主長得像咬展,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子瞒斩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,587評(píng)論 2 350

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