在 Xcode 中为 Titanium 绘制线条不透明度

发布于 2024-11-02 07:00:16 字数 3467 浏览 0 评论 0原文

我是 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];
}

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

I'm new with xCode and I have a problem with it. I'm making an application for my Ipad. I want to highlight text, but I can't set the opacity of my line. I searched the internet, but i didn't find any information on it.Now Im using this drawImage.alpha = lineOpacity; but this is not good, everything I draw before gets that opacity.I just want the alpha to be done on the new draw. im using this code now:

   //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;
    }

I hope someone can help me. Thanks in advance.

Edit: I use this for drawing:

- (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];
}

I really want to find out way it wont work, searched a long time on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-11-09 07:00:16

您可以创建一个 UIColor 并从中获取 CGColorRef,如下所示:

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

You can make a UIColor and get the CGColorRef from it like this:

strokeColor = CGColorRetain([UIColor colorWithRed:1.0f green:1.0f blue:0.0f alpha:0.5f].CGColor);
呢古 2024-11-09 07:00:16

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

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

The opacity is set with the alpha value of the stroke color (0.5 in this example):

strokeColor = CGColorCreateGenericRGB(1.0f, 1.0f, 0.0f, 0.5f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文