objective-c - 如何为 UIBezierPath 设置动画

标签 objective-c animation calayer cgpath uibezierpath

我想将矩形的 UIBezierPath 动画化为三角形,而不是为路径上的 View 设置动画,而是为路径本身设置动画,使其从一种形状变为另一种形状。路径被添加到 CALayer

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];    
maskLayer.path = [self getRectPath];

-(CGPathRef)getTrianglePath
{
    UIBezierPath* triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:CGPointZero];
    [triangle addLineToPoint:CGPointMake(width(self.view),0)];
    [triangle addLineToPoint:CGPointMake(0, height(self.view))];
    [triangle closePath];
    return [triangle CGPath];
}

-(CGPathRef)getRectPath
{
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame];
    return [rect CGPath];
}

最佳答案

我不是 CoreAnimation 方面的专家,但您可以按如下方式定义一个 CABasicAnimation

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
morph.duration = 5;
morph.toValue = (id) [self getTrianglePath];
[maskLayer addAnimation:morph forKey:nil];

第一行声明您要定义一个动画来更改图层的 path 属性。第二行说明动画需要五秒钟,第三行给出动画的最终状态。最后,我们将动画添加到图层中。键是动画的唯一标识符,也就是说,如果您添加两个具有相同键的动画,则只考虑最后一个。此键还可用于覆盖所谓的隐式动画。默认情况下,CALayer 有几个属性是动画的。更准确地说,如果您更改这些属性之一,更改将以 0.25 的持续时间进行动画处理。

关于objective-c - 如何为 UIBezierPath 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869509/

相关文章:

ios - 动画边界更改时具有 CALayer 有线效果的自定义 View

core-animation - CALayer显式动画的行为异常

ios - MapView 委托(delegate)问题

ios - 声音播放完毕后,AVAudioPlayer实例会怎样?

objective-c - GLKBaseEffect prepareToDraw GL 错误 : 0x0501

c# - 'method' 没有重载匹配委托(delegate) 'System.eventhandler'

javascript - 将动画添加到滚动时的 css 更改

ios - 如何在 iOS 6.1 中从我们的应用程序打开 native 日历应用程序?

javascript - JS : Event on ScrollDown and ScrollUp

iphone - CALayer::hitTest 横向失败