UITableViewCell 的子类,用于展示视差效果。MMParallaxCell 是一个即插即用的解决方案,基本上您不需要进行任何配置,但您也可以自行进行定制。
此外,您不需要在 UITableView 的 Delegate 方法中添加额外的代码,它使用起来就像 UITableViewCell 一样自然 :)
演示项目中的视差效果
首选的安装方式是通过 CocoaPods。只需添加
pod 'MMParallaxCell'
然后运行 pod install
。这将安装 MMParallaxCell 的最新版本。
如果您想要使用 MMParallaxCell 的最新代码,使用
pod 'MMParallaxCell', :head
很简单,只需设置要显示的图片。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"CellIdentifier";
MMParallaxCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MMParallaxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.parallaxImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://lorempixel.com/400/400/technics/%ld/",indexPath.row]]];
return cell;
}
并且您可以更改视差效果的比率。
@property (nonatomic, assign) CGFloat parallaxRatio; //ratio of cell height, should between [1.0f, 2.0f], default is 1.5f;
v1.1 修复由 "removeObserver" 导致的崩溃
v1.0 您可以自定义或直接使用它
@interface MMParallaxCell : UITableViewCell
@property (nonatomic, strong) UIImageView *parallaxImage;
@property (nonatomic, assign) CGFloat parallaxRatio; //ratio of cell height, should between [1.0f, 2.0f], default is 1.5f;
@end