我的项目使用UICollectionView
并且UICollectionViewCell
可以通过触摸它来展开/折叠。对于此功能,我正在观察contentView.frame
属性并调整子视图的大小,并且效果很好。
问题从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;
}