Ios 如何在动画CALayer';s阴影路径特性
我的项目使用Ios 如何在动画CALayer';s阴影路径特性,ios,caanimation,Ios,Caanimation,我的项目使用UICollectionView和UICollectionViewCell可以通过触摸来展开/折叠。对于这个函数,我观察了contentView.frameproperty和resize子视图,效果很好 问题开始于UICollectionViewCell使用CALayer进行阴影处理。所以我必须用CAAnimation调整阴影的大小,就像同一个单元格的框架一样 但是CAAnimation在动画过程中,当我触摸电池时,会使坏访问崩溃 当然,我尝试过使用userInteractionEn
UICollectionView
和UICollectionViewCell
可以通过触摸来展开/折叠。对于这个函数,我观察了contentView.frame
property和resize子视图,效果很好
问题开始于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;
}
如果您想停止任何用户交互(实际上您将停止所有操作),请使用阻塞主线程选择器调用您的问题方法:
[self performSelectorOnMainThread:@selector(blockingMethod:) withObject:blockingMethodParam waitUntilDone:YES]
通过这种方式,您可以确保所有操作都停止,直到blockingMethod完全执行为止。然而,您的概念方法不是很好,因为没有用户希望它的UI被阻止,尤其是在没有等待屏幕的情况下
参考:
问候,
hris.to建议的解决方案在我的情况下都不起作用。我的应用程序是基于导航控制器的 在设置动画之前关闭导航控制器视图上的用户交互,修复了崩溃问题
self.navigationController.view.userInteractionEnabled = false;
您无法(安全地)观察视图的帧。此外,您的observeValueForKeyPath:
代码已损坏。