简体   繁体   English

使用蒙版对UIImageView进行动画处理(使用蒙版进行核心动画处理)

[英]Animating UIImageView with mask (Core Animations with masks)

I have one UIImage in UIImageView (object on image). 我在UIImageView中有一个UIImage(图像上的对象)。 It needs to be animated to the right, but this animation needs to be masked with this circle mask. 它需要在右侧设置动画,但是此动画需要使用此圆形蒙版进行蒙版。 在此处输入图片说明

My current code is: 我当前的代码是:

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];

But this animates my whole UIImageView masked. 但这会使我的整个UIImageView处于动画状态。 I need mask to be static, just object behind to be animated. 我需要蒙版是静态的,只是后面的对象要动画。

What is the best solution to make this? 做到这一点的最佳解决方案是什么?

The mask layer shapeLayer acts as though it is a child of the other layer [loadingShape layer] . 遮罩层shapeLayer好像是另一层[loadingShape layer] When the parent layer moves, the child moves with it. 当父层移动时,子级随之移动。

In addition to your current animation, you will have to animate shapeLayer , in the opposite direction. 除了当前的动画,您还必须在相反的方向上对shapeLayer进行动画shapeLayer

Edit: The animation established by -[UIView beginAnimations:context:] does not automatically apply to a bare CALayer , so set up the animation manually. 编辑: -[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];

Thanks to Kurt, I refined a little bit my code and got what I wanter :) 多亏了Kurt,我对代码做了一些改进,得到了我想要的东西:)

CGMutablePathRef path = CGPathCreateMutable(); 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)]; UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(6.5, 7.5, 44, 44)]; UIImageView *loadingShape = [[UIImageView alloc] initWithFrame:CGRectMake(0, 18, 89, 40)]; [loadingShape setImage:[UIImage imageNamed:@"object_image.png"]]; [[loadingView layer] setMask:shapeLayer]; [loadingView addSubview:loadingShape]; [self addSubview:loadingView]; // Set the mask for the image view's layer CABasicAnimation* anim = [[CABasicAnimation alloc] init]; anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 18)]; anim.toValue = [NSValue valueWithCGPoint:CGPointMake(15, 18)]; anim.keyPath = @"position"; anim.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"]; anim.duration = 0.3; anim.repeatCount = 100000; [[loadingShape layer] addAnimation:anim forKey:nil]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM