快速动画的问题

时间:2016-03-05 02:44:23

标签: ios xcode swift

我试图让一些按钮看起来像是在增长。有人能告诉我为什么这段代码只会在while循环完成后出现按钮吗?

var size = CGFloat(5)
    let b1 = UIButton()
    b1.backgroundColor = UIColor(red: 200, green: 0, blue: 0, alpha: 100)
    b1.setTitle("", forState: .Normal)
    b1.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 100), forState: .Normal)
    b1.frame = CGRectMake((view.frame.width/2)-105, (view.frame.height/2-105), CGFloat(size), CGFloat(size))
    b1.titleLabel?.font = UIFont(name: "Helvetica", size: 24)
    let b2 = UIButton()
    b2.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 100)
    b2.setTitle("", forState: .Normal)
    b2.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 100), forState: .Normal)
    b2.frame = CGRectMake((view.frame.width/2)+5, (view.frame.height/2)-105, CGFloat(size), CGFloat(size))
    b2.titleLabel?.font = UIFont(name: "Helvetica", size: 24)
    let b3 = UIButton()
    b3.backgroundColor = UIColor(red: 0, green: 200, blue: 0, alpha: 100)
    b3.setTitle("", forState: .Normal)
    b3.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 100), forState: .Normal)
    b3.frame = CGRectMake((view.frame.width/2)-105, view.frame.height/2+5, CGFloat(size), CGFloat(size))
    b3.titleLabel?.font = UIFont(name: "Helvetica", size: 24)
    let b4 = UIButton()
    b4.backgroundColor = UIColor(red: 200, green: 200, blue: 0, alpha: 100)
    b4.setTitle("", forState: .Normal)
    b4.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 100), forState: .Normal)
    b4.frame = CGRectMake((view.frame.width/2)+5, view.frame.height/2+5, CGFloat(size), CGFloat(size))
    b4.titleLabel?.font = UIFont(name: "Helvetica", size: 24)
    while (size < 100) {
        size+=1
        b1.frame = CGRectMake(CGFloat(view.frame.width/2)-105/*+CGFloat(100-size)*/, CGFloat(view.frame.height/2-105)+CGFloat(100-size), CGFloat(size), CGFloat(size))
        b2.frame = CGRectMake(CGFloat(view.frame.width/2)+5/*+CGFloat(100-size)*/, (view.frame.height/2)-105+(100-size), CGFloat(size), CGFloat(size))
        b3.frame = CGRectMake(CGFloat(view.frame.width/2)-105/*+CGFloat(100-size)*/, view.frame.height/2+5+(100-size), CGFloat(size), CGFloat(size))
        b4.frame = CGRectMake(CGFloat(view.frame.width/2)+5/*+CGFloat(100-size)*/, view.frame.height/2+5+(100-size), CGFloat(size), CGFloat(size))
        view.addSubview(b1)
        view.addSubview(b2)
        view.addSubview(b3)
        view.addSubview(b4)
    }'

3 个答案:

答案 0 :(得分:4)

UI更改在代码返回并且程序访问事件循环之后才会生效。您在函数调用中进行的所有UI更改都会保存起来,然后在下一次通过事件循环时应用。

你的while循环一直运行到最后,然后视图更改一次性呈现在屏幕上。

正如JYeh在他的回答中所说,你应该在家庭animateWithDuration中使用UIView动画方法。

答案 1 :(得分:1)

我建议阅读如何使用UIView动画:https://www.raywenderlich.com/76200/basic-uiview-animation-swift-tutorial

使用UIView.animateWithDuration函数调用而不是while循环。按钮出现的原因是因为while循环结束得太快以至于你看不到任何发生的事情,而且循环不应该被用来做动画。

答案 2 :(得分:1)

这个怎么样?

// make an array containing 4 CGRect values:
var frames = Array<CGRect>(count: 4, repeatedValue: CGRectNull)

// divide view into 4 parts: left and right, then divide left and right into 2 parts each, top and bottom
let (left, right) = view.bounds.divide( view.bounds.width * 0.5, fromEdge: .MinXEdge )
(frames[0], frames[2]) = left.divide( view.bounds.height * 0.5, fromEdge:.MinYEdge)
(frames[1], frames[3]) = right.divide( view.bounds.height * 0.5, fromEdge:.MinYEdge)

// iterate over the range 0 to 3
(0...3).forEach { // this block (function) has the current number as its input
                  // I didn't name the input, so Swift names it $0
    let b = UIButton( type: .System ) // make a standard button
    b.frame = frames[ $0 ] // give the button a frame
    /*
        ...Add code to customize your button `b` here
    */

    view.addSubview( b ) // add button to our view

    b.layer.addAnimation({ // add an animation to the button's layer.
                           // We will scale to current size from 5% size:
        let anim = CABasicAnimation( keyPath: "transform" )
        anim.fromValue = NSValue( CATransform3D: CATransform3DMakeScale( 0.05, 0.05, 1.0))
        anim.duration = 5.0 // animation takes 5.0 s, just for demo purposes
        return anim // the created animation, to be added
        }(), forKey: nil)
}