前提・実現したいこと
いつもお世話になっております。
現在scrollViewを用いて横スクロールに関して勉強中です。
TableViewを横スクロールした時に、各TableViewの位置情報を記録しておき、その地点からスクロールをスタートさせたいです。
該当のソースコード
func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView == self.horizontalScrollView) { barLeftConstraint.constant = scrollView.contentOffset.x/4 } else { print(scrollView.contentOffset) headerTopConstraint.constant = max(-(scrollView.contentOffset.y + 300),-300+50) tableView0.contentOffset.y = scrollView.contentOffset.y tableView1.contentOffset.y = scrollView.contentOffset.y tableView2.contentOffset.y = scrollView.contentOffset.y tableView3.contentOffset.y = scrollView.contentOffset.y } }
試したこと
tableView0.contentOffset.y = scrollView.contentOffset.y tableView1.contentOffset.y = scrollView.contentOffset.y tableView2.contentOffset.y = scrollView.contentOffset.y tableView3.contentOffset.y = scrollView.contentOffset.y
を追加することによって、TableView0でy軸にスクロールし、その後TableView1,2,3横スクロールを行うとTableViewのy軸が引き継がれ、その地点からのスクロールになります。
実現したい動作としては、各TableViewのy軸の位置情報は各々が保持し、スクロールした際に各Tableviewが最後にスクロールしたy軸の地点からスクロールを開始したいです。
サイトを見ても解決に至らなかったので、ご教授いただけましたら幸いです。
何卒宜しくお願いします。
補足情報(FW/ツールのバージョンなど)
xcode9.2
質問の補足
こちらのプログラムは以前回答していただいた方の参考プログラムとなっておりますが、スクロール部分だけ写して誤解させてしまいまして申し訳ございません。
tableView0.contentOffset.y = scrollView.contentOffset.y tableView1.contentOffset.y = scrollView.contentOffset.y tableView2.contentOffset.y = scrollView.contentOffset.y tableView3.contentOffset.y = scrollView.contentOffset.y
の記述を削除するとy軸にある程度スクロールしてから横スクロールすると、下記の画像のようにヘッダー部分のViewが表示されてしまいます。
このバグ対策として、上記を入れましたが目的の達成には至りませんでした。
全文はこのようになっております。
import UIKit class ViewController: UIViewController { @IBOutlet weak var horizontalScrollView: UIScrollView! @IBOutlet weak var tableView0: UITableView! @IBOutlet weak var tableView1: UITableView! @IBOutlet weak var tableView2: UITableView! @IBOutlet weak var tableView3: UITableView! @IBOutlet weak var iconView: UIView! @IBOutlet weak var headerTopConstraint: NSLayoutConstraint! @IBOutlet weak var barLeftConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() [tableView0,tableView1,tableView2,tableView3].forEach { $0?.delegate = self $0?.dataSource = self $0?.contentInset = UIEdgeInsetsMake(300, 0, 0, 0) } horizontalScrollView.delegate = self iconView.clipsToBounds = true iconView.layer.cornerRadius = iconView.frame.width/2 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } extension ViewController:UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 100 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let screenRect0: CGSize = UIScreen.main.bounds.size tableView0.frame = CGRect(x: UIScreen.main.bounds.width * 0.0, y: 0, width: screenRect0.width, height: screenRect0.height) let screenRect1: CGSize = UIScreen.main.bounds.size tableView1.frame = CGRect(x: UIScreen.main.bounds.width * 1.0, y: 0, width: screenRect1.width, height: screenRect1.height) let screenRect2: CGSize = UIScreen.main.bounds.size tableView2.frame = CGRect(x: UIScreen.main.bounds.width * 2.0, y: 0, width: screenRect2.width, height: screenRect2.height) let screenRect3: CGSize = UIScreen.main.bounds.size tableView3.frame = CGRect(x: UIScreen.main.bounds.width * 3.0, y: 0, width: screenRect3.width, height: screenRect3.height) let random:Int = Int(arc4random() % 9) let text = ["????","????","????","????","????","????","????","????","????"][random] let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = text if tableView == tableView0 { cell.contentView.backgroundColor = UIColor(red: 1, green: 0.9, blue: 0.9, alpha: 1) } if tableView == tableView1 { cell.contentView.backgroundColor = UIColor(red: 0.9, green: 1, blue: 0.9, alpha: 1) } if tableView == tableView2 { cell.contentView.backgroundColor = UIColor(red: 1, green: 1, blue: 0.9, alpha: 1) } if tableView == tableView3 { cell.contentView.backgroundColor = UIColor(red: 0.9, green: 0.5, blue: 0.9, alpha: 1) } return cell } func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView == self.horizontalScrollView) { barLeftConstraint.constant = scrollView.contentOffset.x/4 } else { print(scrollView.contentOffset) headerTopConstraint.constant = max(-(scrollView.contentOffset.y + 300),-300+50) } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/24 05:56
2018/11/24 15:02
2018/11/26 07:26