UIMotionEffect does not work in UICollectionViewCell either

i add a motion effect to the cell sub. the first time it worked fine. but when the cell is reused. motion effect does not work ....

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

}

if I want to hide this view. and after the show. then useless motion effect

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

I am trying to use this debugging motion effect. Subview paused

po [UIView _motionEffectEngine]

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

+3
source share
1 answer

I managed to get this to work by subclassing the UICollectionViewCell and removing and reapplying the motion effect during the layout. Example:

- (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
}
+2
source

All Articles