我有一个 CALayer 实现drawInContext: 并绘制一个简单的圆圈,如下所示:
- (void)drawInContext:(CGContextRef)ctx
{
CGContextScaleCTM(ctx, DrawingScale, DrawingScale);
CGContextSetRGBFillColor (ctx, 1, 0, 0, 1);
CGContextFillEllipseInRect (ctx, LocationRectangle);
}
我一直在尝试根据触摸事件增加圆圈大小的不同方法。我尝试过使用 UIView beginAnimation 系统和 CABasicAnimation 系统,但它们都会使图像模糊。在我的阅读中,我发现这是因为 CALayer 似乎被视为这些选项的位图。
很公平......但我想缩放我的图形而不模糊,例如使用矢量缩放。
因此,在我的代码中我有一个“DrawingScale”属性。
有没有一种巧妙的方法来使用图层动画缩放此属性?或者这是人们用 NSTimers 做的事情(yuk)?
在这种情况下,我可以使用 Core Animation 的功能来为自定义属性设置动画吗? 如果是这样,我很想知道人们发现哪个示例是最好的,或者 Apple 文档的哪一部分可能是开始讨论此主题的好地方。
似乎在 Mac/iOS 上的图形方面,有很多方法可以给猫蒙皮/画猫......?
最佳答案
花了几天时间研究这个问题后,我的一个解决方案是使用一个名为“DrawingScale”的变量作为成员变量(它们在 Obj-C 中被称为),并使用 Core Animation 来为该属性设置动画。
- (void)drawInContext:(CGContextRef)ctx
{
CGFloat size = (CGFloat)(DrawingScale*DEFAULT_SIZE);
CGRect elipseRect = CGRectMake(xPos, yPos, size, size);
CGContextStrokeEllipseInRect(ctx, elipseRect);
CGContextFillEllipseInRect (ctx, elipseRect);
}
然后在一个事件中,我设置了一个动画:
-(void) : (CGPoint) location
{
/* Set up an explicit animation to scale the players size up */
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"DrawingScale"];
anim.fromValue = [NSNumber numberWithFloat: 1];
anim.toValue = [NSNumber numberWithFloat: 5];
anim.removedOnCompletion = YES;
anim.duration = 0.1;
anim.delegate = self;
anim.fillMode = kCAFillModeForwards;
anim.autoreverses = NO;
[self addAnimation: anim forKey:@"ElipseGettingBigger"];
}
这会设置动画以将变量 DrawingScale 设置为 1 到 5 的范围,并在 0.1 秒内完成此操作。效果非常好。
我确实需要小心并确保我也先相应地调整了图层边界!
关于iphone - 不模糊地制作 CALayer 比例动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3078462/