如何在按钮点击时更改画笔的颜色?

时间:2011-11-12 04:05:12

标签: iphone objective-c ios ipad core-graphics

我正在为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;
}

2 个答案:

答案 0 :(得分:0)

停止在方法顶部将颜色重置为黑色。删除这些行:

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

答案 1 :(得分:0)

您不应该尝试在触摸代表内部绘制。稍后绘制,当OS调用drawRect时。在触摸代理中设置或排队一些状态变量,以了解稍后需要绘制的内容。