scrollViewDidScrollでセルのフリックと同時にUIViewを動かしたい。
- 評価
- クリップ 0
- VIEW 938

退会済みユーザー
ストーリーボード未使用で開発しています。
画像を見ていだだきたいのですが、
・ブラックのホリゾンタルバーを乗せたパープル部分の階層は
⬇︎の順番です。
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
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let hogeView = HogeView() //⚠️
}
この時点で新しいインスタンスを生成しているので、画像のブラックのUIViewとは繋がりがありません。
ブラックの部分に対する参照をなんとか取得できれば連動できると思います。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
青と一緒に動くのであれば青に乗せればいいんじゃないかな‥。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.35%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2018/04/24 11:27
>>繋がりがありません。
繋がりを持たす為に生成したつもりなのですが、私の解釈は間違っているのでしょうか?
>>参照をなんとか取得
参照とはインスタンスを生成してここに書くということでしょうか?
2018/04/24 11:34
これと同じですよ。
新たに生成するのではなく、すでに生成されているもの(今表示されているもの)取得して下さい。
2018/04/24 15:23
scrollViewDidScroll内に、
HogeView().setup()
HogeView().horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x / 2
と書きましたが、変化ありません。
2018/04/24 15:28
2018/04/24 20:46
私のこの質問https://teratail.com/questions/122788もそうなんですが、
どうやって取得するのか調べても方法が分かりません。
取得する先はfunc setup()だと思っているのですが、
どう取得すれば良いのでしょうか?
2018/04/25 06:28
ただ、現在の構造だとかなり入り組んでいるので何段階か値の受け渡しが発生するとは思いますが。
2018/04/25 15:13
やって見ます。
何度も質問に対して回答して頂き本当に助かりました。
ご親切にありがとうございました。