我有一个 loginViewController 会在用户未登录时持续弹出。我想为这个窗口的背景设置动画。我尝试了两种方法,UIViewAnimateWithDuration
添加子视图和CABasicAnimation
添加子层。两个动画都没有运行。正在绘制子图层和子视图,但它们没有动画。
第一个动画是animateWithDuration
。它试图将绘制的矩形移动到它的 y 轴上的一个随机点。第二个动画CABasicAnimation
用于不透明度,将绘制矩形的 alpha 从 1 更改为 0。我使用视图动画或图层动画都足够但都没有点击对我来说并不重要。
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
}
然后我在呈现登录视图控制器时将函数的结果分配给一个变量:
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)