質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

292閲覧

delegateについて質問です。

aaaaaachannel

総合スコア37

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/11/04 11:19

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

まずご自身が定義されている「drawAnimation」はメソッドではなく、CABasicAnimationクラスのインスンタンスを格納した変数です(もっと名詞っぽい変数名にした方がいいかも知れません)。

CABasicAnimationのdelegateには、CAAnimationDelegateプロトコルを実装したクラスのインスタンスをセットします。

プロトコルというのは実装すべきメソッドを定義した規約です。
CAAnimationDelegateには、「(必要に応じて)animationDidStopメソッドやanimationDidStartメソッドを実装してくださいね」、ということが書かれていると思ってください。

ここでは delegate = self と指定しているので、StartCountDownViewControllerがCAAnimationDelegateを実装する必要があります。

デリゲートを設定しておくと、アニメーションが終わったときにdrawAnimationが、selfに定義したanimationDidStopを呼んでくれます。逆に言うと、アニメーションが終わったときにanimationDidStopを呼ぶので、ちゃんとCAAnimationDelegateを実装しておいてね、というわけです。

CAAnimationDelegateを実装しますよ、というのはクラス定義で以下のように記述します。

class StartCountDownViewController: UIViewController, CAAnimationDelegate {

そしてanimationDidStopメソッドやanimationDidStartをStartCountDownViewControllerの中で定義します。
実際にanimationDidStopを実装されていますね。「override」キーワードを付けていますが、ここではオーバーライドしているわけではないので不要です。

投稿2017/11/04 12:48

ndo

総合スコア115

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問