2014-02-26 5 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; 
쓸모 모션 효과는 내가 국지적 인 서브 뷰가

포 일시 중단이 디버그 모션 효과를 사용하려고 [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