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

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

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

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

Swift

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

Q&A

解決済

2回答

466閲覧

scrollViewDidScrollでセルのフリックと同時にUIViewを動かしたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/04/23 17:02

編集2018/04/23 17:07

ストーリーボード未使用で開発しています。

イメージ説明

画像を見ていだだきたいのですが、

・ブラックのホリゾンタルバーを乗せたパープル部分の階層は
⬇︎の順番です。
UICollectionViewController
Cell( HugaCell )
UIView( HogeView )
collectionview( collectionView )
Cell( HogeCell )パープル

・この下に別セクションとしてブルーがあります。階層は
⬇︎の順番です。
UICollectionViewController
Cell( AAACell )
collectionView( collectionView )
Cell( BBBCell )ブルー

・グリーンは気にしないでください。

//

今回実現したいのは、
ブルーのビューをページをめくる様に右へフリックすると、
ブラックのバーも同時に付いてくる(今ある左側から右側へスライドする)様に実装したいのです。

コード内の⚠️メソッドで、
ブルーをフリックしてコンソールに現在のスクロール位置を取得できていますが、
ブラックのバーは無反応です。
この場合、どうすればブラックのバーも同時に動かせるのでしょうか?

⚠️のメソッドをいくつかの場所に書いて見て反応を見ましたが、分からずにいます。

教えて頂けないでしょうか?
よろしくお願いします。

//AAACellです ブルー import UIKit class AAACell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { let cellId5 = "cellId" override init(frame: CGRect) { super.init(frame: frame) collectionView.register(BBBCell.self, forCellWithReuseIdentifier: "cellId5") setup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } let collectionView: UICollectionView = { let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.minimumLineSpacing = 20 layout.minimumInteritemSpacing = 0 let cv = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout) cv.isPagingEnabled = true cv.translatesAutoresizingMaskIntoConstraints = false return cv }() private func setup() { addSubview(collectionView) collectionView.dataSource = self collectionView.delegate = self addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": collectionView])) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": collectionView])) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 2 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId5", for: indexPath) as!BBBCell return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: frame.width, height: frame.height) } //⚠️コンソールに反応しますが、ブラックのバーは付いてきません。 func scrollViewDidScroll(_ scrollView: UIScrollView) { let hogeView = HogeView() hogeView.setupHorizontalBar() print(scrollView.contentOffset.x) hogeView.horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x / 2 } } class BBBCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) //ブルー backgroundColor = .blue } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
//HogeViewです パープル import UIKit let cellId = "cellId" class HogeView: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { override init(frame: CGRect) { super.init(frame: frame) collectionView.register(HogeCell.self, forCellWithReuseIdentifier: "cellId") setup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } let collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) cv.translatesAutoresizingMaskIntoConstraints = false return cv }() func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HogeCell return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 2 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: frame.width / 2, height: frame.height) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 0 } func setup() { addSubview(collectionView) collectionView.dataSource = self collectionView.delegate = self addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": collectionView])) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": collectionView])) setup() } var horizontalBarLeftAnchorConstraint: NSLayoutConstraint? func setup() { let horizontalBarView = UIView() //ブラック horizontalBarView.backgroundColor = .black horizontalBarView.translatesAutoresizingMaskIntoConstraints = false addSubview(horizontalBarView) horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor) horizontalBarLeftAnchorConstraint?.isActive = true horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/2).isActive = true horizontalBarView.heightAnchor.constraint(equalToConstant: 46).isActive = true } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print(indexPath.item) let x = CGFloat(indexPath.item) * frame.width / 2 horizontalBarLeftAnchorConstraint?.constant = x //アニメーション UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { self.layoutIfNeeded() }, completion: nil) } } class HogeCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } override func setup() { //パープル backgroundColor = .purple } }

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

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

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

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

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

guest

回答2

0

ベストアンサー

func scrollViewDidScroll(_ scrollView: UIScrollView) { let hogeView = HogeView() //⚠️ }

この時点で新しいインスタンスを生成しているので、画像のブラックのUIViewとは繋がりがありません。
ブラックの部分に対する参照をなんとか取得できれば連動できると思います。

投稿2018/04/23 21:47

newmt

総合スコア1277

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

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

退会済みユーザー

退会済みユーザー

2018/04/24 02:27

ありがとうございます。 >>繋がりがありません。 繋がりを持たす為に生成したつもりなのですが、私の解釈は間違っているのでしょうか? >>参照をなんとか取得 参照とはインスタンスを生成してここに書くということでしょうか?
fuzzball

2018/04/24 02:34

https://teratail.com/questions/122788 これと同じですよ。 新たに生成するのではなく、すでに生成されているもの(今表示されているもの)取得して下さい。
退会済みユーザー

退会済みユーザー

2018/04/24 06:23

新たに生成してもそれは取得したいものと繋がりがない為、すでにあるものを取得するのですね。 scrollViewDidScroll内に、 HogeView().setup() HogeView().horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x / 2 と書きましたが、変化ありません。
fuzzball

2018/04/24 06:28

HogeView() ← これが新しく生成してるってことなのですが。
退会済みユーザー

退会済みユーザー

2018/04/24 11:46

そうなんですね。生成してる事を分かっていませんでした。 私のこの質問https://teratail.com/questions/122788もそうなんですが、 どうやって取得するのか調べても方法が分かりません。 取得する先はfunc setup()だと思っているのですが、 どう取得すれば良いのでしょうか?
newmt

2018/04/24 21:28

ブルーのcellの中でHogeViewのインスタンスを保持する変数を持てば良いと思います。イメージとしては画面遷移で次画面にデータを渡すのと同じようなイメージです。https://qiita.com/fromage-blanc/items/3ea2dfe97d4c2d6f0646 ただ、現在の構造だとかなり入り組んでいるので何段階か値の受け渡しが発生するとは思いますが。
退会済みユーザー

退会済みユーザー

2018/04/25 06:13

newmt様 やって見ます。 何度も質問に対して回答して頂き本当に助かりました。 ご親切にありがとうございました。
guest

0

青と一緒に動くのであれば青に乗せればいいんじゃないかな‥。

投稿2018/04/24 02:33

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問