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

时间:2011-04-18 20:05:04

标签: xcode ipad line alpha

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

我真的想找出它不会工作的方式,长时间搜索它。

2 个答案:

答案 0 :(得分:2)

你可以制作一个UIColor并从中获取CGColorRef:

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

答案 1 :(得分:0)

不透明度使用笔触颜色的alpha值设置(本例中为0.5):

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