【发布时间】:2015-03-12 19:43:46
【问题描述】:
我在情节提要中创建了一个 UIView,并链接到一个名为“XXX”的自定义 UIView 类。在drawRect中我写了代码来填充XXX的颜色。从我的视图控制器我正在尝试为 UIView 设置动画。正在填充 XXX(UIView) 中的颜色,但没有动画。这是我的代码。
class XXX: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override func drawRect(rect: CGRect) {
var context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextClearRect(context, rect)
let color = UIColor.greenColor().CGColor
CGContextSetFillColorWithColor(context, color)
CGContextAddRect(context, rect)
CGContextFillPath(context)
}
myviewcontroller.swift
@IBOutlet var xxxView: XXX!
override func viewDidLoad() {
super.viewDidLoad()
XXX.animateWithDuration(1.0, delay: 0, options:
UIViewAnimationOptions.Repeat, animations: { () -> Void in
XXX.setAnimationDuration(1.0)
self.xxxView = XXX (frame: CGRectMake(0, 0, 100, 100))
},completion: nil);
}
我在视图控制器动画中犯了一些错误。想不通。那么,如何为自定义 UIView 设置动画?或者如何对drawrect的内容进行动画处理?
注意:drawRect 被调用并且 rect 被颜色填充但没有动画。 我之前的方法只是在控制器中创建一个 UIView 并通过动画增加宽度。但是,它没有被团队接受。像下面这样的东西,工作正常。
var xxxView = UIView()
xxxView.frame = CGRectMake(0, 10, startingPoint, 20)
xxxView.backgroundColor = UIColor.greenColor()
UIView.animateWithDuration(1.0, delay: 0, options:
UIViewAnimationOptions.Repeat, animations: { () -> Void in
UIView.setAnimationDuration(3.0)
let totalWidth: CGFloat = 50
let animationWidth: CGFloat = totalWidth - startingPoint
var frame : CGRect = xxxView.frame;
frame.size.width = (startingPoint + animationWidth)
xxxView.frame = frame;
},completion: nil);
谢谢!
【问题讨论】:
标签: ios objective-c swift animation uiview