简体   繁体   English

animateWithDuration问题

[英]Issues with animateWithDuration

Like many others I'm struggling with getting animateWithDuration to work. 像许多其他人一样,我也在努力使animateWithDuration工作。 The code below is a little test I was trying to run. 下面的代码是我试图运行的一个小测试。 What I want todo is to add a UIView behind, fade the UIView on top and then remove it. 我想做的是在后面添加一个UIView,在顶部褪色UIView,然后将其删除。 The problem is that the fade animation doesn't last the entire 10 second length (it actually completes instantly) despite the completion block executing 10 seconds later as desired. 问题在于,尽管按要求在10秒钟后执行了完成块,但淡入淡出动画并不会持续整个10秒的时间(实际上是立即完成)。

Does anyone have any ideas what I'm doing wrong. 有谁知道我在做什么错。 I'm a beginner so I'm sorry in advance if I'm overlooked something very simple. 我是一个初学者,所以如果我忽略了一些非常简单的内容,请先抱歉。

Thanks! 谢谢!

import UIKit

class ViewController: UIViewController {

    @IBOutlet  var graphView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        graphView.addSubview(createView(UIColor.greenColor()))
    }

    func createView(color: UIColor) -> UIView {
        let viewToReturn = UIView(frame: CGRectMake(0, 0, 320, 354))
        viewToReturn.backgroundColor = color
        return viewToReturn
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func zoomIn() {

        let viewToRemove = graphView.subviews[0] as UIView

        graphView.addSubview(createView(UIColor.redColor()))

        UIView.animateWithDuration(10.0, animations: {
                println("Runs")
                viewToRemove.alpha = 0.0
            }, completion: {(bool) in
                if bool {
                    viewToRemove.removeFromSuperview()
                    println("End")
                }
        })

    }

}

You are adding the new subview on the top of the old subview . 您要在旧subview的顶部添加新subview subview When you call addSubView function, the new subview is added on top of the old. 调用addSubView函数时,新的子视图将添加到旧的子视图上。 This is why the the animation change is not visible. 这就是为什么动画更改不可见的原因。 Try adding 尝试添加

self.view.bringSubviewToFront(viewToRemove)

before starting the animation. 在开始动画之前。

Or you can directly add your new subview below the old using. 或者,您可以直接在旧使用下面添加新的子视图。

self.view.insertSubview(createView(UIColor.redColor()), belowSubview: viewToRemove)

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

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