I have a loginViewController that pops up persistently when a user is not logged in. I would like to animate the background of this window. I have tried two methods, UIViewAnimateWithDuration
by adding a subview, and CABasicAnimation
by adding a sublayer. Neither animation is running. The sublayer and subview are being drawn but they're not animated.
The first animation is animateWithDuration
. It attempts to move the drawn rectangle to a random point on it's y-axis. The second animation is CABasicAnimation
for opacity, changing the alpha of the drawn rectangle from 1 to 0. It doesn't matter to me whether I use view animation or layer animation both would suffice but neither are clicking.
func makeBackground() -> UIView {
let backRect = UIView()
backRect.frame = self.view.bounds
backRect.backgroundColor = backgroundColor
let floater2 = UIView(frame: CGRectMake(25.0, 0.0, 25, 25))
floater2.backgroundColor = backgroundColor
floater2.layer.borderWidth = 1.0
floater2.layer.borderColor = lowColor.CGColor
floater2.alpha = 0.0
backRect.addSubview(floater2)
UIView.animateWithDuration(2.0, delay: 0.0, options: .Repeat, animations: { () -> Void in
let randomY = CGFloat(arc4random_uniform(568))
floater2.frame.origin.y = randomY
}, completion: nil)
let floater1 = CALayer()
floater1.frame = CGRectMake(0.0, 0.0, 25, 25)
floater1.backgroundColor = UIColor.redColor().CGColor
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 1.0
animation.toValue = 0.0
animation.repeatCount = 100
animation.duration = 2.0
floater1.addAnimation(animation, forKey: "opacity")
backRect.layer.addSublayer(floater1)
return backRect
}
And then I assign the result of the function to a variable when presenting the login view controller:
var loginViewController = PFLogInViewController()
loginViewController.delegate = self
loginViewController.fields = PFLogInFields.Default
| PFLogInFields.Facebook
| PFLogInFields.Twitter
loginViewController.logInView?.backgroundColor = backgroundColor
var bg = self.makeBackground()
loginViewController.logInView?.addSubview(bg)
loginViewController.logInView?.sendSubviewToBack(bg)
答案 0 :(得分:1)
此代码存在问题:
backRect.addSubview(floater2)
UIView.animateWithDuration(2.0, delay: 0.0, options: .Repeat, animations: { () -> Void in
floater2.frame.origin.y = randomY
...是你试图动画一些不属于渲染树的东西。您需要在addSubview
之后提供“心跳”,以便floater2
是部分的渲染树 - 也就是说,它实际上是 in 界面 - 在开始制作动画之前。短暂的延迟将会:
backRect.addSubview(floater2)
delay(0.1) {
UIView.animateWithDuration(2.0, delay: 0.0, options: .Repeat, animations: { () -> Void in
floater2.frame.origin.y = randomY
(其中delay
与我的效用函数相似或相似:https://stackoverflow.com/a/24318861/341994)。
图层动画的问题是并行的。在图层位于渲染树中之前,您无法为图层设置动画。事实上,由于视图是一个图层,并且由于视图动画实际上是图层动画,因此它的问题完全相同。