2014-02-26 27 views
3

我在单元格子视图上添加运动效果。第一次它的工作正常。 但是当单元重新使用。运动效果不行....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; 

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

PO [UIView的_motionEffectEngine]

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

+0

您应该提供更多信息,例如:你有什么尝试,什么是错误,日志,... – Kay

+0

为什么它的价值,我做了非常相似的事情,也发现它不起作用。你的代码并没有防范多次添加到单元格中的相同运动效果(当单元格被重用时),你应该这样做。我正在这样做,但我仍然遇到同样的问题。 – Benjohn

回答

2

我能得到这个通过继承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 
} 
+0

好的提示。当你重新进入屏幕时不要忘记重新加载可见的单元格。 – FBente

相关问题