iOS 7 parallax effect in my view controller

前端 未结 9 1872
终归单人心
终归单人心 2020-11-30 16:27

I\'m developing an app for iOS 7 in Objective-C. I\'ve got a screen in my app with a few buttons and a pretty background image. (It\'s a simple xib with UIButtons

9条回答
  •  有刺的猬
    2020-11-30 16:30

    This will help someone who is looking to implement parallax for tableView or collectionView.

    • first of all create the cell for the tableview and put the image view in it.

    • set the image height slightly more than the cell height. if cell height = 160 let the image height be 200 (to make the parallax effect and you can change it accordingly)

    • put this two variable in your viewController or any class where your tableView delegate is extended

    let imageHeight:CGFloat = 150.0
    let OffsetSpeed: CGFloat = 25.0
    
    • add the following code in the same class
     func scrollViewDidScroll(scrollView: UIScrollView) {
        //  print("inside scroll")
    
        if let visibleCells = seriesTabelView.visibleCells as? [SeriesTableViewCell] {
            for parallaxCell in visibleCells {
                var yOffset = ((seriesTabelView.contentOffset.y - parallaxCell.frame.origin.y) / imageHeight) * OffsetSpeedTwo
                parallaxCell.offset(CGPointMake(0.0, yOffset))
            }
        }
    }
    
    • where seriesTabelView is my UItableview

    • and now lets goto the cell of this tableView and add the following code

    func offset(offset: CGPoint) {
            posterImage.frame = CGRectOffset(self.posterImage.bounds, offset.x, offset.y)
        }
    
    • were posterImage is my UIImageView

    If you want to implement this to collectionView just change the tableView vairable to your collectionView variable

    and thats it. i am not sure if this is the best way. but it works for me. hope it works for you too. and let me know if there is any problem

提交回复
热议问题