Ios 设置带遮罩的UIImageView动画(带遮罩的核心动画)

Ios 设置带遮罩的UIImageView动画(带遮罩的核心动画),ios,core-animation,core-graphics,Ios,Core Animation,Core Graphics,我在UIImageView中有一个UIImage(图像上的对象)。它需要设置右侧动画,但此动画需要使用此圆形遮罩进行遮罩。 我目前的代码是: CGMutablePathRef path = CGPathCreateMutable(); CGPathAddArc(path, NULL, 44, 44, 21, 0, 2 * M_PI, NO); CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; [shapeLayer setPat

我在UIImageView中有一个UIImage(图像上的对象)。它需要设置右侧动画,但此动画需要使用此圆形遮罩进行遮罩。

我目前的代码是:

CGMutablePathRef path = CGPathCreateMutable();

CGPathAddArc(path, NULL, 44, 44, 21, 0, 2 * M_PI, NO);

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:CGRectMake(0, 0, 44, 44)];
[shapeLayer setPosition:CGPointMake(0, 0)];

UIImageView *loadingShape = [[UIImageView alloc] initWithFrame:CGRectMake(6.5, 7.5, 89, 40)];
[loadingShape setImage:[UIImage imageNamed:@"object_image.png"]];
[self addSubview:loadingShape];
// Set the mask for the image view's layer
[[loadingShape layer] setMask:shapeLayer];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3];
[UIView setAnimationRepeatCount:10];
[loadingShape setFrame:CGRectMake(0, 7.5, 89, 40)];
[UIView commitAnimations];
但这会让我的整个UIImageView变得生动起来。我需要面具是静态的,只是物体后面的动画


最好的解决方案是什么?

遮罩层
shapeLayer
的作用就像它是另一层的子层
[loadingShape layer]
。父层移动时,子层也随之移动

除了当前动画外,还必须以相反方向设置
shapeLayer
的动画

编辑:
-[UIView beginAnimations:context:
创建的动画不会自动应用于裸
CALayer
,因此手动设置动画

CGPoint p = /* you decide the new position of the shapeLayer */;
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
anim.toValue = [NSValue valueWithCGPoint:p];
anim.keyPath = @"position";
anim.timingFunction = [CAMediaTimingFunction functionWithName:@"easeOut"];
anim.duration = 0.3;
anim.repeatCount = 10;
[shapeLayer.mask addAnimation:anim forKey:nil];
[anim release];

遮罩层
shapeLayer
的作用就像它是另一层
[加载形状层]
的子层一样。父层移动时,子层也随之移动

除了当前动画外,还必须以相反方向设置
shapeLayer
的动画

编辑:
-[UIView beginAnimations:context:
创建的动画不会自动应用于裸
CALayer
,因此手动设置动画

CGPoint p = /* you decide the new position of the shapeLayer */;
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
anim.toValue = [NSValue valueWithCGPoint:p];
anim.keyPath = @"position";
anim.timingFunction = [CAMediaTimingFunction functionWithName:@"easeOut"];
anim.duration = 0.3;
anim.repeatCount = 10;
[shapeLayer.mask addAnimation:anim forKey:nil];
[anim release];

多亏了库尔特,我对代码做了一点改进,得到了我想要的:)

CGMutablePathRef path=CGPathCreateMutable()


多亏了库尔特,我对代码做了一点改进,得到了我想要的:)

CGMutablePathRef path=CGPathCreateMutable()


嗯,试图通过设置层和边界的位置动画来实现这一点,但没有成功:|你能给我看一些片段吗?你应该能够通过设置
shapeLayer
的位置动画来实现这一点。我猜您希望
UIView
动画参数自动应用于CALayer。他们没有。我在答案中添加了一些代码。嗯,试图通过设置层和边界的位置动画来实现这一点,但没有成功:|你能给我看一些片段吗?你应该能够通过设置
shapeLayer
的位置动画来实现这一点。我猜您希望
UIView
动画参数自动应用于CALayer。他们没有。我在答案中添加了一些代码。您正在设置
位置的动画,但将
从value
到value
设置为
CGRect
s。它们不应该是
CGPoints
?你是对的,我更改了代码。但这两种解决方案都有效:)您正在设置
位置的动画,但将
从value
到value
赋予它,它们是
CGRect
s。它们不应该是
CGPoints
?你是对的,我更改了代码。但这两种解决方案都有效:)