2017-02-20 40 views
0

我仍然试图学习这部分的东西。我环顾四周,看了一些关于它的问题,但是我真的不明白。我如何调用一个类的功能 - 斯威夫特

我有创建和打圈

class CircleView: UIView { 
var circleLayer: CAShapeLayer! 
var isAnimating = false 
override init(frame: CGRect) { 
    let fColor = UIColor.clear.cgColor 





    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(arcCenter: CGPoint(x: frame.size.width/2.0, y: frame.size.height/2.0), radius: (frame.size.width - 10)/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.init(rgbColorCodeRed: 230, green: 226, blue: 218, alpha: 1).cgColor 
    circleLayer.lineWidth = 9.0; 

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

    // Add the circleLayer to the view's layer's sublayers 
    layer.addSublayer(circleLayer) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 



func setCircleClockwise(){ 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2.0, y: frame.size.height/2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true) 
    self.circleLayer.removeFromSuperlayer() 
    self.circleLayer = formatCirle(circlePath: circlePath) 
    self.layer.addSublayer(self.circleLayer) 
} 

func setCircleCounterClockwise(){ 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2.0, y: frame.size.height/2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: false) 
    self.circleLayer.removeFromSuperlayer() 
    self.circleLayer = formatCirle(circlePath: circlePath) 
    self.layer.addSublayer(self.circleLayer) 
} 

func formatCirle(circlePath: UIBezierPath) -> CAShapeLayer{ 
    let circleShape = CAShapeLayer() 
    circleShape.path = circlePath.cgPath 
    circleShape.fillColor = UIColor.clear.cgColor 
    circleShape.strokeColor = UIColor.init(rgbColorCodeRed: 230, green: 226, blue: 218, alpha: 1).cgColor 
    circleShape.lineWidth = 9.0; 
    circleShape.strokeEnd = 0.0 
    return circleShape 
} 

func animate(duration: TimeInterval){ 
    self.isAnimating = true 
    self.animateCircleFull(duration: 1) 
} 

func endAnimate(){ 
    self.isAnimating = false 
} 

func animateCircleFull(duration: TimeInterval) { 
    if self.isAnimating{ 
     CATransaction.begin() 
     let animation = CABasicAnimation(keyPath: "strokeEnd") 
     animation.duration = duration 
     animation.fromValue = 0 
     animation.toValue = 1 
     animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
     circleLayer.strokeEnd = 1.0 
     CATransaction.setCompletionBlock { 
      self.setCircleCounterClockwise() 
      self.animateCircleEmpty(duration: duration) 
     } 
     // Do the actual animation 
     circleLayer.add(animation, forKey: "animateCircle") 
     CATransaction.commit() 
    } 
} 

func animateCircleEmpty(duration: TimeInterval){ 
    if self.isAnimating{ 
     CATransaction.begin() 
     let animation = CABasicAnimation(keyPath: "strokeEnd") 
     animation.duration = duration 
     animation.fromValue = 1 
     animation.toValue = 0 
     animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
     circleLayer.strokeEnd = 0 
     CATransaction.setCompletionBlock { 
      self.setCircleClockwise() 
      self.animateCircleFull(duration: duration) 
     } 
     // Do the actual animation 
     circleLayer.add(animation, forKey: "animateCircle") 
     CATransaction.commit() 
    } 
} 

正在被从我的viewController具有以下功能叫做Circle类。它一切正常,但我无法解决的是如何去调用同一个圆上的endAnimation函数?

func addCircleView() { 
    let diceRoll = CGFloat(Int(arc4random_uniform(7))*50) 
    var circleWidth = CGFloat(100) 
    var circleHeight = circleWidth 
    var bgColor: UIColor = UIColor.init(rgbColorCodeRed: 230, green: 226, blue: 218, alpha: 1) 

    // Create a new CircleView 
    let circleView = CircleView(frame: CGRect(x: self.view.frame.width/2, y: self.view.frame.height-110, width: circleWidth, height: circleHeight)) 
    //let test = CircleView(frame: CGRect(x: diceRoll, y: 0, width: circleWidth, height: circleHeight)) 
    cv = circleView 
    view.addSubview(circleView) 

    // Animate the drawing of the circle over the course of 1 second 
    circleView.animate(duration: 1) 
    let imageName = "ButtonBackground.png" 
    let image = UIImage(named: imageName) 
    let imageView = UIImageView(image: image!) 
    imageView.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height-110, width: 100, height: 100) 
    view.addSubview(imageView) 
    view.bringSubview(toFront: circleView) 
} 
+0

您没有任何endAnimation()函数,它是endAnimate()。我希望你试图调用正确的功能。 – Vakas

回答

0

对于在同一圆上调用endAnimation函数,您必须在ViewController类中声明属性。

import UIKit 

class ViewController: UIViewController { 

var circleView = CircleView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.addCircleView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

func addCircleView() { 
    let diceRoll = CGFloat(Int(arc4random_uniform(7))*50) 
    var circleWidth = CGFloat(100) 
    var circleHeight = circleWidth 
    var bgColor: UIColor = UIColor.init(rgbColorCodeRed: 230, green: 226, blue: 218, alpha: 1) 

    // Create a new CircleView 
    self.circleView = CircleView(frame: CGRect(x: self.view.frame.width/2, y: self.view.frame.height-110, width: circleWidth, height: circleHeight)) 
    //let test = CircleView(frame: CGRect(x: diceRoll, y: 0, width: circleWidth, height: circleHeight)) 
    cv = circleView 
    view.addSubview(circleView) 

    // Animate the drawing of the circle over the course of 1 second 
    circleView.animate(duration: 1) 
    let imageName = "ButtonBackground.png" 
    let image = UIImage(named: imageName) 
    let imageView = UIImageView(image: image!) 
    imageView.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height-110, width: 100, height: 100) 
    view.addSubview(imageView) 
    view.bringSubview(toFront: circleView) 
    //now you are able to call endAnimation function 
    self.circleView.endAnimate() 

    } 

}