使用自定义容器视图创建视差效果,类似于 Path 的日历顶视图。
示例用法可在 SampleApp
中找到。以下是其摘要
- (void)viewDidLoad {
[super viewDidLoad];
// Create backgroud image view.
UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
CGRect backgroundRect = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), backgroundImage.size.height);
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:backgroundRect];
backgroundImageView.image = backgroundImage;
backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
// Create foreground view.
CGRect foregroundRect = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 400.0f);
UIView *foregroundView = [[UIView alloc] initWithFrame:foregroundRect];
// Create parallax view with background and foreground views.
// You can see additional configuration options in the SampleApp.
MDCParallaxView *parallaxView = [[MDCParallaxView alloc] initWithBackgroundView:backgroundImageView
foregroundView:foregroundView];
parallaxView.frame = self.view.bounds;
parallaxView.backgroundHeight = 250.0f;
parallaxView.scrollView.scrollsToTop = YES;
[self.view addSubview:parallaxView];
}
偏移更新是基于在 PXParallaxViewController 中找到的。虽然该项目使用自定义的 UIViewController
,但这个项目使用自定义的 UIView
,这我相信提供了一个更好的 API。