2012-09-10 234 views
8

使用Objective-C中的Cocoa Touch,我正在尋找繪製目標圖像的最佳方式(即圓圈內的圓圈,足夠簡單),然後將用戶的觸摸記錄在圖像(可能爲x或+標記),更重要的是在內存中用於以後重新繪製。使用Cocoa Touch繪製目標圖像

當用戶長時間保持手指放下時,我將使用放大鏡,以便實現更精確的定位,這是通過閱讀並試驗CoffeeShopped文章和來源得出的。

回答

1

創建的UIView的子類,並實現drawRect中

- (void)drawRect:(CGRect)rect { 
    CGRect circleRect = self.bounds; 

    CGFloat circleWidth = circleRect.size.width/5.0; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); 
    CGContextFillEllipseInRect(context, circleRect); 

    circleRect = CGRectInset(circleRect, circleWidth, circleWidth); 
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); 
    CGContextFillEllipseInRect(context, circleRect);   

    circleRect = CGRectInset(circleRect, circleWidth, circleWidth); 
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); 
    CGContextFillEllipseInRect(context, circleRect); 
} 

將借鑑

enter image description here

你應該設置你的觀點backgoundColor到[UIColor clearColor]使周圍的邊緣它不是黑色。

你也可以將其調整爲一個循環,但這是我能夠顯示的最簡單的示例代碼。

注:我不重用的顏色,圓弧/ nonarc簡化代碼

-1

如果你想重新使用圖像和重繪(例如 - 當用戶觸摸它),你應緩存圖紙作爲圖片。

1)畫出目標(如下面所述)
2)從當前上下文

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

3創建圖像)保存該圖像和下一次重複使用它

- (void)drawRect:(CGRect)rect { 
    if(image == nil) { 
     image = [self drawTargetImage]; //use code that mentioned below to create image 
    } 
    [image drawAtPoint:CGPointMake(10, 10)]; 
}