drawanimationメソッドの中で、drawAnimation.delegateというのは自身に何を通知しているのですか?
またdelegateを使用するのに本当にこの記述だけでいいのか教えていただきたいです。
swift
1 2import UIKit 3 4class StartCountDownViewController: UIViewController { 5 6 func segueToScoreViewController(){ 7 self.performSegue(withIdentifier: "toViewController", sender: nil) 8 } 9 10 var countNumberLabel:UILabel! 11 var circleView:UIView! 12 var countDownNum = 3 13 let countDownMax = 3 14 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 //数値ラベル 20 countNumberLabel = UILabel(frame: CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height)) 21 countNumberLabel.font = UIFont(name: "HelveticaNeue", size: 54) 22 //中央ぞろえ 23 countNumberLabel.textAlignment = NSTextAlignment.center 24 countNumberLabel.baselineAdjustment = UIBaselineAdjustment.alignCenters 25 self.view.addSubview(countNumberLabel) 26 27 //円 28 circleView = UIView(frame:CGRect(x:(self.view.frame.width/2)-100, y:(self.view.frame.height/2)-100, width:200, height:200)) 29 circleView.layer.addSublayer(drawCircle(viewWidth: circleView.frame.width, strokeColor: UIColor(red:0.0,green:0.0,blue:0.0,alpha:0.2))) 30 circleView.layer.addSublayer(drawCircle(viewWidth: circleView.frame.width, strokeColor: UIColor(red:0.0,green:0.0,blue:0.0,alpha:1.0))) 31 self.view.addSubview(circleView) 32 33 // Do any additional setup after loading the view. 34 } 35 36 override func viewWillAppear(_ animated: Bool) { 37 countDownNum = countDownMax 38 countNumberLabel.text = String(countDownNum) 39 circleAnimation(layer: circleView.layer.sublayers[1] as CAShapeLayer) 40 } 41 42 func drawCircle(viewWidth:CGFloat, strokeColor:UIColor) ->CAShapeLayer{ 43 var circle:CAShapeLayer = CAShapeLayer() 44 let lineWidth:CGFloat = 20 45 let viewScale:CGFloat = viewWidth 46 let radius:CGFloat = viewScale - lineWidth 47 48 circle.path = UIBezierPath(roundedRect:CGRect(x:0,y:0,width:radius,height:radius), cornerRadius:radius/2).cgPath 49 circle.position = CGPoint(x:lineWidth/2, y:lineWidth/2) 50 circle.fillColor = UIColor.clear.cgColor 51 circle.strokeColor = strokeColor.cgColor 52 circle.lineWidth = lineWidth 53 return circle 54 } 55 56 func circleAnimation(layer:CAShapeLayer){ 57 //アニメーションを行うプロパティ=今回はstrokeend(起点と終点を設定) 58 var drawAnimation = CABasicAnimation(keyPath: "strokeEnd") 59 60 drawAnimation.setValue(layer, forKey: "animationLayer") 61 drawAnimation.delegate = self 62 63 drawAnimation.duration = 1.0 64 drawAnimation.repeatCount = 1.0 65 drawAnimation.fromValue = 0.0 66 drawAnimation.toValue = 1.0 67 68 drawAnimation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseOut) 69 70 layer.add(drawAnimation, forKey: "circleAnimation") 71 72 73 74 } 75 override func animationDidStop(anim:CAAnimation!, finished flag:Bool ){ 76 let layer:CAShapeLayer = anim.valueForKey("animationLayer") as CAShapeLayer 77 countDownNum -= 1 78 countNumberLabel.text = String(countDownNum) 79 80 if countDownNum <= 0 { 81 82 }else{ 83 circleAnimation(layer: layer) 84 } 85 } 86 87 88 override func didReceiveMemoryWarning() { 89 super.didReceiveMemoryWarning() 90 // Dispose of any resources that can be recreated. 91 } 92 93 94 /* 95 // MARK: - Navigation 96 97 // In a storyboard-based application, you will often want to do a little preparation before navigation 98 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 99 // Get the new view controller using segue.destinationViewController. 100 // Pass the selected object to the new view controller. 101 } 102 */ 103 104} 105
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。