2013-02-26 28 views
6

Belirli bir resimle bir UIImageView içinde bazı daireler çizmeye çalışıyorum.Bir UIImageView IOS biçiminde çizim Çizim

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

daire ince çizilir ama PhotoView'un buna bir maske olarak hareket etmek istiyorum: Bu benim yapmaya çalıştığım budur. Öyleyse UIImageView'i UIView'den bir animasyon kullanarak hareket ettirirseniz, dairenin onunla birlikte hareket etmesini isterim. Önemli olan koordinatların tüm ekrana göreli olmasıdır.

+0

Özel UIView veya türetilmiş özel bir UIImageView bana benziyor. – trumpetlicks

cevap

10

Çekirdek Animasyonun bunun yerine şekil katmanını kullanın. Eğer bu katmanı içeren UIImageView animasyon eğer bir çocuk tabakasıdır beri

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

Şimdi, tabaka onunla hareket edecektir. Ve şimdi drawRect:'u geçersiz kılmaya gerek yok.

+0

harika görünüyor, ama çemberin konumunu belirleyen CoordsFinal ne olacak? – user2014474

+0

Şimdi nasıl hesaplıyorsunuz? Aynı tekniği kullan. Aynı olmalı. Oluşturduğunuz her bir daire/şekil katmanı için 'position' özelliğini ayarlamanız yeterlidir. –

+0

CoordsFinal, bu yöntemden bağımsız bir durumdur CGPoint – user2014474