【问题标题】:How to change color of the brush on button click?如何在单击按钮时更改画笔的颜色?
【发布时间】:2011-11-12 04:05:12
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您不应该尝试在触摸委托中进行绘制。稍后绘制,当操作系统调用你的 drawRect 时。在触摸委托中设置或排队一些状态变量,以了解稍后需要绘制的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-21
        • 2019-05-20
        • 2021-10-18
        • 1970-01-01
        • 2021-07-07
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        相关资源
        最近更新 更多