【发布时间】:2011-08-17 09:26:44
【问题描述】:
使用核心动画层,我一直在尝试实现以下功能。在包含超层中,有两个锚层,以及连接两者的另一个层。下图应该可以清楚地说明情况。在左侧,两个橙色锚点分别标记为“A”和“B”,绿线将它们连接起来。封闭层框架使用虚线显示。在右侧,层层次结构显示为我当前实现的那样,其中锚点和连接都是封闭超层的子层。
现在,我要做的是让锚点四处移动,并使连接保持连接状态。我目前正在利用连接层的-setFrame: 方法更新其框架和路径属性,使用如下所示的代码:
- (void)setFrame:(CGRect)frame {
CGSize size = frame.size;
CGPoint startPoint = CGPointZero;
if (size.height < 0.0) startPoint.y -= size.height;
CGPathRef oldPath = self.path;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(path, NULL, startPoint.x + size.width, startPoint.y + size.height);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = [CATransaction animationDuration];
animation.timingFunction = [CATransaction animationTimingFunction];
animation.fromValue = (id)oldPath;
animation.toValue = (id)path;
[self addAnimation:animation forKey:@"pathAnimation"];
self.path = path;
CGPathRelease(path);
[super setFrame:frame];
}
现在,这种方法有效,但问题是帧(或位置 + 边界)动画与路径动画不同步运行,导致连接远端暂时分离(以及其他一些小问题到,大概是由同一个核心问题引起的)。
我一直在玩弄这个问题,但只是为了适度的成功。有一次,我将连接的框架设置为与封闭的超层的框架相同,这确实产生了预期的效果(因为现在框架不再需要动画了)。但是,我担心此解决方案在具有多个连接的上下文中的性能 - 即。多个不透明的大尺寸重叠层似乎不好?
有人对此有更好、更优雅的解决方案吗?谢谢!
【问题讨论】:
标签: ios core-animation calayer cashapelayer