ios - UIMotionEffect 同时在 UICollectionViewCell 中不起作用

标签 ios ios7 parallax uicollectionviewcell core-motion

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

- (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;

如果我想隐藏这个 subview 。展示之后。那么动效就没用了

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

我尝试使用这个调试 Action 效果。 subview 被暂停

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
}

关于ios - UIMotionEffect 同时在 UICollectionViewCell 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22038302/

相关文章:

html - 你可以将一个div的信息设置为另一个div的背景图像吗?

javascript - Skrollr - 暂停滚动时有关数据属性的问题

objective-c - 来自同一文件的不同 md5 总和

ios - 在插入 CoreData 之前检查文本字段是否为空

ios - 如何使用由枚举组成的可变参数绑定(bind)方法

crash - Xcode iOS间歇性崩溃

html - 如何使用 CSS 为部分提供不随页面滚动的背景?

ios - Swift:容器 View 布局在旋转和再次旋转回来时发生变化

ios - UIPickerView - 无法在 ios7 中点击项目进行选择

ios7 - 当手机联系人与 iOS 中的 Facebook 好友联系人同步时,应用程序崩溃