2017-02-14 62 views
0

Refrence:https://stackoverflow.com/a/26578895/6619234畫動畫圈中迅速3

如何擦除和重繪上點擊evnet圈? 我試圖在點擊事件時調用addCircleView方法,但是每次都有圓圈重疊。

class CircleClosing: UIView { 

var circleLayer: CAShapeLayer! 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    self.backgroundColor = UIColor.clear 

    // Use UIBezierPath as an easy way to create the CGPath for the layer. 
    // The path should be the entire circle. 
    let circlePath : UIBezierPath! 
    circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2.0, y: frame.size.height/2.0), radius: (frame.size.width - 5)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true) 

    // Setup the CAShapeLayer with the path, colors, and line width 
    circleLayer = CAShapeLayer() 
    circleLayer.path = circlePath.cgPath 
    circleLayer.fillColor = UIColor.clear.cgColor 
    circleLayer.strokeColor = UIColor.blue.cgColor 
    circleLayer.lineWidth = 20.0; 

    // Don't draw the circle initially 
    circleLayer.strokeEnd = 0.0 

    // Add the circleLayer to the view's layer's sublayers 


} 
override func layoutSubviews() 
{ 
    let frame = self.layer.bounds 
    circleLayer.frame = frame 
    layer.addSublayer(circleLayer) 

} 
required init?(coder aDecoder: NSCoder) 
    { super.init(coder: aDecoder) } 

func animateCircle(duration: TimeInterval) { 


    // We want to animate the strokeEnd property of the circleLayer 
    let animation = CABasicAnimation(keyPath: "strokeEnd") 

    // Set the animation duration appropriately 
    animation.duration = duration 


    // Animate from 0 (no circle) to 1 (full circle) 
    animation.fromValue = 0 
    animation.toValue = 1 

    // Do a linear animation (i.e. the speed of the animation stays the same) 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the 
    // right value when the animation ends. 
    circleLayer.strokeEnd = 1.0 

    // Do the actual animation 
    circleLayer.add(animation, forKey: "animateCircle") 
} 

    } 

添加在您的子視圖

func addCircleView() { 

    var circleView : CircleClosing! 
    let diceRoll = CGFloat(510) //CGFloat(Int(arc4random_uniform(7))*50) 
    let diceRolly = CGFloat(70) 
    let circleWidth = CGFloat(40) 
    let circleHeight = circleWidth 

    // Create a new CircleView 
    circleView = CircleClosing(frame:CGRect(x:diceRoll,y: diceRolly,width: circleWidth,height: circleHeight)) 

    view.addSubview(circleView) 

    // Animate the drawing of the circle over the course of 1 second 
    circleView.animateCircle(duration: 20.0) 
} 

由於提前

+0

聲明circleView全球和增加新的看法之前,從上海華刪除。 – ChanWarde

+0

@ChanWarde謝謝你,我忘了這件小事:-) – johnyDiOS

+0

歡迎@johnyDiOs,如果有幫助,你可以接受我的回答。 – ChanWarde

回答

0
var circleView : CircleClosing! 

func addCircleView() { 
    let diceRoll = CGFloat(510) //CGFloat(Int(arc4random_uniform(7))*50) 
    let diceRolly = CGFloat(70) 
    let circleWidth = CGFloat(40) 
    let circleHeight = circleWidth 

    //Add this line here to remove from superview 
    circleView.removeFromSuperview() 

    circleView = CircleClosing(frame:CGRect(x:diceRoll,y: diceRolly,width: circleWidth,height: circleHeight)) 

    view.addSubview(circleView) 

    // Animate the drawing of the circle over the course of 1 second 
    circleView.animateCircle(duration: 20.0) 
} 
+0

你不能刪除UIView之前創建..順便說一句謝謝你的回答 – johnyDiOS

+0

這就是爲什麼我提到它聲明全球 – ChanWarde