簡體   English   中英

如何使用循環來增加滾動視圖的大小(swift3)

[英]How to use a loop to increase a scroll view size (swift3)

現在,每按一次按鈕,得分就會上升。 一旦分數達到3,滾動視圖的高度就會增加。 我怎樣才能做到這一點,所以每次按下按鈕時,高度都會無限次地增加50。

@IBAction func enterScore(_ sender: Any) {
        score += 1


            THESCROOL.contentSize.height = 1000
            if score >= 3 {
                THESCROOL.contentSize.height = 5000

            }}

不確定我是否完全理解您的問題。 你想做這樣的事情嗎?

@IBAction func enterScore(_ sender: Any) {
    score += 1

    // every time the button is pressed, the contentHeight is increased by 50
    THESCROOL.contentSize.height = THESCROOL.contentSize.height + 50
}

不確定我也完全理解您的問題,但僅是對@bughana答案的補充

@IBAction func enterScore(_ sender: Any) {
    //add score on button pressed
    score += 1    

    //just for every time the score is multiple of 3
    if 3 % 3 == 0
       //increase 50 with operator +=
       THESCROOL.contentSize.height += 50    
    }
}

將全局變量作為計數器,將其設置為0。

var counter = 0

然后將此功能添加到按鈕操作中

func btnTempClicked(sender:UIButton) -> Void {

    counter = counter+1
    let height = CGFloat(50*counter)

    scrlVw.contentSize = CGSize(width: scrlVw.frame.size.width, height: height)

}

注意:內容大小增加,而不是框架高度增加,因此您將看到scrollview的滾動區域將增加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM