Ios 如何从UICollectionViewController中的scrollViewDidScroll方法访问单元格成员(用于动画)?

Ios 如何从UICollectionViewController中的scrollViewDidScroll方法访问单元格成员(用于动画)?,ios,xcode11,swift5,Ios,Xcode11,Swift5,如何从UICollectionViewController中的scrollViewDidScroll方法访问添加到UICollectionViewCell中视图的成员(UIImage或UIExtView) 我想设置动画,即在垂直滚动到下一个单元格时以“不同的速度”移动图像和文本 我知道这可以在scrollViewDidScroll方法中完成,但我不知道如何访问成员 视图控制器: class OnboardingViewController: UICollectionViewController,

如何从UICollectionViewController中的scrollViewDidScroll方法访问添加到UICollectionViewCell中视图的成员(UIImage或UIExtView)

我想设置动画,即在垂直滚动到下一个单元格时以“不同的速度”移动图像和文本

我知道这可以在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),
        ])
    }

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")
    }

}
这是页面单元格:

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),
        ])
    }

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")
    }

}

您可以通过获取当前收藏视图的可见单元格来实现这一点,如果您在ScrollViewDiEndDecelling中访问这些单元格,那就太好了

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
        }
 }

你要寻找的可能是视差效应,试着搜索它