objective-c - Objective-C:触摸绘图不透明度

标签 objective-c ios core-graphics

我正在尝试制作一个可以控制画笔不透明度的绘图应用程序,但是当我尝试降低不透明度时,结果是这样的。我使用了核心图形。 (检查图像)。

我将如何解决此问题?

这是我的一些代码。

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

    previousPoint1 = [touch locationInView:self.view];
    previousPoint2 = [touch locationInView:self.view];
    currentTouch = [touch locationInView:self.view];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//upon moving
{
    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = currentTouch;
    currentTouch = [touch locationInView:self.view];

    CGPoint mid1 = midPoint(previousPoint2, previousPoint1); 
    CGPoint mid2 = midPoint(currentTouch, previousPoint1);

    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    [imgDraw.image drawInRect:CGRectMake(0, 0, 1024, 768)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context,kCGLineCapRound);
    CGContextSetLineWidth(context, slider.value);
    CGContextSetBlendMode(context, blendMode);
    CGContextSetRGBStrokeColor(context,red, green, blue, 0.5);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGContextStrokePath(context);

    imgDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsGetCurrentContext();
    endingPoint=currentTouch;
}

最佳答案

不要用-touchesMoved:withEvent:绘制图形。在任何事件方法中,您都应该更新需要绘制的内容,然后发送-setNeedsDisplay.
在上面编写代码时,您正在从事件消息获得的每对位置之间创建一条路径。

您应该在-touchesBegan:withEvent:中创建路径,并在-touchesMoved:withEvent:中添加路径。

关于objective-c - Objective-C:触摸绘图不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499652/

相关文章:

objective-c - objective-c 通过电话号码在地址簿中查找联系人

ios - 标签未出现在 Swift 表中

iphone - 如何创建径向 UIView?

cocoa-touch - 为什么我的 CGGradient 不能使用预设的 UIColor?

ios - 在 iOS 中下载文件的内存管理

objective-c - 如何在 Xcode 的 "Cocoa Application"模板上编辑关于页面

iphone - 如何将 iPhone 摇动手势检测限制在特定情况下?

ios - 如何停止在 Xcode 中播放不同类的背景音乐?

ios - 删除 PieChartDataEntry 标签的旋转

ios - 将 CIImage 转换为 CGImage 太慢