gpt4 book ai didi

objective-c - CABasicAnimation 不起作用

转载 作者:行者123 更新时间:2023-12-03 16:54:39 26 4
gpt4 key购买 nike

我正在尝试为圆形笔画设置动画(来自另一个 SO answer )。我正在尝试在基于 Cocoa 的应用程序中执行此操作。

但是它不起作用,并且立即调用 animationDidStop:finished: ,并将 finished 标志设置为 NO。为什么会发生这种情况?有关如何获取一些信息的任何指示,为什么 finished 标志是 NO

这是我使用的代码:

注意:quartzPathNSColorToCGColor 来自 NSColorNSBezierPath 上的类别。

- (IBAction)animateCircle:(id)sender {

int radius = 10;
circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(10, 10, 2.0*radius, 2.0*radius)] quartzPath] ;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.vcForCellView.view.frame)-radius,
CGRectGetMidY(self.vcForCellView.view.frame)-radius);

// Configure the apperence of the circle
circle.fillColor = [NSColor NSColorToCGColor:[NSColor clearColor]];
circle.strokeColor = [NSColor NSColorToCGColor:[NSColor blackColor]];
circle.lineWidth = 5;

// Add to parent layer
[self.vcForCellView.view.layer addSublayer:circle];

// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 2.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation..

// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];

// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

drawAnimation.delegate = self;

// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];

}

还有以下代码:

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
CALayer *layer = [anim valueForKey:@"parentLayer"];
NSLog(@"%@",layer);
NSLog(@"%@",flag?@"YES":@"NO");
}

给出以下输出:

2013-07-02 19:28:14.760 TicTacToe[16338:707](空)
2013-07-02 19:28:14.761 TicTacToe[16338:707] 否

更新:

在我用图层支持 View 后,animationDidStop:... 方法被调用,finishedYES ([查看 setWantsLayer:YES])。但屏幕上仍然没有显示任何内容。

以下是从 NSBezierPath 获取 CGPathRef 的代码:

- (CGPathRef)quartzPath
{
int i;
NSInteger numElements;

// Need to begin a path here.
CGPathRef immutablePath = NULL;

// Then draw the path elements.
numElements = [self elementCount];
if (numElements > 0)
{
CGMutablePathRef path = CGPathCreateMutable();
NSPoint points[3];
BOOL didClosePath = YES;

for (i = 0; i < numElements; i++)
{
switch ([self elementAtIndex:i associatedPoints:points])
{
case NSMoveToBezierPathElement:
CGPathMoveToPoint(path, NULL, points[0].x, points[0].y);
break;

case NSLineToBezierPathElement:
CGPathAddLineToPoint(path, NULL, points[0].x, points[0].y);
didClosePath = NO;
break;

case NSCurveToBezierPathElement:
CGPathAddCurveToPoint(path, NULL, points[0].x, points[0].y,
points[1].x, points[1].y,
points[2].x, points[2].y);
didClosePath = NO;
break;

case NSClosePathBezierPathElement:
CGPathCloseSubpath(path);
didClosePath = YES;
break;
}
}

// Be sure the path is closed or Quartz may not do valid hit detection.
if (!didClosePath)
CGPathCloseSubpath(path);

immutablePath = CGPathCreateCopy(path);
CGPathRelease(path);
}

return immutablePath;
}

而且我使用的是 10.7 SDK,无法使用通过 NSBezierPath 的新 SDK 添加的内置 CGPath 方法。

最佳答案

默认情况下,OS X 上的 View 没有附加层,因此可能的问题是 vcForCellView.view.layernil。这意味着形状图层永远不会添加到图层层次结构中,因此当动画添加到形状图层时,它会立即取消(如 finishedNO 所示)。

您可以使用以下命令告诉您的 View 它应该由图层支持:[myView setWantsLayer:YES];

关于objective-c - CABasicAnimation 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17427759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com