Ole*_*ann 40 animation core-animation cashapelayer ios
我有一个CAShapeLayer(它是UIView子类的一层),path
只要视图的bounds
大小发生变化,它就会更新.为此,我重写了图层setBounds:
重置路径的方法:
@interface CustomShapeLayer : CAShapeLayer
@end
@implementation CustomShapeLayer
- (void)setBounds:(CGRect)bounds
{
[super setBounds:bounds];
self.path = [[self shapeForBounds:bounds] CGPath];
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,直到我为边界变化设置动画.我希望在任何动画边界变化(在UIView动画块中)中创建路径动画,以便形状图层始终采用其大小.
由于该path
属性默认没有动画,我想出了这个:
覆盖图层的addAnimation:forKey:
方法.在其中,确定是否将边界动画添加到图层.如果是这样,通过复制传递给方法的边界动画中的所有属性,创建第二个显式动画,以便为边界旁边的路径设置动画.在代码中:
- (void)addAnimation:(CAAnimation *)animation forKey:(NSString *)key
{
[super addAnimation:animation forKey:key];
if ([animation isKindOfClass:[CABasicAnimation class]]) {
CABasicAnimation *basicAnimation = (CABasicAnimation *)animation;
if ([basicAnimation.keyPath isEqualToString:@"bounds.size"]) {
CABasicAnimation *pathAnimation = [basicAnimation copy];
pathAnimation.keyPath = @"path";
// The path property has not been updated to the new value yet
pathAnimation.fromValue = (id)self.path;
// Compute the new value for path
pathAnimation.toValue = (id)[[self shapeForBounds:self.bounds] CGPath];
[self addAnimation:pathAnimation forKey:@"path"];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从阅读DavidRönnqvist的View-Layer Synergy文章中得到了这个想法.(此代码适用于iOS 8.在iOS 7上,您似乎必须检查动画keyPath
,@"bounds"
而不是`@"bounds.size".)
触发视图动画边界更改的调用代码如下所示:
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.3 initialSpringVelocity:0.0 options:0 animations:^{
self.customShapeView.bounds = newBounds;
} completion:nil];
Run Code Online (Sandbox Code Playgroud)
这主要是有效的,但我有两个问题:
偶尔,当前一个动画仍在进行 编辑:没关系.我忘了转换EXC_BAD_ACCESS
中CGPathApply()
时触发此动画时,我会在()中遇到崩溃.我不确定这是否与我的特定实现有关.UIBezierPath
成CGPathRef
.谢谢@antonioviero!
使用标准UIView弹簧动画时,视图边界的动画和图层的路径稍微不同步.也就是说,路径动画也以弹性方式执行,但它并不完全遵循视图的界限.
更一般地说,这是最好的方法吗?似乎有一个形状图层,其路径依赖于其边界大小,并且应该与任何边界变化同步动画,这应该只是工作,但我很难.我觉得必须有更好的方法.
actionForKey:
或视图actionForLayer:forKey:
,以便返回path
属性的自定义动画对象.我认为这将是首选方式,但我没有找到一种方法来获取应由封闭动画块设置的事务属性.即使从动画块内部调用,[CATransaction animationDuration
]等也始终返回默认值.有没有办法(a)确定你当前在动画块内,以及(b)获得在该块中设置的动画属性(持续时间,动画曲线等)?
这是动画:橙色三角形是形状图层的路径.黑色轮廓是托管形状图层的视图的框架.
看一下GitHub上的示例项目.(此项目适用于iOS 8,需要运行Xcode 6,抱歉.)
Jose Luis Piedrahita 向我指出Nick Lockwood撰写的这篇文章,其中提出了以下方法:
覆盖视图actionForLayer:forKey:
或图层的actionForKey:
方法,并检查传递给此方法的键是否是您要设置动画的键(@"path"
).
如果是这样,调用super
图层的"普通"可动画属性之一(例如@"bounds"
)将隐式告诉您是否在动画块内.如果视图(图层的委托)返回一个有效的对象(而不是nil
或NSNull
),我们就是.
从[super actionForKey:]
返回的动画中设置路径动画的参数(持续时间,计时功能等)并返回路径动画.
这确实在iOS 7.x下运行良好.但是,在iOS 8下,返回的对象actionForLayer:forKey:
不是标准(并记录),CA...Animation
而是私有_UIViewAdditiveAnimationAction
类(NSObject
子类)的实例.由于没有记录此类的属性,因此我无法轻松地使用它们来创建路径动画.
_Edit:或者它可能只是起作用.正如尼克在他的文章中提到的,一些属性backgroundColor
仍然CA...Animation
会在iOS 8上恢复标准.我将不得不尝试一下
我知道这是一个老问题,但是我可以为您提供一个类似于洛克伍德先生的方法的解决方案。可悲的是,这里的源代码很快,因此您需要将其转换为ObjC。
如前所述,如果该层是视图的支持层,则可以在视图本身中拦截CAAction。但是,例如,如果在一个以上的视图中使用了衬里层,这将不方便。
好消息是actionForLayer:forKey:实际上在支持层中调用actionForKey:。
它位于actionForKey中:在支持层中,我们可以在其中拦截这些调用并为更改路径提供动画。
快速编写的示例图层如下:
class AnimatedBackingLayer: CAShapeLayer
{
override var bounds: CGRect
{
didSet
{
if !CGRectIsEmpty(bounds)
{
path = UIBezierPath(roundedRect: CGRectInset(bounds, 10, 10), cornerRadius: 5).CGPath
}
}
}
override func actionForKey(event: String) -> CAAction?
{
if event == "path"
{
if let action = super.actionForKey("backgroundColor") as? CABasicAnimation
{
let animation = CABasicAnimation(keyPath: event)
animation.fromValue = path
// Copy values from existing action
animation.autoreverses = action.autoreverses
animation.beginTime = action.beginTime
animation.delegate = action.delegate
animation.duration = action.duration
animation.fillMode = action.fillMode
animation.repeatCount = action.repeatCount
animation.repeatDuration = action.repeatDuration
animation.speed = action.speed
animation.timingFunction = action.timingFunction
animation.timeOffset = action.timeOffset
return animation
}
}
return super.actionForKey(event)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10706 次 |
最近记录: |