UIMotionEffect 同时在 UICollectionViewCell 中不起作用



>我在单元格子视图上添加了运动效果。第一次工作正常。但是当细胞重复使用时。运动效果不起作用。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
testcell *processingCell = (testcell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    horizontalMotionEffect.minimumRelativeValue = @(-kMotionEffectRelativeValue );
    horizontalMotionEffect.maximumRelativeValue = @(kMotionEffectRelativeValue );
    UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    verticalMotionEffect.minimumRelativeValue = @(-kMotionEffectRelativeValue );
    verticalMotionEffect.maximumRelativeValue = @(kMotionEffectRelativeValue );
    group = [[UIMotionEffectGroup alloc] init];
    group.motionEffects = @[horizontalMotionEffect,verticalMotionEffect];
});
if (!processingCell.coustomView) {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.center = processingCell.contentView.center;
    view.backgroundColor =[UIColor blackColor];
    [processingCell addSubview:view];
    processingCell.coustomView = view;
}
processingCell.coustomView.hidden = YES;
processingCell.coustomView.hidden = NO;
[processingCell.coustomView addMotionEffect:group];
return processingCell;

}

如果我想隐藏这个子视图。 并在显示它之后. 那么运动效果就没用了

processingCell.coustomView.hidden = YES;
processingCell.coustomView.hidden = NO;

我尝试使用此调试动作效果.子视图已暂停

po [UIView _motionEffectEngine]

https://github.com/sipdar/parallax-effect-Bug

我能够通过子类化UICollectionViewCell并在布局期间删除和重新应用运动效果来使其工作。例:

- (void) layoutSubviews{
    [super layoutSubviews];
    [self addMotionEffectToView:self.contentView];
}
- (void) addMotionEffectToView:(UIView*) view{
    for (UIMotionEffect* effect in [view.motionEffects copy]){
        [view removeMotionEffect:effect];
    }
    ... add your motion effect
}

相关内容

  • 没有找到相关文章

最新更新