// SHTableHeaderView.swift
final class SHTableHeaderView: UIView {
/**
init ()
setSubViews ()
setSubViewLayouts() -
*/
func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = -(scrollView.contentOffset.y + scrollView.contentInset.top)
imageViewHeight.constant = max(offsetY + scrollView.contentInset.top, scrollView.contentInset.top)
}
}
setSubViewLayouts
func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = -(scrollView.contentOffset.y + scrollView.contentInset.top)
containerView.clipsToBounds = offsetY <= 0
imageViewBottom.constant = offsetY >= 0 ? 0 : -offsetY / 2 // 추가
imageViewHeight.constant = max(offsetY + scrollView.contentInset.top, scrollView.contentInset.top)
}
// ViewController.swift
private lazy var tableViewHeaderView = SHTableHeaderView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200))
@IBOutlet private weak var tableView: UITableView! {
didSet {
tableView.tableHeaderView = tableViewHeaderView
}
}
// ViewController.swift
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let headerView = self.tableView.tableHeaderView as! SHTableHeaderView
headerView.scrollViewDidScroll(scrollView: scrollView)
}
좌 - 이미지뷰를 컨테이너로 한번 더 감쌌을 때
우 - 이미지뷰 단독 설정
scrollViewDidScroll
에서 레이아웃 조절class CollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
layoutAttributes?.forEach { attribute in
if attribute.representedElementKind == UICollectionView.elementKindSectionHeader {
guard let collectionView = collectionView else { return }
let contentOffsetY = collectionView.contentOffset.y
if contentOffsetY < 0 {
let width = collectionView.frame.width
let height = attribute.frame.height - contentOffsetY
attribute.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: height)
}
}
}
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
// ...
}`
// MARK: - HeaderView
final class StretchyCollectionHeaderView: UICollectionReusableView {
// ...
// MARK: - Init
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// Create Subviews
private func createViews() {
}
/// Setup View Constraints
func setViewConstraints() {
}
/// Notify View of scroll change from container
public func scrollviewDidScroll(scrollView: UIScrollView) {
containerViewHeight.constant = scrollView.contentInset.top
let offsetY = -(scrollView.contentOffset.y + scrollView.contentInset.top)
containerView.clipsToBounds = offsetY <= 0
imageViewBottom.constant = offsetY >= 0 ? 0 : -offsetY / 2
imageViewHeight.constant = max(offsetY + scrollView.contentInset.top, scrollView.contentInset.top)
}
}
// MARK: - ViewController
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let header = homePageCollectionView.supplementaryView(forElementKind: HomePageViewController.sectionHeaderElementKind, at: IndexPath(item: 0, section: 0)) as? StretchyCollectionHeaderView {
header.scrollviewDidScroll(scrollView: homePageCollectionView)
}
}