2014-10-17 76 views
0

我見過很多關於創建視差效果的教程,但是其中大多數教程似乎都只有一個圖像和一個UITableView或使用加速度計。我沒有遇到一個教程,顯示如何使用UIScrollView來完成它。滾動視圖上的Parralax效果

我期待的效果可以在Yik Yak和Two Dots中看到。任何教程/ githubs,因爲我找不到它們?

所以要根據滾動位置尋找要移動的對象。

enter image description hereenter image description here

回答

0

我在一個單一的背景圖像的UITableView的實現視差,但你可能只是通過所有對象循環,請根據您的看法如何「深」是不同的因素。

這只是它是如何基本上完成了,但我敢肯定,你就可以輕鬆地我的代碼轉換到您的項目:

// of cause, you have to have your view controller as delegate for the scroll view 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGRect backgroundImageFrame = [_backgroundImageView frame]; 
    backgroundImageFrame.origin.y -= (scrollView.contentOffset.y - _previousY)/2.25; 
    //  play around with different factors here to meet your needs here  ^^^^ 

    CGFloat scaledImageHeight = _backgroundImageView.image.size.height * _backgroundImageView.scaledImageSize.height; 
    CGFloat relativeImageY = _backgroundImageView.superview.frame.size.height - backgroundImageFrame.origin.y; 

    if ((scaledImageHeight - relativeImageY + 49) >= 0) { 
    // this is just a bit extra padding ^^ 
     _previousY = scrollView.contentOffset.y; 
     [_backgroundImageView setFrame:backgroundImageFrame]; 
    } 
}