前提・実現したいこと
sectionHeaderのUIlabelがスクロール時にフェードインするのを防ぎたいです。
スクロールの速さに関わらず横からフェードインします。
横からフェードインするSectionはバラバラです。画像では栃木が横からフェードインしていますが、しない時もあり、どういった法則で横からフェードインするSectionHeaderが決まっているのか理解できていません。
titleForHeaderInSection(tableViewDelegate)でSectionのtitleを渡した場合は、正常に動きました。
回答のほどよろしくお願いします。
##MainController
import UIKit import Firebase public let Prefectures = [ "北海道", "青森", "秋田",//割愛 ] public let tableCell = "MainTableCell" public let collectionCell = "CollectionCell" private let sectionReuseIdentifier = "SectionHeader" class MainController: UIViewController { //MARK: - Properties private var scrollUpDirection: Bool = false private let collectionSectionInSets: UIEdgeInsets = .init(top: 0, left: 10, bottom: 0, right: 10) private var prevcontentOffSet: CGPoint = .init(x: 0, y: 0) private let headerAnimationSpeed: CGFloat = 12 private let headerHeight: CGFloat = 120 private var headerViewHeightConstraint: NSLayoutConstraint! private lazy var screenSize = self.view.bounds private let tableView = UITableView(frame: .zero, style: .plain) private let headerView:MainHeaderView = { let view = MainHeaderView() view.backgroundColor = .systemPurple return view }() //MARK: - View LifeCycle override func viewDidLoad() { super.viewDidLoad() checkFormStates() } override var prefersStatusBarHidden: Bool { return scrollUpDirection } override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { return .slide } //MARK: - Helpers func configureUI() { configureHeaderView() configureTableView() } fileprivate func configureHeaderView() { view.addSubview(headerView) headerView.anchor(top: view.topAnchor, left: view.leftAnchor, right: view.rightAnchor, width: screenSize.width) headerViewHeightConstraint = headerView.heightAnchor.constraint(equalToConstant: headerHeight) headerViewHeightConstraint.priority = UILayoutPriority(600) headerViewHeightConstraint.isActive = true } fileprivate func configureTableView() { tableView.delegate = self tableView.dataSource = self tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableCell) tableView.rowHeight = 200 tableView.tableFooterView = UIView() tableView.register(MainTableCell.self, forCellReuseIdentifier: tableCell) tableView.register(SectionHeaderView.self, forHeaderFooterViewReuseIdentifier: sectionReuseIdentifier) tableView.contentInsetAdjustmentBehavior = .never view.addSubview(tableView) tableView.anchor(top: headerView.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, bottom: view.bottomAnchor) } func checkFormStates() { if Auth.auth().currentUser?.uid != nil && Auth.auth().currentUser?.isEmailVerified == true{ configureUI() }else{ DispatchQueue.main.async { let controller = SignInController() controller.modalPresentationStyle = .fullScreen self.present(controller, animated: true, completion: nil) } } } fileprivate func animationStatusBar() { UIView.animate(withDuration: 0.3) { self.setNeedsStatusBarAppearanceUpdate() } } } //MARK: - UITableView Datasource delegate extension MainController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionReuseIdentifier) let sectionHeader = SectionHeaderView(reuseIdentifier: sectionReuseIdentifier) sectionHeader.prefecturesName = Prefectures[section] return sectionHeader } func numberOfSections(in tableView: UITableView) -> Int { return Prefectures.count } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { guard let cell = cell as? MainTableCell else { return } cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.section) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: tableCell, for: indexPath) return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } } //MARK: CollectionView Delegate DataSource extension MainController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionCell, for: indexPath) as! MainCollectionCell return cell } } //MARK: - CollectionView Flowlayout extension MainController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 200, height: 180) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return collectionSectionInSets } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return collectionSectionInSets.left } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return collectionSectionInSets.left } }
##SectionHeaderView
import UIKit class SectionHeaderView: UITableViewHeaderFooterView { //MARK: - Properties internal var prefecturesName: String? { didSet { guard let prefecturesName = prefecturesName else { return } titleLabel.text = prefecturesName } } private lazy var titleLabel: UILabel = { let label = UILabel() label.font = UIFont.boldSystemFont(ofSize: 24) label.textAlignment = .center label.textColor = .black return label }() private let showButton: UIButton = { let button = UIButton(type: .system) button.setTitle("全て見る", for: .normal) button.titleLabel?.textColor = .systemBlue return button }() //MARK: - View LifeCycle override init(reuseIdentifier: String?) { super.init(reuseIdentifier: reuseIdentifier) setHeight(height: 60) } override func layoutSubviews() { let view = UIView() view.frame = self.frame view.backgroundColor = .white backgroundView = view self.addSubview(titleLabel) titleLabel.centerY(inView: self) titleLabel.anchor(left: leftAnchor, padddingLeft: 16) addSubview(showButton) showButton.centerY(inView: self) showButton.anchor(right: rightAnchor, paddingRight: -16) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
補足情報(FW/ツールのバージョンなど)
swift5 xcode 11.6
##追記
1000文字を超えてしまうので不要なコードを添削しました。
sectionHeaderViewを登録して使いまわす様に変更しました。
回答1件
あなたの回答
tips
プレビュー