CALayer의 frame 속성은 위치, anchorPoint, bounds 및 transform에 따라 파생 속성입니다. 프레임에 애니메이션을 적용하는 대신 달성하려는 효과에 따라 위치 또는 경계를 애니메이션으로 처리해야합니다.
-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
// Prepare the animation from the old size to the new size
CGRect oldBounds = layer.bounds;
CGRect newBounds = oldBounds;
newBounds.size = size;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
// NSValue/+valueWithRect:(NSRect)rect is available on Mac OS X
// NSValue/+valueWithCGRect:(CGRect)rect is available on iOS
// comment/uncomment the corresponding lines depending on which platform you're targeting
// Mac OS X
animation.fromValue = [NSValue valueWithRect:NSRectFromCGRect(oldBounds)];
animation.toValue = [NSValue valueWithRect:NSRectFromCGRect(newBounds)];
// iOS
//animation.fromValue = [NSValue valueWithCGRect:oldBounds];
//animation.toValue = [NSValue valueWithCGRect:newBounds];
// Update the layer's bounds so the layer doesn't snap back when the animation completes.
layer.bounds = newBounds;
// Add the animation, overriding the implicit animation.
[layer addAnimation:animation forKey:@"bounds"];
}
당신은 CAAnimationGroup 경우를 사용하여 이러한 애니메이션을 결합 할 수 있습니다 :
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
// Prepare the animation from the current position to the new position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [layer valueForKey:@"position"];
// NSValue/+valueWithPoint:(NSPoint)point is available on Mac OS X
// NSValue/+valueWithCGPoint:(CGPoint)point is available on iOS
// comment/uncomment the corresponding lines depending on which platform you're targeting
// Mac OS X
animation.toValue = [NSValue valueWithPoint:NSPointFromCGPoint(point)];
// iOS
//animation.toValue = [NSValue valueWithCGPoint:point];
// Update the layer's position so that the layer doesn't snap back when the animation completes.
layer.position = point;
// Add the animation, overriding the implicit animation.
[layer addAnimation:animation forKey:@"position"];
}
레이어의 크기를 조정하려면, 당신은 bounds
매개 변수를 애니메이션 것 :
레이어를 이동하려면 position
(가) 애니메이션을 적용 할 수 있습니다 레이어를 동시에 이동하고 크기를 조정해야합니다.
감사합니다. 블라디미르. CALayer가 프레임 속성을 직접 애니메이션으로 만들 수 없다는 것을 발견했습니다. 문서에 회색 상자가 있습니다. 어떤 이유로 그것을 보지 못했습니다. – dontWatchMyProfile
마지막 줄은 그대로입니다. addAnimation에서 지정한 키는 임의의 NSString이 될 수 있습니다. 실제 답변은 베릴륨이 – Mattia
이하라고 생각합니다 [myLayer addAnimation : frameAnimation forKey : @ "frame"]; "프레임"은 애니메이션의 이름 일 뿐이며 실제 속성과 아무런 관련이 없습니다. –