简体   繁体   English

如何使用循环来增加滚动视图的大小(swift3)

[英]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. 一旦分数达到3,滚动视图的高度就会增加。 How can I make this so every time the button is pressed the height increases by 50 for a infinite amount of times. 我怎样才能做到这一点,所以每次按下按钮时,高度都会无限次地增加50。

@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 不确定我也完全理解您的问题,但仅是对@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    
    }
}

Take a global variable as counter set it as 0. 将全局变量作为计数器,将其设置为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. 注意:内容大小增加,而不是框架高度增加,因此您将看到scrollview的滚动区域将增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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