【问题标题】:Draw line opacity in Xcode for Titanium在 Xcode for Titanium 中绘制线条不透明度
【发布时间】:2011-08-08 04:07:37
【问题描述】:

我是 xCode 的新手,但遇到了问题。我正在为我的 Ipad 申请。我想突出显示文本,但无法设置线条的不透明度。我搜索了互联网,但我没有找到任何信息。现在我使用这个 drawImage.alpha = lineOpacity;但这并不好,我之前绘制的所有内容都会变得不透明。我只想在新绘制时完成 alpha。我现在正在使用此代码:

   //here I init the opacity
     -(id)init
    {
        if (self = [super init])
        {
            strokeWidth = 5;
            strokeColor = CGColorRetain([[TiUtils colorValue:@"#000"] _color].CGColor);
            lineOpacity = 1;
        }
        return self;
    }
    - (void)dealloc
{
    RELEASE_TO_NIL(drawImage);
    CGColorRelease(strokeColor);
    [super dealloc];
}

- (void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
    [super frameSizeChanged:frame bounds:bounds];
    if (drawImage!=nil)
    {
        [drawImage setFrame:bounds];
    }
}

    //here I add a call
    - (void)setLineOpacity_:(id)alpha
    {
        lineOpacity = [TiUtils floatValue:alpha];
    }

    //here I want to set the opacity
    - (void)drawAt:(CGPoint)currentPoint
    {
        UIView *view = [self imageView];
        UIGraphicsBeginImageContext(view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), strokeWidth);
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), strokeColor);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        //here i want to set the alpha, but everything I draw before get also the new alpha.
        //I just want the alpha to be done on the new draw
        drawImage.alpha = lineOpacity;
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        lastPoint = currentPoint;
    }

我希望有人可以帮助我。提前致谢。

编辑:我用这个来画画:

- (UIImageView*)imageView
{
    if (drawImage==nil)
    {   
        drawImage = [[UIImageView alloc] initWithImage:nil];
        drawImage.frame = [self bounds];
        [self addSubview:drawImage];
    }
    return drawImage;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:[self imageView]];
    lastPoint.y -= 20;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:[self imageView]];
    currentPoint.y -= 20;
    [self drawAt:currentPoint];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesEnded:touches withEvent:event];
    [self drawAt:lastPoint];
}

#pragma mark Public APIs

- (void)setStrokeWidth_:(id)width
{
    strokeWidth = [TiUtils floatValue:width];
}

- (void)setStrokeColor_:(id)value
{
    CGColorRelease(strokeColor);
    TiColor *color = [TiUtils colorValue:value];
    strokeColor = [color _color].CGColor;
    CGColorRetain(strokeColor);
}

- (void)setLineOpacity_:(id)alpha
{
    lineOpacity = [TiUtils floatValue:alpha];
}

我真的很想找出它不起作用的方法,搜索了很长时间。

【问题讨论】:

    标签: xcode ipad line alpha


    【解决方案1】:

    您可以像这样制作 UIColor 并从中获取 CGColorRef:

    strokeColor = CGColorRetain([UIColor colorWithRed:1.0f green:1.0f blue:0.0f alpha:0.5f].CGColor);
    

    【讨论】:

    • 当我包含您的代码时,我的应用程序将退出。你不知道别的方法吗?就像我在上面发布的那样,我可以使用 strokeColor.setOpacity(0.2))。但是当我以这种方式使用它时,我得到的结果是:i55.tinypic.com/zufbbq.png
    • 对不起,我没有仔细看你的代码。显然,使用我的方法,您仍然需要保留 CGColor 对象。
    • 你知道我该如何解决这个问题吗,i55.tinypic.com/zufbbq.png。我尝试了一切,但我无法修复它(排队的球)。非常感谢
    • 我想你每次调用drawAt: 时都在画一个“球”,它们相互堆叠。更好的方法是继承 UIView 并在其drawRect: 方法中绘制突出显示路径。更新各种touches* 方法中的点列表,然后使用该列表绘制路径。
    • 我明白你的意思,但我不知道如何解决这个问题。
    【解决方案2】:

    不透明度设置为描边颜色的 alpha 值(本例中为 0.5):

    strokeColor = CGColorCreateGenericRGB(1.0f, 1.0f, 0.0f, 0.5f);
    

    【讨论】:

    • 我试过了,但它说我不能使用它。错误:“CGColorCreateGenericRGB”不可用(在 /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGColor.h:29 声明)。我之前也尝试过设置描边不透明度(不是你的方式,只是 strokColor.setOpacity(0.2)),结果是:i55.tinypic.com/zufbbq.png
    • 您的结果显示不透明度正在起作用。你可能不喜欢的是不完整的结果。这很可能是由绘制多个单独的线段而不是单个连续路径引起的。对于单独的段,重叠段的颜色叠加并导致暗斑。为避免这种情况,请将所有点添加到路径并绘制路径。
    • 这对我有用:CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, 1.0f, 0.0f, 0.2f);但是我遇到了与上图中相同的问题。我添加了用于在视图上绘制的代码。你能解释一下我该如何解决这个问题吗?提前致谢!
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多