2016-03-29 3 views
-1

あなたの返信ありがとうございます。輸入のUIKitアップルウォッチのようなグラフィック

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

} 

しかし、私は本当に今、どのように知っていただきたいと思います:私の新しい固定circleViewは今ある

はリンゴの腕時計1のように残すために、コーナーでこれを行います。 enter image description here

パーセントラベルを0から70%に移動する方法は、円がアニメーション化されると同時に0から70までアニメーション化することです。

+0

質問する前に検索してください。これはSOに何百回もカバーされています。 – matt

+0

変数を変数に、描画する前にdrawRectにvar + = val;を格納し、varを使って右円の線を描きます。また、varの最小値と最大値を制限することを忘れないでください。 –

+0

私はあなたが私に言ったことをすでにしていますが、私はリンゴの時計としてグラフィックを取得したいと思います。何か私にできることはありますか?たぶんコーナーの半径ですか?あらかじめありがとうございます – Sergio

答えて

1

これを行う簡単な方法は、CAShapeLayerを使用してstrokeEndをアニメーション化することです。それ以外の場合は、すべての中間図形を自分で描画し、独自のアニメーションを作成するだけです。

+0

マットが言ったこと。 (投票しました)drawRectを使用して自分自身をアニメーション化するのは、一般的にはCPUを大量に消費します。コアアニメーションを使用する。 –

+1

編集のための@GeneratorOfOne thx、私はあまりにも速く動いていた:) – matt

+0

@mattありがとう。私はすでにサークルアニメーションを行っていますが、以前と同じ効果を得るためにバックグラウンドサークルを取得するにはどうすればよいですか?私はここに新しいコードをアップロードします。 – Sergio

関連する問題