2017-02-19 56 views
-3

我使用自定義指標,但是當我在我的viewdidload中調用子類指示符時,我的視圖控制器是空白的,但是當我在操場上運行它時,我可以在側窗中看到它。這是指標的代碼。 Theres沒有錯誤,但我的指標沒有顯示。那是我的問題。如果有人能告訴我爲什麼,我將不勝感激。我的指標是空的

import UIKit 

class MaterialLoadingIndicator: UIView { 

let MinStrokeLength: CGFloat = 0.05 
let MaxStrokeLength: CGFloat = 0.7 
let circleShapeLayer = CAShapeLayer() 

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

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

func initShapeLayer() { 
    circleShapeLayer.actions = ["strokeEnd" : NSNull(), 
           "strokeStart" : NSNull(), 
           "transform" : NSNull(), 
           "strokeColor" : NSNull()] 
    circleShapeLayer.backgroundColor = UIColor.clear.cgColor 
    circleShapeLayer.strokeColor  = UIColor.blue.cgColor 
    circleShapeLayer.fillColor  = UIColor.clear.cgColor 
    circleShapeLayer.lineWidth  = 5 
    circleShapeLayer.lineCap   = kCALineCapRound 
    circleShapeLayer.strokeStart  = 0 
    circleShapeLayer.strokeEnd  = MinStrokeLength 
    let center      = CGPoint(x: bounds.width*0.5, y: bounds.height*0.5) 
    circleShapeLayer.frame   = bounds 
    circleShapeLayer.path   = UIBezierPath(arcCenter: center, 
                radius: center.x, 
                startAngle: 0, 
                endAngle: CGFloat(M_PI*2), 
                clockwise: true).cgPath 
    layer.addSublayer(circleShapeLayer) 
} 

func startAnimating() { 
    if layer.animation(forKey: "rotation") == nil { 
     startColorAnimation() 
     startStrokeAnimation() 
     startRotatingAnimation() 
    } 
} 

private func startColorAnimation() { 
    let color  = CAKeyframeAnimation(keyPath: "strokeColor") 
    color.duration = 10.0 
    color.values = [UIColor(hex: 0x4285F4, alpha: 1.0).cgColor, 
         UIColor(hex: 0xDE3E35, alpha: 1.0).cgColor, 
         UIColor(hex: 0xF7C223, alpha: 1.0).cgColor, 
         UIColor(hex: 0x1B9A59, alpha: 1.0).cgColor, 
         UIColor(hex: 0x4285F4, alpha: 1.0).cgColor] 
    color.calculationMode = kCAAnimationPaced 
    color.repeatCount  = Float.infinity 
    circleShapeLayer.add(color, forKey: "color") 
} 

private func startRotatingAnimation() { 
    let rotation   = CABasicAnimation(keyPath: "transform.rotation.z") 
    rotation.toValue  = M_PI*2 
    rotation.duration  = 2.2 
    rotation.isCumulative  = true 
    rotation.isAdditive  = true 
    rotation.repeatCount = Float.infinity 
    layer.add(rotation, forKey: "rotation") 
} 

private func startStrokeAnimation() { 
    let easeInOutSineTimingFunc = CAMediaTimingFunction(controlPoints: 0.39, 0.575, 0.565, 1.0) 
    let progress: CGFloat  = MaxStrokeLength 
    let endFromValue: CGFloat = circleShapeLayer.strokeEnd 
    let endToValue: CGFloat = endFromValue + progress 
    let strokeEnd     = CABasicAnimation(keyPath: "strokeEnd") 
    strokeEnd.fromValue    = endFromValue 
    strokeEnd.toValue    = endToValue 
    strokeEnd.duration    = 0.5 
    strokeEnd.fillMode    = kCAFillModeForwards 
    strokeEnd.timingFunction  = easeInOutSineTimingFunc 
    strokeEnd.beginTime    = 0.1 
    strokeEnd.isRemovedOnCompletion = false 
    let startFromValue: CGFloat  = circleShapeLayer.strokeStart 
    let startToValue: CGFloat  = fabs(endToValue - MinStrokeLength) 
    let strokeStart     = CABasicAnimation(keyPath: "strokeStart") 
    strokeStart.fromValue   = startFromValue 
    strokeStart.toValue    = startToValue 
    strokeStart.duration   = 0.4 
    strokeStart.fillMode   = kCAFillModeForwards 
    strokeStart.timingFunction  = easeInOutSineTimingFunc 
    strokeStart.beginTime   = strokeEnd.beginTime + strokeEnd.duration + 0.2 
    strokeStart.isRemovedOnCompletion = false 
    let pathAnim     = CAAnimationGroup() 
    pathAnim.animations   = [strokeEnd, strokeStart] 
    pathAnim.duration   = strokeStart.beginTime + strokeStart.duration 
    pathAnim.fillMode   = kCAFillModeForwards 
    pathAnim.isRemovedOnCompletion = false 
    CATransaction.begin() 
    CATransaction.setCompletionBlock { 
     if self.circleShapeLayer.animation(forKey: "stroke") != nil { 
      self.circleShapeLayer.transform = CATransform3DRotate(self.circleShapeLayer.transform, CGFloat(M_PI*2) * progress, 0, 0, 1) 
      self.circleShapeLayer.removeAnimation(forKey: "stroke") 
      self.startStrokeAnimation() 
     } 
    } 
    circleShapeLayer.add(pathAnim, forKey: "stroke") 
    CATransaction.commit() 
} 

func stopAnimating() { 
    circleShapeLayer.removeAllAnimations() 
    layer.removeAllAnimations() 
    circleShapeLayer.transform = CATransform3DIdentity 
    layer.transform   = CATransform3DIdentity 
} 

} 

extension UIColor { 

convenience init(hex: UInt, alpha: CGFloat) { 
    self.init(
     red: CGFloat((hex & 0xFF0000) >> 16)/255.0, 
     green: CGFloat((hex & 0x00FF00) >> 8)/255.0, 
     blue: CGFloat(hex & 0x0000FF)/255.0, 
     alpha: CGFloat(alpha) 
    ) 
} 

} 

這裏是我的視圖控制器在viewDidLoad中

import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let view  = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) 
    let indicator = MaterialLoadingIndicator(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 
    indicator.center = CGPoint(x: 320*0.5, y: 568*0.5) 
    view.addSubview(indicator) 
    indicator.startAnimating() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

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


    } 

回答

-1

view持有indicator只是浮動左右,感覺失落,不屬於心情不爽,不是代碼增加了給某人。 :)

編輯: 好了,約翰,現在我們被卡住了,讓我們添加的指示器有人

override func viewDidLoad() { 
    super.viewDidLoad() 
    let view  = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) 
    let indicator = MaterialLoadingIndicator(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 
    indicator.center = CGPoint(x: 320*0.5, y: 568*0.5) 
    view.addSubview(indicator) 
    indicator.startAnimating() 

    self.view.addSubview(view) // John, this is what was missing 
} 
+0

我試圖把它放在一個單元格內但它不工作 – john

+0

@john你是怎麼試試它的? – ystack

+0

我把它放在索引行的單元格中,但它加載需要很長時間 – john