2012-03-23 49 views
1

我在UIImageView(图像上的对象)有一个UIImage。它需要动画到右侧,但是这个动画需要用这个圆圈蒙版来掩盖。 enter image description here动画UIImageView与面具(核心动画与面具)

我当前的代码是:

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掩盖。我需要面具是静态的,只是在物体背后被动画。

什么是最好的解决方案呢?

回答

2

感谢库尔特,我精一点我的代码,并得到了我wanter :)

CGMutablePathRef路径= 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]; 
+0

你正在动画'position',但是给它'fromValue'和'toValue'这是'CGRect's。他们不应该成为'CGPoints'吗? – 2012-03-23 18:25:16

+0

你是对的,我改变了代码。但这两种解决方案的工作 – dormitkon 2012-03-23 18:49:45

4

掩模层shapeLayer的行为好像是其他层的子节点[loadingShape layer]。当父层移动时,孩子随之移动。

除了您当前的动画,您还需要在相反的方向上动画shapeLayer

编辑:通过​​建立的动画并不自动适用于裸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]; 
+0

嗯,试图通过动画层和边界的位置,要做到这一点,但没有成功:| 你能给我看一些片段吗? – dormitkon 2012-03-23 04:44:22

+0

你应该可以通过动画'shapeLayer'的位置来做到这一点。这就是它的全部。 – 2012-03-23 04:47:02

+0

我只是尝试过,但我得到了同样的效果:| – dormitkon 2012-03-23 04:54:20