繁体   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