CALayer动画从一点移动到另一点
CALayer animation to move from one point to another
我正在尝试为附加到 objective C ios 中的 UIView 的根层上的子层设置动画。我在这里尝试了很多可用的答案,但子层是固定的,根本没有改变它的位置。
我尝试了以下方法来制作动画:
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point with:(CGRect)bounds
{
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,74.0,74.0);
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path=thePath;
theAnimation.duration=5.0;
// Add the animation to the layer.
[layer addAnimation:theAnimation forKey:@"position"];
}
并且还在使用
`
[featureLayer setFrame:oldRect];
CGRect oldBounds = oldRect;
CGRect newBounds = faceRect;
CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
revealAnimation.toValue = [NSValue valueWithCGRect:newBounds];
revealAnimation.duration = 3.0;
// Update the bounds so the layer doesn't snap back when the animation completes.
featureLayer.bounds = newBounds;
[featureLayer addAnimation:revealAnimation forKey:@"revealAnimation"];
[self.previewLayer setMask:featureLayer];
`
如果有人能帮助我,那就太好了。提前致谢。
编辑 1
为了确保这是根层上的蒙版图像,我想设置蒙版动画以从一个位置移动到另一个位置,但它保持静止。
您可以试试下面的代码。
- (void)viewDidLoad {
[super viewDidLoad];
CAShapeLayer *shape = [CAShapeLayer new];
shape.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)].CGPath;
shape.fillColor = [UIColor redColor].CGColor;
shape.position = CGPointMake(self.view.bounds.size.width / 2 - 25, 100);
[self.view.layer addSublayer:shape];
[self performSelector:@selector(doAnimation:) withObject:shape afterDelay:1];
//[self performSelector:@selector(doKeyFrameAnimation:) withObject:shape afterDelay:1];
}
- (void)doAnimation:(CALayer*)layer {
NSLog(@"%@",layer);
CABasicAnimation *anim = [CABasicAnimation new];
anim.duration = 2;
anim.keyPath = @"position.y";
anim.fromValue = @(layer.position.y);
anim.toValue = @300;
[layer addAnimation:anim forKey:@"move_down"];
}
// the KeyFrameAnimation way
- (void)doKeyFrameAnimation:(CALayer*)layer {
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:layer.position];
[path addLineToPoint:CGPointMake(layer.position.x, 300)];
CAKeyframeAnimation *anim = [CAKeyframeAnimation new];
anim.duration = 2;
anim.keyPath = @"position";
anim.path = path.CGPath;
[layer addAnimation:anim forKey:@"move_down_by_keyframe"];
}
我正在尝试为附加到 objective C ios 中的 UIView 的根层上的子层设置动画。我在这里尝试了很多可用的答案,但子层是固定的,根本没有改变它的位置。
我尝试了以下方法来制作动画:
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point with:(CGRect)bounds
{
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,74.0,74.0);
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path=thePath;
theAnimation.duration=5.0;
// Add the animation to the layer.
[layer addAnimation:theAnimation forKey:@"position"];
}
并且还在使用
`
[featureLayer setFrame:oldRect];
CGRect oldBounds = oldRect;
CGRect newBounds = faceRect;
CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
revealAnimation.toValue = [NSValue valueWithCGRect:newBounds];
revealAnimation.duration = 3.0;
// Update the bounds so the layer doesn't snap back when the animation completes.
featureLayer.bounds = newBounds;
[featureLayer addAnimation:revealAnimation forKey:@"revealAnimation"];
[self.previewLayer setMask:featureLayer];
`
如果有人能帮助我,那就太好了。提前致谢。
编辑 1
为了确保这是根层上的蒙版图像,我想设置蒙版动画以从一个位置移动到另一个位置,但它保持静止。
您可以试试下面的代码。
- (void)viewDidLoad {
[super viewDidLoad];
CAShapeLayer *shape = [CAShapeLayer new];
shape.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)].CGPath;
shape.fillColor = [UIColor redColor].CGColor;
shape.position = CGPointMake(self.view.bounds.size.width / 2 - 25, 100);
[self.view.layer addSublayer:shape];
[self performSelector:@selector(doAnimation:) withObject:shape afterDelay:1];
//[self performSelector:@selector(doKeyFrameAnimation:) withObject:shape afterDelay:1];
}
- (void)doAnimation:(CALayer*)layer {
NSLog(@"%@",layer);
CABasicAnimation *anim = [CABasicAnimation new];
anim.duration = 2;
anim.keyPath = @"position.y";
anim.fromValue = @(layer.position.y);
anim.toValue = @300;
[layer addAnimation:anim forKey:@"move_down"];
}
// the KeyFrameAnimation way
- (void)doKeyFrameAnimation:(CALayer*)layer {
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:layer.position];
[path addLineToPoint:CGPointMake(layer.position.x, 300)];
CAKeyframeAnimation *anim = [CAKeyframeAnimation new];
anim.duration = 2;
anim.keyPath = @"position";
anim.path = path.CGPath;
[layer addAnimation:anim forKey:@"move_down_by_keyframe"];
}