2013-08-02 38 views
0

我的項目使用UICollectionViewUICollectionViewCell可以通過觸摸進行展開/摺疊。對於這個功能,我正在觀察contentView.frame屬性並調整子視圖的大小,它的效果很好。如何在動畫化CALayer的shadowPath屬性期間禁用用戶交互

問題從UICollectionViewCell開始有陰影使用CALayer。所以我必須將CAAnimation的影子調整爲同一個單元的幀。

但是CAAnimation當我在動畫過程中觸摸細胞重複時會造成訪問失敗。

當然,我試過使用userInteractionEnabled屬性和動畫委託,但它不起作用。

誰有什麼想法?

觀察代碼:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if ([self.contentView isEqual:object]) { 
     // Change subviews frame 
     // Code.... 

     // Shadow Path 
     self.userInteractionEnabled = NO; 

     CGPathRef newShadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:kCornerRadius].CGPath; 
     NSArray *animationKeys = [self.layer animationKeys]; 

     if ([animationKeys count]&&[[animationKeys objectAtIndex:0] isKindOfClass:[NSString class]]) { 

      NSString *animationKey = [animationKeys objectAtIndex:0]; 
      CAAnimation *animation = [self.layer animationForKey:animationKey]; 

      CABasicAnimation *newAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
      newAnimation.duration = animation.duration; 
      newAnimation.toValue = [NSValue valueWithPointer:newShadowPath]; 
      newAnimation.timingFunction = animation.timingFunction; 
      newAnimation.delegate = self; 
      newAnimation.removedOnCompletion = NO; 

      [self.layer addAnimation:newAnimation forKey:@"shadowPath"]; 
     } 
     self.layer.shadowPath = newShadowPath; 
    } 
} 

和動畫的代表在這裏:

#pragma mark - CAAnimation delegate 
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { 
    self.userInteractionEnabled = YES; 
} 
+0

你不能(安全)KVO觀察視圖的框架。另外,您的'observeValueForKeyPath:'代碼已損壞。 –

回答

0

如果你想停止任何用戶交互(實際上,你會停止一切)與阻塞主線程選擇調用您的問題的方法:

[self performSelectorOnMainThread:@selector(blockingMethod:) withObject:blockingMethodParam waitUntilDone:YES] 

這樣,你確保一切都停止,直到阻止方法完全執行。然而,你的概念方法並不是很好,因爲沒有用戶想要它的UI被阻止,特別是當沒有某種等待屏幕時。

參考:

– performSelectorOnMainThread:withObject:waitUntilDone:

問候,

hris.to

相關問題