2012-03-23 19 views
1

私はUIImageView(オブジェクト上の画像)にUIImageを1つ持っています。右にアニメートする必要がありますが、このアニメーションはこのサークルマスクでマスクする必要があります。マスク付きアニメーション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をマスクアニメーション化: enter image description here

私の現在のコードがあります。私は静的であるためにマスクが必要です。

これを行うには最適なソリューションは何ですか?

答えて

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'をアニメーション化していますが、' CGRect'である 'fromValue'と' toValue'を与えています。彼らは 'CGPoints 'ではないでしょうか? –

+0

あなたは正しいです、私はコードを変更しました。しかし、両方のソリューションが動作します:) – dormitkon

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

+0

'shapeLayer'の位置をアニメーション化することでこれを行うことができます。それは本当にそこにあるすべてです。 –

+0

私はちょうど試しましたが、私は同じ効果を得ています: – dormitkon

関連する問題