ストーリーボード未使用で開発しています。
画像を見ていだだきたいのですが、
・ブラックのホリゾンタルバーを乗せたパープル部分の階層は
⬇︎の順番です。
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 } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/24 02:27
2018/04/24 02:34
退会済みユーザー
2018/04/24 06:23
2018/04/24 06:28
退会済みユーザー
2018/04/24 11:46
2018/04/24 21:28
退会済みユーザー
2018/04/25 06:13