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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

2回答

1453閲覧

【Swift】タップして画面に波紋

sakuradaiya

総合スコア24

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/10/14 14:43

編集2018/10/16 14:44

前提・実現したいこと

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}

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

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

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

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

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

sakuradaiya

2018/10/15 14:52

大変申し訳ございません。おっしゃる通り、最初はサイトからのコピペにて「出来たらいいな」と軽い気持ちから入ってやっていました。ただ、丸投げしているつもりはありません。調べても調べても分からず、最後の希望にすがる気持ちで投稿しています。純粋に理解できておらずに、質問内容に到達していないレベルなのかもしれません。不快な思いをされている方、大変申し訳ございませんでした。
guest

回答2

0

ベストアンサー

ViewControllerにcontentViewというメンバーが無い、という意味でエラーが出ています。

試していませんが、viewに変えるだけで期待する動きになるのではないかと思われます。

swift:

1class ViewController: UIViewController { 2 override func awakeFromNib() { 3 super.awakeFromNib() 4 5 let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(self.tapContentView(touch:))) 6 self.view.addGestureRecognizer(tapGesture) 7 } 8 9 @objc func tapContentView(touch: UITapGestureRecognizer) { 10 11 self.view.波紋(touch: touch) 12 } 13}

追記:2018/10/16
手元で試しましたがうまく動いているように見えます。
イメージ説明

投稿2018/10/15 00:06

編集2018/10/16 00:19
takabosoft

総合スコア8356

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

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

sakuradaiya

2018/10/15 13:57

ご回答いただき、有難うございます。 ただviewに変えてみましたが上手くいきませんでした。 ご回答いただいたのに活かしきれず申し訳ないです。
takabosoft

2018/10/16 00:19

上手くいかないとはどういう意味でしょうか?
sakuradaiya

2018/10/16 14:40

返信有難うございます。 再度、やってみたら出来ました! 昨日はなぜ出来なかったのか。。ベストアンサーつけさせて頂きます! 今後もお時間ありましたら、よろしくお願いします。
guest

0

「使い方」に書かれているコードはUITableViewCellUICollectionViewCellのカスタムクラスに実装して下さい。

投稿2018/10/15 01:13

編集2018/10/15 01:22
fuzzball

総合スコア16731

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

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

sakuradaiya

2018/10/15 14:43

ご回答いただき有難うございます。 UITableViewCellをストーリーボードで実装し、ViewController.swiftから分けてUITableViewCell.swiftに使い方に書かれているコードを記載しましたが、上手くいきませんでした。fuzzballさんが言われてる 「カスタムクラスに実装」とは上記の事ですよね? やり方はfuzzballさんの方法だと思いますので、さらに調べてからやってみます。
fuzzball

2018/10/16 00:21 編集

質問文にも書かれていますが「上手くいきませんでした」では、こちらは何も分からないのですよ。 何を調べたか、何をやったか、その結果どうなったか、をこちらに伝えて下さい。 試したコードは質問に追記(もしくは編集)して下さい。
sakuradaiya

2018/10/16 14:37

返信有難うございます。 上手くいかないじゃ伝わらないですよね。。質問に慣れてないので今後注意していきます。 今回は追記しているやり方で解決しました! 今後もどんどん質問するので、お時間ありましたらよろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問