簡體   English   中英

在func draw()下的UIBezier路徑的動畫CGAffineTransform(rotate)

[英]Animated CGAffineTransform(rotate) of a UIBezier path that is under func draw()

我想在UIView的繪制下使用CGAffineTransform(rotationAngle :)對路徑進行動畫處理。 我找不到任何辦法...

這是我的代碼:

     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()


            })

        }        

我不知道我是否具體..告訴我是否需要更多信息。

我建議您將動畫與圖形分開。 覆蓋draw()通常適用於自定義繪圖,但不適用於任何類型的動畫。

根據分配給arrow的屬性,它看起來像是CAShapeLayer 這很好,因為這意味着兩者之間已經存在一定距離。

但是 ,在draw()內添加子視圖或子層不是一個好主意。 只能用於繪圖。 否則,每次重新繪制視圖時,這些東西都會重新添加。 在這種情況下,它不太明顯,因為一遍又一遍地添加了同一層。 但是仍然應該避免。

您仍然可以使用draw()渲染居中的點。 但這並不是動畫的一部分。

至於旋轉動畫; 除非我極大地忘記了底層機制,否則獨立圖層(不支持視圖的圖層(很可能是您創建的))不在乎UIView動畫上下文。 取而代之的是,您將使用Core Animation級別的API對其進行動畫處理。 這可以通過更改CATransaction內的屬性或通過從一個角度創建CABasicAnimation並將其添加到圖層來完成。

請注意 ,添加動畫不會更改“模型”值(圖層的屬性),並且一旦動畫完成,圖層將像添加動畫之前一樣進行渲染。 如果該層預期動畫的值,呆在那里,你會增加了動畫和更新圖層屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM