背景视差滚动是基于多个 UIScrollViews 的视差滚动的简单示例。此库简单地是一个 UIView 的子类,包含两个 UIScrollView,一个是内容滚动视图,另一个是背景图片滚动视图。
而不是将源文件直接添加到您的项目中,您可能希望考虑使用 CocoaPods 来管理您的依赖项。遵循 CocoaPods 网站上的说明安装 gem,并在
Podfile
中指定FFBackgroundParallax
作为依赖项。
pod 'FFBackgroundParallax', '~> 1.1.0'
但如果您想用老式方法做这件事,只需将 FFBackgroundParallax.h/m
文件添加到项目中即可。
首先需要导入头文件
#import "FFBackgroundParallax.h"
然后定义您的总项和项图像大小
#define kImageSize 250.0
#define kTotalItems 6
将子视图正常添加到公共属性 contentScrollView
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configureBackgroundParallax];
}
- (void)configureBackgroundParallax {
FFBackgroundParallax *backgroundParallax = [[FFBackgroundParallax alloc] initWithFrame:self.view.bounds];
[backgroundParallax setImageBackground:[UIImage imageNamed:@"mountains.jpg"]];
for (NSUInteger i = 0; i < kTotalItems; i++) {
CGFloat xOrigin = (i * CGRectGetWidth(backgroundParallax.frame)) + (CGRectGetWidth(backgroundParallax.bounds) / 2) - (kImageSize / 2);
UIImageView *badge = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, (CGRectGetHeight(backgroundParallax.frame) / 2) - (kImageSize / 2), kImageSize, kImageSize)];
badge.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i", (i + 1)]];
[backgroundParallax.contentScrollView addSubview:badge];
}
[backgroundParallax.contentScrollView setContentSize:CGSizeMake(CGRectGetWidth(backgroundParallax.frame) * kTotalItems, CGRectGetHeight(backgroundParallax.frame))];
[self.view addSubview:backgroundParallax];
}
@end
要添加背景图像,只需调用
- (void)setImageBackground:(UIImage *)image
如果需要垂直兼容性,则在初始化类时应将 scrollingMode
属性更改为 FFParallaxModeVertical
FFBackgroundParallax *backgroundParallax = [[FFBackgroundParallax alloc] initWithFrame:self.view.bounds];
backgroundParallax.scrollingMode = FFParallaxModeVertical;
随时以想法、问题/请求和/或拉取请求数据进行协作。
FFBackgroundParallax 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。