2013-02-26 5 views
6

私はUIImageViewの中にいくつかの円を特定の画像で描画しようとしています。これは私がやろうとしたものである。UIImageViewでシェイプを描くIOS

UIGraphicsBeginImageContext(self.view.bounds.size); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(contextRef, 2.0); 
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]); 
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0)); 

CGContextStrokeEllipseInRect(contextRef, circlePoint); 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[photoView addSubview:image]; 

円が細かい描かれているが、私はそれにマスクとして機能するようにてPhotoViewをしたいと思います。たとえば、UIImageViewをアニメーションを使用してUIViewから移動する場合、その円も一緒に移動したいと思います。重要なことは、座標がスクリーン全体に関連しているということです。

+0

カスタムUIView、または派生したカスタムUIImageViewのようなサウンドです。 – trumpetlicks

答えて

10

コアアニメーションのシェイプレイヤーを代わりに使用します。今

CAShapeLayer *circleLayer = [CAShapeLayer layer]; 
// Give the layer the same bounds as your image view 
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [photoView bounds].size.width, 
               [photoView bounds].size.height)]; 
// Position the circle anywhere you like, but this will center it 
// In the parent layer, which will be your image view's root layer 
[circleLayer setPosition:CGPointMake([photoView bounds].size.width/2.0f, 
            [photoView bounds].size.height/2.0f)]; 
// Create a circle path. 
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect: 
            CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)]; 
// Set the path on the layer 
[circleLayer setPath:[path CGPath]]; 
// Set the stroke color 
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]]; 
// Set the stroke line width 
[circleLayer setLineWidth:2.0f]; 

// Add the sublayer to the image view's layer tree 
[[photoView layer] addSublayer:circleLayer]; 

あなたは、この層が含まれているUIImageViewのアニメーション場合、それは子層であることから、層が一緒に移動します。今やdrawRect:を無効にする必要はありません。

+0

は素晴らしいようですが、サークルの位置を決めたCoordsFinalはどうですか? – user2014474

+0

どうやって計算していますか?同じテクニックを使用してください。それは同じでなければなりません。作成するサークル/シェイプレイヤーごとに 'position'プロパティを設定するだけです。 –

+0

CoordsFinalはこのメソッドから独立していますCGPoint – user2014474

関連する問題