2011-06-08 68 views
3

我正在使用核心圖形繪製到iOS屏幕,並且我設法能夠在屏幕上繪製,但是從我跟隨的教程中,繪圖屏幕覆蓋了我的筆尖文件中的所有其他內容。任何人都可以幫助我弄清楚如何將繪圖區域限制在我在筆尖創建的視圖中?我一直堅持這一點。iOS繪製到屏幕

我跟着教程是這樣的: http://www.ifans.com/goto/http://www.touchrepo.com/SampleCode/TEST_DRAW_APP.zip

我的代碼:在我從我的廈門國際銀行在Interface Builder中引用一個子類擴展一個UIView

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    //Set text from previous 
    text.text = [[GameManager sharedManager] inText]; 

    drawImage = [[UIImageView alloc] initWithImage:nil]; 
    drawImage.frame = self.view.frame; 
    [self.view addSubview:drawImage]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     drawImage.image = nil; 
     return; 
    } 

    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 20; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    mouseSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -= 20; 

    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint;  
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) 
    { 
      drawImage.image = nil; 
      return; 
    } 

    if(!mouseSwiped) 
    { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

回答

0

我已經成功。這樣我就可以在界面構建器中看到視圖,移動它並從界面構建器中更改它的屬性。我這樣做,但是你可以按照你喜歡的任何順序做到這一點:

  1. 爲您的視圖創建一個新的UIView類實例,稍後您將在界面構建器中鏈接。
  2. 在您的新視圖類中重寫drawRect方法並在那裏進行繪製。請記住獲取對圖形上下文的引用,以便您可以使用它進行繪製。我看到你經常使用UIGraphicsGetCurrentContext()方法。如果你正在做複雜的事情,最好把它分配給一個變量以避免開銷並提高繪圖性能。
  3. 在界面生成器中選擇要放入視圖的視圖控制器,然後從對象庫中將新視圖拖到視圖控制器上。根據需要調整空白框的大小。
  4. 最後在界面構建器中選擇您的視圖,然後轉到「標識檢查器」選項卡。選擇您之前在Custom Class - > Class下拉菜單下創建的類(如果在這裏找不到它,不用擔心,重新啓動Xcode並重新啓動,有時會發生這種情況)。
  5. 雖然您的視圖在界面構建器中看起來空白,但它應該在您爲視圖設置的框架中運行時繪製。

我看到您在用戶觸摸視圖控制器時正在繪製圖形。這些觸摸事件仍然可以在您的主視圖控制器類中處理。爲了處理這個問題,你可以改變你的子視圖中的一些屬性,並在你的子視圖中調用'setNeedsDisplay'來確保在下一次顯示刷新時調用子視圖的繪製方法。