iOS Parallax Scrolling effect (like in Yahoo News Digest app)

后端 未结 4 1255
时光说笑
时光说笑 2021-01-30 18:26

I would like to know how to implement parallax scrolling similar to Yahoo News Digest app. In which when user scroll horizontally background image scrolls in a different speed w

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 19:06

    Swift 3

    Here's how I got a parallax effect to work in Swift 3 for a vertical scroll in a tvOS app.

    In ViewDidLoad():

        parallaxScrollView.delegate = self
        detailScrollView.delegate = self
    

    And following:

    override func viewDidLayoutSubviews() {
            self.detailScrollView!.contentSize = CGSize(width: 1920, height: 2700)
            self.parallaxScrollView?.contentSize = CGSize(width: 1920, height: 3000)
        }
    
    
    //// THE SCROLL VIEW
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        // Parallax effect
        let detailMaxOffset = self.detailScrollView.contentSize.height - self.detailScrollView.frame.height;
        let percentage = self.detailScrollView.contentOffset.y / detailMaxOffset
        let parallaxMaxOffset = self.parallaxScrollView.contentSize.height - self.parallaxScrollView.frame.height;
        let parallaxOffset = CGPoint(x:0,y:(percentage * parallaxMaxOffset))
        self.parallaxScrollView.setContentOffset((parallaxOffset), animated: false)
    
    }
    

提交回复
热议问题