【发布时间】:2017-06-21 03:05:33
【问题描述】:
谈到核心动画,我是个菜鸟。我正在尝试实现简单的动画,其中UILabel 从 A 点移动到 B 点。我有以下从动画代码示例中获得的代码,但我无法让它工作。标签根本不动。我做错了什么?
let frame = self.view.frame
let blueBox = UIView(frame: frame)
blueBox.backgroundColor = UIColor(red: 46/255, green: 83/255, blue: 160/255, alpha: 1.0)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
label.center = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
label.textAlignment = .center
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.layer.position = label.center
var attrsA = [NSFontAttributeName: UIFont(name: "LemonMilk", size: 92), NSForegroundColorAttributeName: UIColor.white]
var a = NSMutableAttributedString(string:"Hello\n", attributes:attrsA)
var attrsB = [NSFontAttributeName: UIFont(name: "LemonMilk", size: 38), NSForegroundColorAttributeName: UIColor.white]
var b = NSAttributedString(string:"World", attributes:attrsB)
a.append(b)
label.attributedText = a
let theAnimation = CABasicAnimation(keyPath: "position");
theAnimation.fromValue = [NSValue(cgPoint: CGPoint(x: screenWidth/2, y: screenHeight/2))]
theAnimation.toValue = [NSValue(cgPoint: CGPoint(x: 100.0, y: 100.0))]
theAnimation.duration = 3.0;
theAnimation.autoreverses = false //true - reverses into the initial value either smoothly or not
theAnimation.repeatCount = 2
blueBox.addSubview(label)
view.addSubview(blueBox)
label.layer.add(theAnimation, forKey: "animatePosition");
【问题讨论】:
-
使用
UIView.animate(withDuration:)可能会容易得多 -
@Pierce 但这并不能回答问题
-
@matt - 我知道这就是我没有回答的原因。我只是在 cmets 中提出建议
-
@matt - 你有什么问题?下车,再去读我的评论。我从来没有说过“不要那样做”,我只是提出了一种可能更容易让他们实现目标的方法的建议。他说他是新手。我没有给他一个不能解决他问题的答案,我也从来没有告诉他他不能做他正在尝试的事情。我所说的“可能会更容易”尝试另一种选择。
-
@matt - 您的评论暗示建议和寻找替代方案是不合理的 - 这本质上是错误的。
标签: ios swift uiview core-graphics core-animation