2013-09-26 29 views
1

我是新開發的iOS。創建可伸縮路徑CGContextPath

我用Core Graphics/UIKit繪製時出現問題。 我想在Window中實現一個像paint形狀的函數。

我使用此源:https://github.com/JagCesar/Simple-Paint-App-iOS,並添加新的功能。

當touchesMoved時,我繪製一個形狀,基於touchesBegan的點和當前的觸摸點。它繪製所有的形狀。

- (void)drawInRectModeAtPoint:(CGPoint)currentPoint 
    { 
    UIGraphicsBeginImageContext(self.imageViewDrawing.frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [self.currentColor setFill]; 

    [self.imageViewDrawing.image drawInRect:CGRectMake(0, 0, self.imageViewDrawing.frame.size.width, self.imageViewDrawing.frame.size.height)]; 

    CGContextMoveToPoint(context, self.beginPoint.x, self.beginPoint.y); 
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); 
    CGContextAddLineToPoint(context, self.beginPoint.x * 2 - currentPoint.x, currentPoint.y); 
    CGContextAddLineToPoint(context, self.beginPoint.x, self.beginPoint.y); 
    CGContextFillPath(context); 

    self.currentImage = UIGraphicsGetImageFromCurrentImageContext(); 
    self.imageViewDrawing.image = self.currentImage; 
    UIGraphicsEndImageContext(); 
    } 

我的意思是,我只希望創建一個形狀,當的touchesBegan,應用程序記錄點,當touchesMoved,形狀由觸摸縮放,當touchesEnd,繪製形狀的ImageContex

希望你能給我一些提示來做到這一點。 謝謝。

+0

因此,你的意思是你想從觸摸開始拖出到結束,每次獲取更新時調整圖像大小,然後將最終圖像保存到上下文中? – Wain

+0

我的意思是,我想創建只有一個形狀, 當touchesBegan,應用程序記錄點, 當touchesMoved,形狀是通過觸摸縮放, ,當touchesEnd,繪製形狀到ImageContext – user2818764

回答

0

您可能希望從上下文中提取此功能。在您使用圖像時,請使用圖像視圖。在觸摸開始時,創建圖像和圖像視圖。將圖像視圖框設置爲大小爲{1,1}的觸摸點。隨着觸摸移動,通過更改frame來移動/縮放圖像視圖。觸摸結束時,使用開始點和結束點將圖像渲染到上下文中(應該與圖像視圖的最終frame相同)。

這樣做意味着您不會向接收到下一次觸摸更新時需要刪除的上下文添加任何內容。上述方法與CALayer相似,而不是圖像視圖。您也可以在視圖上使用transform來查看解決方案。

+0

感謝您的意見,我會嘗試。 – user2818764