如何从 UICollectionViewController 中的 scrollViewDidScroll 方法访问单元格成员(用于动画)?
Posted
技术标签:
【中文标题】如何从 UICollectionViewController 中的 scrollViewDidScroll 方法访问单元格成员(用于动画)?【英文标题】:How to access cell members (for animation) from scrollViewDidScroll method in UICollectionViewController? 【发布时间】:2019-10-08 09:46:53 【问题描述】:如何通过 UICollectionViewController 中的 scrollViewDidScroll 方法访问添加到 UICollectionViewCell 中的视图的成员(UIImage 或 UITextView)?
我想制作动画,即在垂直滚动到下一个单元格时以“不同的速度”移动图像和文本。
我知道这可以在 scrollViewDidScroll 方法中完成,但我不知道如何访问成员。
视图控制器:
class OnboardingViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
override func scrollViewDidScroll(_ scrollView: UIScrollView)
**code here which I just can't figure out....**
override func viewDidLoad()
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
collectionView?.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
// this method "creates" the UIPageControll and assigns
setupPageControl()
lazy var pageControl: UIPageControl =
let pageControl = UIPageControl()
pageControl.currentPage = 0
pageControl.numberOfPages = data.count <--- data provided by a model from a plist - works perfectly
pageControl.currentPageIndicatorTintColor = .black
pageControl.pageIndicatorTintColor = .gray
pageControl.translatesAutoresizingMaskIntoConstraints = false
return pageControl
()
private func setupPageControl()
view.addSubview(pageControl)
NSLayoutConstraint.activate([
onboardingPageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
onboardingPageControl.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
onboardingPageControl.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
])
所有其他覆盖方法都在扩展中实现并且工作正常,即
numberOfItemsInSection section: Int) -> Int
return data.count
这是 PageCell:
class PageCell: UICollectionViewCell
var myPage: MyModel?
didSet
guard let unwrappedPage = myPage else return
// the image in question:
myImage.image = UIImage(named: unwrappedPage.imageName)
myImage.translatesAutoresizingMaskIntoConstraints = false
myImage.contentMode = .scaleAspectFit
// the text in question
let attributedText = NSMutableAttributedString(string: unwrappedPage.title, attributes: [:])
attributedText.append(NSAttributedString(string: "\n\(unwrappedPage.description)", attributes: [:]))
myText.attributedText = attributedText
myText.translatesAutoresizingMaskIntoConstraints = false
myText.textColor = .black
myText.textAlignment = .center
myText.isEditable = false
myText.isScrollEnabled = false
myText.isSelectable = false
let myImage: UIImageView =
let imageView = UIImageView()
return imageView
()
let myText: UITextView =
let textView = UITextView()
return textView
()
fileprivate func setup()
addSubview(myImage)
NSLayoutConstraint.activate([
myImage.safeAreaLayoutGuide.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 60),
myImage.centerXAnchor.constraint(equalTo: centerXAnchor),
myImage.leadingAnchor.constraint(equalTo: leadingAnchor),
myImage.trailingAnchor.constraint(equalTo: trailingAnchor),
myImage.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.4)
])
addSubview(myText)
NSLayoutConstraint.activate([
myText.topAnchor.constraint(equalTo: myImage.bottomAnchor, constant: 16),
myText.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
myText.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16)
])
override init(frame: CGRect)
super.init(frame: frame)
setup()
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
【问题讨论】:
你要找的可能是视差效果,试着搜索一下 【参考方案1】:您可以通过获取 collectionView 的当前可见单元格来实现这一点,如果您在 scrollViewDidEndDecelerating 中访问它们会很棒,而不是scrollViewDidScroll。
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
for cell in collectionView.visibleCells
//cell.imageView
//cell.txtView
// you can access both imageView and txtView here
let indexPath = collectionView.indexPath(for: cell)
print(indexPath) // this will give you indexPath as well to differentiate
【讨论】:
以上是关于如何从 UICollectionViewController 中的 scrollViewDidScroll 方法访问单元格成员(用于动画)?的主要内容,如果未能解决你的问题,请参考以下文章
如何从外部从 GitHub 加载 JavaScript 文件? [复制]