尽管看到了类似的答案,但我不知道这里发生了什么。我已经制作了一个自定义UIImageview,它应该在创建后立即开始制作动画:
class HeaderAnimator: UIImageView {
var travelUp = Bool(false)
var img = UIImage()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
//init
//ALWAYS SAME COLOR - black with less opacity, just change shapes randomly via parent
self.image = img
UIView.animate(withDuration: 1,
delay: 0.2,
options: UIViewAnimationOptions.curveEaseIn,
animations: { () -> Void in
//animate
if(self.travelUp)
{
self.center.y += screenSize.height * (50/screenSize.height)
}
else {
self.center.y -= screenSize.height * (50/screenSize.height)
}
}, completion: { (finished) -> Void in
// ....
print("done")
self.removeFromSuperview()
})
然后我试图在另一个视图中的计时器上产生这些视图:
let rand = Double(arc4random_uniform(4))
var bgTimer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.animateInBg), userInfo: nil, repeats: true)
func animateInBg()
{
var randPos = CGPoint()
var upOrDown = Int(arc4random_uniform(UInt32(2)))
//print(upOrDown)
let h = HeaderAnimator(frame: CGRect(x: 0, y: 0, width: self.bounds.width * (40/self.bounds.width), height: self.bounds.width * (40/self.bounds.width)))
h.img = UIImage(named: "rocket")!
switch upOrDown {
case 0: //down
randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0.9)
h.travelUp = false
//h.transform = CGAffineTransform(scaleX: 1, y: -1)
case 1: //up
randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0)
h.travelUp = true
default:
randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0)
h.travelUp = true
}
h.center = CGPoint(x: self.bounds.width * 0.5, y: 0)
self.addSubview(h)
}
这些视图应该显示然后向上或向下移出视图然后删除。然而,在一次产生之后不久,我收到错误:CALayerInvalidGeometry CALayer bounds contains NaN: [nan nan; nan nan]
这是什么意思?创建这些视图我做错了什么?
答案 0 :(得分:2)
Per Apple,您不应该继承String was not recognized as a valid DateTime
。相反,他们建议您继承2017-06-11T08:10:06.2212339 03:00
^
missing sign offset here
并在那里进行绘图。
不要将图像视图用于自定义绘图。 TheUIImageView类可以 不使用draw(_ :)方法绘制其内容。仅使用图像视图 呈现图像。要进行涉及图像的自定义绘图,子类 UIView直接在那里画你的图像。