2016-03-29 13 views
-1

Yanıtlarınız için hepinize teşekkür ederiz. ithalat UIKitGrafik olarak elma izle bir izle

class CircleView: UIView { 

    private let progressLayer: CAShapeLayer = CAShapeLayer() 
    private let backgroundLayer = CAShapeLayer() 

    private var progressLabel: UILabel 

    required init?(coder aDecoder: NSCoder) { 
     progressLabel = UILabel() 
     super.init(coder: aDecoder) 
     createProgressLayer() 
//  createLabel() 
    } 

    override init(frame: CGRect) { 
     progressLabel = UILabel() 
     super.init(frame: frame) 
     createProgressLayer() 
//  createLabel() 
    } 

// func createLabel() { 
//  progressLabel = UILabel(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(frame), 60.0)) 
//  progressLabel.textColor = .whiteColor() 
//  progressLabel.textAlignment = .Center 
//  progressLabel.text = "Load content" 
//  progressLabel.font = UIFont(name: "HelveticaNeue-UltraLight", size: 40.0) 
//  progressLabel.translatesAutoresizingMaskIntoConstraints = false 
//  addSubview(progressLabel) 
//   
//  addConstraint(NSLayoutConstraint(item: self, attribute: .CenterX, relatedBy: .Equal, toItem: progressLabel, attribute: .CenterX, multiplier: 1.0, constant: 0.0)) 
//  addConstraint(NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: progressLabel, attribute: .CenterY, multiplier: 1.0, constant: 0.0)) 
// } 

    private func createProgressLayer() { 
     var arcWidth: CGFloat = 3 
     var arcBgColor: UIColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0) 

     let startAngle = CGFloat(M_PI_2) 
     let endAngle = CGFloat(M_PI * 2 + M_PI_2) 
     let radius: CGFloat = max(bounds.width, bounds.height) 
     let center = CGPoint(x:bounds.width/2, y: bounds.height/2) 

     let centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame)/2) 

     let gradientMaskLayer = gradientMask() 
     progressLayer.path = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).CGPath 
     backgroundLayer.path = UIBezierPath(arcCenter: centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle: startAngle, endAngle: endAngle, clockwise: true).CGPath 

     backgroundLayer.fillColor = UIColor.clearColor().CGColor 
     backgroundLayer.lineWidth = 15.0 
     backgroundLayer.strokeColor = arcBgColor.CGColor 
     self.layer.addSublayer(backgroundLayer) 

     progressLayer.backgroundColor = UIColor.clearColor().CGColor 
     progressLayer.fillColor = nil 
     progressLayer.strokeColor = UIColor.blackColor().CGColor 
     progressLayer.lineWidth = 15.0 
     progressLayer.strokeStart = 0.0 
     progressLayer.strokeEnd = 0.0 


     gradientMaskLayer.mask = progressLayer 
     layer.addSublayer(gradientMaskLayer) 
    } 

    private func gradientMask() -> CAGradientLayer { 
     let gradientLayer = CAGradientLayer() 
     gradientLayer.frame = bounds 

     gradientLayer.locations = [0.0, 1.0] 

     let colorTop: AnyObject = UIColor(red: 255.0/255.0, green: 213.0/255.0, blue: 63.0/255.0, alpha: 1.0).CGColor 
     let colorBottom: AnyObject = UIColor(red: 255.0/255.0, green: 198.0/255.0, blue: 5.0/255.0, alpha: 1.0).CGColor 
     let arrayOfColors: [AnyObject] = [colorTop, colorBottom] 
     gradientLayer.colors = arrayOfColors 

     return gradientLayer 
    } 
//  
// func hideProgressView() { 
//  progressLayer.strokeEnd = 0.0 
//  progressLayer.removeAllAnimations() 
//  progressLabel.text = "Load content" 
// } 

    func animateProgressView() { 
//  progressLabel.text = "Loading..." 
     progressLayer.strokeEnd = 0.0 

     let animation = CABasicAnimation(keyPath: "strokeEnd") 
     animation.fromValue = CGFloat(0.0) 
     animation.toValue = CGFloat(0.7) 
     animation.duration = 1.0 
     animation.delegate = self 
     animation.removedOnCompletion = false 
     animation.additive = true 
     animation.fillMode = kCAFillModeForwards 
     progressLayer.addAnimation(animation, forKey: "strokeEnd") 
    } 

} 

Ama gerçekten artık nasıl bilmek istiyorum: Benim yeni sabit circleView şimdi enter image description here

:

elma seyretmek biri gibi bırakmak, köşeli yapın ve yüzdelik etiketini, 0 ile 70% arasında hareket ettirmek, yani, 0 ile 70 arasında animasyon yapmak için daire animasyonları.

+0

Lütfen sormadan önce arama yapın. Bu SO üzerinde yüzlerce kez ele alınmıştır. – matt

+0

Çizim değişkenini yüzde cinsinden ve çizimden önce drawRect uygulamasında var + = val; gibi yapın ve doğru daire çizgisini çizmek için var öğesini kullanın. Ayrıca var min ve max değerlerini de sınırlamayı unutmayın. –

+0

Bana söylediklerini daha önce yaptım, ama grafiği bir elma saati olarak görmek istiyorum. Yapabileceğim bir şey var mı? Belki köşe yarıçapı ile? Şimdiden teşekkürler – Sergio

cevap

1

Bunu yapmanın kolay yolu, bir CAShapeLayer kullanmak ve strokeEnd'u hareketlendirmektir. Aksi takdirde tüm ara şekilleri kendiniz çizmeniz ve kendi animasyonunuzu yaratmanız gerekir.

+0

Matt ne dedi. (vote.) DrawRect'i kullanarak kendinizi hareket ettirmek genellikle daha fazla CPU yoğun, daha da zor ve sonuçlar iyi görünmüyor. Çekirdek Animasyon kullanın. –

+1

@GeneratorOfOneOn thx düzenleme için, çok hızlı hareket ediyordum :) – matt

+0

@matt Teşekkür ederim. Çember animasyonunu zaten yaptım, ama daha önce olduğu gibi aynı etkiyi elde etmek için arka plan çemberini elde etmek için ne yapmalıyım? Yeni kodumu buraya yükleyeceğim. – Sergio

İlgili konular