0
我的項目使用UICollectionView
和UICollectionViewCell
可以通過觸摸進行展開/摺疊。對於這個功能,我正在觀察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;
}
你不能(安全)KVO觀察視圖的框架。另外,您的'observeValueForKeyPath:'代碼已損壞。 –