【问题标题】:Animated CGAffineTransform(rotate) of a UIBezier path that is under func draw()func draw() 下的 UIBezier 路径的动画 CGAffineTransform(rotate)
【发布时间】:2017-03-31 02:44:17
【问题描述】:

我想用 CGAffineTransform(rotationAngle:) 为 UIView 绘制下的路径制作动画。我找不到任何方法来做到这一点......

这是我的代码:

     public override func draw(_ rect: CGRect) {


             //Drawing a dot at the center

             let dotRadius = max(bounds.width/15, bounds.height/15)
             let dotWidth:CGFloat = 10
             let dotCenter = CGPoint(x:bounds.width/2, y: bounds.height/2)
             let dotStartAngle: CGFloat = 0
             let dotEndAngle: CGFloat = CGFloat(2 * M_PI) // π

             var path = UIBezierPath(arcCenter: dotCenter,
                                 radius: dotRadius/2,
                                 startAngle: dotStartAngle,
                                 endAngle: dotEndAngle,
                                 clockwise: true)

             path.lineWidth = dotWidth
             counterColor.setStroke()
             counterColor.setFill()
             path.stroke()
             path.fill()


             arrowLayer.frame = CGRect(x:bounds.width/2, y: bounds.height/2, width: bounds.width, height: bounds.height)


             arrow.path = UIBezierPath(rect: CGRect(x: 0, y:0 - 1, width: -250, height: 1)).cgPath
             arrow.backgroundColor = UIColor.red.cgColor
             arrow.fillColor = UIColor.clear.cgColor
             arrow.strokeColor = counterColor.cgColor
             arrow.anchorPoint = CGPoint(x:0.5, y:0.5)
             arrow.lineWidth = 3
             arrow.setAffineTransform(CGAffineTransform(rotationAngle:self.radians(arrowDegree)))
             arrowLayer.addSublayer(arrow)

             self.layer.addSublayer(arrowLayer)
         }         

这就是我想要做的:

    func rotateButton() {

            UIView.animate(withDuration: 1, animations: {

                self.arrow.setAffineTransform(CGAffineTransform(rotationAngle:self.radians(self.arrowDegree + 10)))

                self.setNeedsDisplay()


            })

        }        

我不知道我是否具体。如果需要更多信息,请告诉我。

【问题讨论】:

    标签: ios swift xcode animation core-graphics


    【解决方案1】:

    我建议您将动画与绘图分开。覆盖draw() 通常适用于自定义绘图,但不适用于任何类型的动画。

    根据分配给arrow 的属性,它看起来像是CAShapeLayer。这很好,因为这意味着两者之间已经有了一些分离。

    但是,在draw() 中添加子视图或子层并不是一个好主意。它应该用于绘图。否则每次重绘视图时这些东西都会被重新添加。在这种情况下,它不太明显,因为一遍又一遍地添加相同的图层。但还是应该避免。

    您仍然可以使用draw() 来渲染居中的点。但预计不会成为动画的一部分。

    至于旋转动画;除非我严重记错了底层机制,否则独立层(那些不支持视图的层(很可能是您创建的))并不关心 UIView 动画上下文。相反,您将使用 Core Animation 级别的 API 为它们设置动画。这可以通过更改CATransaction 中的属性来完成,或者通过从一个角度到另一个角度创建CABasicAnimation 并将其添加到图层中来完成。

    注意添加动画不会改变“模型”值(图层的属性),一旦动画完成,图层将呈现为它是在添加动画之前完成的。如果希望图层动画到一个值并留在那里,您将both添加动画并更新图层属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2010-12-25
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多