원형 스트로크를 애니메이션하려고합니다 (다른 SO answer). 나는 코코아 기반 응용 프로그램에서 그것을하려고 노력하고 있습니다.CABasicAnimation이 작동하지 않습니다.
그러나 작동하지 않습니다. 은 즉시 finished
플래그로 NO
으로 호출됩니다. 왜 이런 일이 일어나는 걸까요? finished
플래그가 NO
인 이유에 대한 정보는 얻을 수 있습니까?
주 : quartzPath
및 NSColorToCGColor
이 NSColor
및 NSBezierPath
의 범주에서 여기에
- (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"];
}
다음 코드
2013-07-02 19:28:14.760 TicTacToe[16338:707] (null)
2013-07-02 19:28:14.761 TicTacToe[16338:707] NO
UPDATE :
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
CALayer *layer = [anim valueForKey:@"parentLayer"];
NSLog(@"%@",layer);
NSLog(@"%@",[email protected]"YES":@"NO");
}
다음과 같은 출력을 제공한다 : 나는 레이어 ([view setWantsLayer:YES]
)와보기를 백업 한 후
animationDidStop:...
방법은 YES
로 finished
로 호출됩니다. 그러나 여전히 화면에는 아무 것도 보이지 않습니다. 여기
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
방법을 사용할 수 없습니다.
가 제대로 계층 백업/레이어 호스팅하도록 구성보기가 : 그것이 사용 층에 의해 뒷받침되어야한다
당신은 당신의보기를 알 수 있습니까? '[myView setWantsLayer : YES];'그렇지 않으면 뷰에 레이어가 없으므로 셰이프 레이어가 레이어 계층에 추가되지 않을 수 있다고 생각합니다. –
@ DavidRönnqvist : Aargh .. 나는 그것을 놓쳐서는 안됩니다. 고마워. :) 대답으로 게시하면 신용을 줄 수 있습니다. animationDidStop이 예상대로 작동하지 않습니다. 단 한가지는 여전히 드로잉 애니메이션을 볼 수 없다는 것입니다. : | – Rakesh
그리기 애니메이션의 문제점은 무엇입니까? –