2017-02-14 5 views
0

refrence: https://stackoverflow.com/a/26578895/6619234zeichnen animierte Kreis in schnellen 3

wie löschen und Kreis auf Klick EVnet neu zu zeichnen? Ich habe versucht, addCircleView-Methode auf Click-Ereignis, aber Kreis überlappt sich jedes Mal.

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") 
} 

    } 

in Ihre Subview hinzufügen

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

Vielen Dank im Voraus

+0

Deklarieren Circle global und von Superview entfernen, bevor neue Ansicht hinzufügen. – ChanWarde

+0

@ChanWarde danke ich vergesse diese kleine Sache :-) – johnyDiOS

+0

Willkommen @johnyDiOs, wenn es hilfreich war, können Sie meine Antwort akzeptieren. – ChanWarde

Antwort

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

Sie können UIView nicht vor dem Erstellen entfernen .. BTW danke für die Antwort – johnyDiOS

+0

Das ist der Grund, warum ich erwähne es global erklären – ChanWarde