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

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

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

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

Q&A

0回答

267閲覧

カウント画面を作る上でつまづいています

aaaaaachannel

総合スコア37

Swift

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

0グッド

0クリップ

投稿2017/11/06 01:52

編集2017/11/06 02:05

カウントダウン画面を作っています。
circleAnimationメソッドの中の
drawAnimation.setValue(layer, forKey: "animationLayer")
の意味がわかりません。
layerとは何をさしているのでしょうか?DidStopメソッドで取り出していますが、取り出すことで具体的に何ができるのでしょうか。
よろしくお願いします。

swift

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

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

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

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

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

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

fuzzball

2017/11/06 01:56

同じような質問を繰り返さないでください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問