简体   繁体   中英

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

Right now every time the button is pressed a the score goes up. Once the score hits 3 the scrollviews height is increased. How can I make this so every time the button is pressed the height increases by 50 for a infinite amount of times.

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


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

            }}

Not sure i fully understand your question. Do you want to do something like this?

@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
}

Not sure i fully understand your question too, but only to complement @bughana answer

@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    
    }
}

Take a global variable as counter set it as 0.

var counter = 0

then add this function to the button action

func btnTempClicked(sender:UIButton) -> Void {

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

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

}

NB: Content size is increased, not the frame height, so you will see the scrollview's scrolling area will increase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM