Swift中的简单线条绘制动画

Swift中的简单线条绘制动画,swift,swift2,core-animation,drawrect,uibezierpath,Swift,Swift2,Core Animation,Drawrect,Uibezierpath,我是swift的新手,对我遇到的这个问题感到沮丧 我想做的很简单:在屏幕的中心画一条直线并设置动画。听起来很简单,对吧?是 啊这就是我目前所拥有的 导入UIKit 类图形:UIView{ 重写func drawRect(rect:CGRect){ 让屏幕大小:CGRect=UIScreen.mainScreen().bounds 设screenWidth=screensize.width 设screenHeight=screensize.height //上下文 let context=UIGr

我是swift的新手,对我遇到的这个问题感到沮丧

我想做的很简单:在屏幕的中心画一条直线并设置动画。听起来很简单,对吧?是 啊这就是我目前所拥有的

导入UIKit
类图形:UIView{
重写func drawRect(rect:CGRect){
让屏幕大小:CGRect=UIScreen.mainScreen().bounds
设screenWidth=screensize.width
设screenHeight=screensize.height
//上下文
let context=UIGraphicsGetCurrentContext()
CGContextSetLineWidth(上下文,7.0)
CGContextSetStrokeColorWithColor(上下文,UIColor.whiteColor().CGColor)
//路径
CGContextMoveToPoint(上下文、屏幕宽度、屏幕高度*0.5)
CGContextAddLineToPoint(上下文,屏幕宽度*0.5,屏幕高度*0.5)
CGContextSetLineCap(上下文,CGLineCap.Round)
//画
CGContextStrokePath(上下文)
}
}
我问这个问题的原因是我发现我无法设置它的动画,因为它在drawRect函数中。这是真的吗?需要你们的解释。谢谢


你不应该像这样在
drawRect
中绘制形状,尤其是当你要制作动画时

相反,制作一个CAShapeLayer,您可以使用核心动画为其设置动画

// Create the path using UIBezierPath.
// Position can start at 0,0 because we'll move it using the layer
let linePath = UIBezierPath()
linePath.moveToPoint(CGPoint(x: 0, y: 0))
linePath.addLineToPoint(CGPoint(x: CGRectGetMidX(frame), y: 0))

// Create the shape layer that will draw the path
let lineLayer = CAShapeLayer()

// set the bounds to the size of the path. 
// You could also set to the size of your view, 
// but if i know the size of my shape ahead of time, 
// I like to set it to be the same.
lineLayer.bounds = linePath.bounds

// give it the same position as our view's layer.
// layers have an anchor point of 0.5, 0.5 (their center)
// so this will put the layer in the center of the view.
lineLayer.position = layer.position

// finally, set up the shape properties. set the path and stroke etc
lineLayer.path = linePath.CGPath
lineLayer.strokeColor = UIColor.whiteColor().CGColor
lineLayer.lineWidth = 4

// add it to your view's layer and you're done!
layer.addSublayer(lineLayer)

现在,您可以使用核心动画在
lineLayer
上为属性设置动画。

您不应该像这样在
drawRect
中绘制形状,尤其是如果要设置动画

相反,制作一个CAShapeLayer,您可以使用核心动画为其设置动画

// Create the path using UIBezierPath.
// Position can start at 0,0 because we'll move it using the layer
let linePath = UIBezierPath()
linePath.moveToPoint(CGPoint(x: 0, y: 0))
linePath.addLineToPoint(CGPoint(x: CGRectGetMidX(frame), y: 0))

// Create the shape layer that will draw the path
let lineLayer = CAShapeLayer()

// set the bounds to the size of the path. 
// You could also set to the size of your view, 
// but if i know the size of my shape ahead of time, 
// I like to set it to be the same.
lineLayer.bounds = linePath.bounds

// give it the same position as our view's layer.
// layers have an anchor point of 0.5, 0.5 (their center)
// so this will put the layer in the center of the view.
lineLayer.position = layer.position

// finally, set up the shape properties. set the path and stroke etc
lineLayer.path = linePath.CGPath
lineLayer.strokeColor = UIColor.whiteColor().CGColor
lineLayer.lineWidth = 4

// add it to your view's layer and you're done!
layer.addSublayer(lineLayer)

现在,您可以使用核心动画在
lineLayer
上设置属性动画。

您不应该访问屏幕或其大小。使用
self.bounds
确定在哪里绘制(不要在这些边界之外绘制)。
drawRect:
用于“一次”(您可以使其重新绘制)执行视图的绘制,并且局部坐标系有效。如果您没有看到Apple的注释(此时不重要):
rect
参数通常与边界相等。它可能是一个子集,上面写着“你只需要画一部分画面。重画所有画面是很常见的(除非你有一个复杂的、昂贵的视图)。你不应该访问屏幕或它的大小。使用
self.bounds
来确定在哪里画(不要画在那些界限之外)。
drawRect:
使用“一次”(您可以使其重新绘制)执行视图的绘制,并且局部坐标系生效。如果您没有看到Apple的注释(此时不重要):
rect
参数通常相当于边界。它可能是一个子集,表示“您只需要绘制框架的一部分。重新绘制所有框架是常见的。”(除非你有一个复杂的,昂贵的观点)谢谢你!真的帮了我很多谢谢你!真的帮了我很多