2011-11-12 150 views
1

我正在爲iOS開發中的第一次嘗試開發繪畫應用程序。如何改變按鈕點擊畫筆的顏色?

我想做一個調色板,將改變我按鈕點擊刷的顏色,但我不知道如何。我已經有UIButtons連接到它的參考插座併發送了事件,但是我的代碼不工作。

這裏:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    touchSwiped = YES; 
    redAmt = 0; 
    blueAmt = 0; 
    greenAmt = 0; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouch = [touch locationInView:self.view]; 
    currentTouch.y -= 20; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 35.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redAmt, blueAmt, greenAmt, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), endingPoint.x, endingPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentTouch.x, currentTouch.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    endingPoint = currentTouch; 

    touchMoved++; 

    if (touchMoved == 10) { 
     touchMoved = 0; 
    } 
} 

    -(IBAction)blackInk:(id)sender 
{ 
    redAmt = 0; 
    blueAmt = 0; 
    greenAmt = 0; 
} 

-(IBAction)blueInk:(id)sender 
{ 
    redAmt = 0; 
    blueAmt = 0; 
    greenAmt = 1; 
} 

-(IBAction)redInk:(id)sender 
{ 
    redAmt = 1; 
    blueAmt = 1; 
    greenAmt = 0; 
} 

-(IBAction)greenInk:(id)sender 
{ 
    redAmt = 0; 
    blueAmt = 1; 
    greenAmt = 0; 
} 

回答

0

停留在方法的頂部重置顏色爲黑色。刪除這些行:

redAmt = 0; 
blueAmt = 0; 
greenAmt = 0; 
+0

謝謝!有效!! – SeongHo

0

您不應該嘗試在觸摸委託中繪製。稍後繪製,當OS調用您的drawRect時。在觸摸委託中設置或排隊稍後需要繪製的內容。