前提・実現したいこと
Swift勉強中です。
今回はタップした場所から波紋を実現したいと思いましたが上手くいきません。
参考にしたサイト:http://tech-gym.com/2018/02/未分類/2602.html
発生している問題・エラーメッセージ
Value of type 'ViewController' has no member 'contentView'; did you mean 'tapContentView'? Replace 'contentView' with 'tapContentView'
該当のソースコード
Swift
1import UIKit 2 3extension UIView { 4 func 波紋(touch: UITapGestureRecognizer) { 5 6 // ①: タップされた場所にlayerを置き、半径200の円を描画 7 let location = touch.location(in: self) 8 let layer = CAShapeLayer.init() 9 self.layer.addSublayer(layer) 10 layer.frame = CGRect.init(x: location.x, y: location.y, width: 100, height: 100) 11 layer.position = CGPoint.init(x: location.x, y: location.y) 12 layer.contents = { 13 let size: CGFloat = 200.0 14 UIGraphicsBeginImageContext(CGSize.init(width: size, height: size)) 15 let context = UIGraphicsGetCurrentContext()! 16 context.saveGState() 17 context.setFillColor(UIColor.clear.cgColor) 18 context.fill(self.frame) 19 let r = CGFloat.init(size/2-10) 20 let center = CGPoint.init(x: size/2, y: size/2) 21 let path : CGMutablePath = CGMutablePath() 22 path.addArc(center: center, radius: r, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false) 23 context.addPath(path) 24 context.setFillColor(UIColor.lightGray.cgColor) 25 context.setStrokeColor(UIColor.lightGray.cgColor) 26 context.drawPath(using: .fillStroke) 27 let image = UIGraphicsGetImageFromCurrentImageContext() 28 context.restoreGState() 29 return image!.cgImage 30 }() 31 32 // ②: 円を拡大しつつ透明化するAnimationを用意 33 let animationGroup: CAAnimationGroup = { 34 let animation: CABasicAnimation = { 35 let animation = CABasicAnimation(keyPath: "transform.scale") 36 animation.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeOut) 37 animation.duration = 0.5 38 animation.isRemovedOnCompletion = false 39 animation.fillMode = CAMediaTimingFillMode.forwards 40 animation.fromValue = NSNumber(value: 0.5) 41 animation.toValue = NSNumber(value: 5.0) 42 return animation 43 }() 44 45 let animation2: CABasicAnimation = { 46 let animation = CABasicAnimation(keyPath: "opacity") 47 animation.duration = 0.5 48 animation.isRemovedOnCompletion = false 49 animation.fillMode = CAMediaTimingFillMode.forwards 50 animation.fromValue = NSNumber(value: 0.5) 51 animation.toValue = NSNumber(value: 0.0) 52 return animation 53 }() 54 55 let group = CAAnimationGroup() 56 group.beginTime = CACurrentMediaTime() 57 group.animations = [animation, animation2] 58 group.isRemovedOnCompletion = false 59 group.fillMode = CAMediaTimingFillMode.backwards 60 return group 61 }() 62 63 // ③: layerにAnimationを適用 64 CATransaction.setAnimationDuration(5.0) 65 CATransaction.setCompletionBlock({ 66 layer.removeFromSuperlayer() 67 }) 68 CATransaction.begin() 69 layer.add(animationGroup, forKey: nil) 70 layer.opacity = 0.0 71 CATransaction.commit() 72 } 73} 74 75class ViewController: UIViewController { 76 override func awakeFromNib() { 77 super.awakeFromNib() 78 79 let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(self.tapContentView(touch:))) 80 self.contentView.addGestureRecognizer(tapGesture) 81 } 82 83 @objc func tapContentView(touch: UITapGestureRecognizer) { 84 85 self.contentView.波紋(touch: touch) 86 } 87 88 89 90}
試したこと
storyboardでの作業は特に行なっていません。。必要でしょうか?
→成功したコードには必要ありませんでした。
成功したソースコード添付
Swift
1import UIKit 2 3extension UIView { 4 func 波紋(touch: UITapGestureRecognizer) { 5 6 // ①: タップされた場所にlayerを置き、半径200の円を描画 7 let location = touch.location(in: self) 8 let layer = CAShapeLayer.init() 9 self.layer.addSublayer(layer) 10 layer.frame = CGRect.init(x: location.x, y: location.y, width: 100, height: 100) 11 layer.position = CGPoint.init(x: location.x, y: location.y) 12 layer.contents = { 13 let size: CGFloat = 200.0 14 UIGraphicsBeginImageContext(CGSize.init(width: size, height: size)) 15 let context = UIGraphicsGetCurrentContext()! 16 context.saveGState() 17 context.setFillColor(UIColor.clear.cgColor) 18 context.fill(self.frame) 19 let r = CGFloat.init(size/2-10) 20 let center = CGPoint.init(x: size/2, y: size/2) 21 let path : CGMutablePath = CGMutablePath() 22 path.addArc(center: center, radius: r, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false) 23 context.addPath(path) 24 context.setFillColor(UIColor.lightGray.cgColor) 25 context.setStrokeColor(UIColor.lightGray.cgColor) 26 context.drawPath(using: .fillStroke) 27 let image = UIGraphicsGetImageFromCurrentImageContext() 28 context.restoreGState() 29 return image!.cgImage 30 }() 31 32 // ②: 円を拡大しつつ透明化するAnimationを用意 33 let animationGroup: CAAnimationGroup = { 34 let animation: CABasicAnimation = { 35 let animation = CABasicAnimation(keyPath: "transform.scale") 36 animation.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeOut) 37 animation.duration = 0.5 38 animation.isRemovedOnCompletion = false 39 animation.fillMode = CAMediaTimingFillMode.forwards 40 animation.fromValue = NSNumber(value: 0.5) 41 animation.toValue = NSNumber(value: 5.0) 42 return animation 43 }() 44 45 let animation2: CABasicAnimation = { 46 let animation = CABasicAnimation(keyPath: "opacity") 47 animation.duration = 0.5 48 animation.isRemovedOnCompletion = false 49 animation.fillMode = CAMediaTimingFillMode.forwards 50 animation.fromValue = NSNumber(value: 0.5) 51 animation.toValue = NSNumber(value: 0.0) 52 return animation 53 }() 54 55 let group = CAAnimationGroup() 56 group.beginTime = CACurrentMediaTime() 57 group.animations = [animation, animation2] 58 group.isRemovedOnCompletion = false 59 group.fillMode = CAMediaTimingFillMode.backwards 60 return group 61 }() 62 63 // ③: layerにAnimationを適用 64 CATransaction.setAnimationDuration(5.0) 65 CATransaction.setCompletionBlock({ 66 layer.removeFromSuperlayer() 67 }) 68 CATransaction.begin() 69 layer.add(animationGroup, forKey: nil) 70 layer.opacity = 0.0 71 CATransaction.commit() 72 } 73} 74 75class ViewController: UIViewController { 76 override func awakeFromNib() { 77 super.awakeFromNib() 78 79 let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(self.tapContentView(touch:))) 80 self.View.addGestureRecognizer(tapGesture) 81 } 82 83 @objc func tapContentView(touch: UITapGestureRecognizer) { 84 85 self.View.波紋(touch: touch) 86 } 87 88 89 90}
大変申し訳ございません。おっしゃる通り、最初はサイトからのコピペにて「出来たらいいな」と軽い気持ちから入ってやっていました。ただ、丸投げしているつもりはありません。調べても調べても分からず、最後の希望にすがる気持ちで投稿しています。純粋に理解できておらずに、質問内容に到達していないレベルなのかもしれません。不快な思いをされている方、大変申し訳ございませんでした。
回答2件
あなたの回答
tips
プレビュー