您可以通过在物理设备上下载并运行以下代码来重现该错误:https : //github.com/Hoya/RCCPeakableImageView
它的作用是通过UIImageView子类向图像添加视差效果。尝试克隆,构建项目,如果倾斜/平移手机,则图像会稍微动起来以产生视差效果。
UIImageView子类创建一个内部UIImageView,其框架比其自身稍大,并通过UIView的addMotionEffect添加UIInterpolatingMotionEffects:非常简单。
当使用UIScrollView和UITableView时,此方法效果很好,但是由于某些原因,滚动UICollectionView时,重用的UICollectionViewCells将失去运动效果。请注意,有趣的是,一旦切换到UIScrollView或UITableView演示并切换回UICollectionView演示,动作效果将再次开始起作用。
有没有解决的办法?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
RCCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
static NSString *identifier = @"Cell";
RCCPeakableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier
forIndexPath:indexPath];
[cell.imageView setImage:appDelegate.images[indexPath.row]];
[cell.imageView setPadding:CGPointMake(20.f, 20.f)];
return cell;
}
@interface RCCPeakableCollectionViewCell : UICollectionViewCell
@property (nonatomic, weak) IBOutlet RCCPeakableImageView *imageView;
@end
您的代码中有一个问题:
[cell.imageView setImage:appDelegate.images[indexPath.row]];
[cell.imageView setPadding:CGPointMake(20.f, 20.f)];
在这里,setImage:
将在图像视图上添加一些运动效果,然后setPadding:
将其再次添加。
我对RCCPeakableImageView
类进行了一些修改,将添加的运动效果代码移至layoutSubviews
,并且在重用的UICollectionViewCell上运行良好。以下是更新的代码:
- (void)setImage:(UIImage *)image
{
// Store image
self.RCC_imageView.image = image;
[self setNeedsLayout];
}
- (void)setPadding:(CGPoint)padding
{
// Store padding value
_padding = padding;
[self setNeedsLayout];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Remove exising motion effects
for (UIMotionEffect *effect in [self.RCC_imageView motionEffects]) {
[self.RCC_imageView removeMotionEffect:effect];
}
// Add motion effects if there is any image.
UIImage *image = self.RCC_imageView.image;
if (image) {
// code that add motion effects
// ...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句