2011-04-18 21 views
3

我是新来的xCode,我有一个问题。我正在为我的Ipad做一个应用程序。我想突出显示文字,但是我无法设置我的线条的不透明度。我搜索了互联网,但我没有找到任何有关它的信息。现在我使用这个drawImage.alpha = lineOpacity;但是这并不好,我之前画的所有东西都会得到那种不透明性。我只想在新的绘制中完成alpha。现在就在此处使用此代码:在Xcode中为钛画线不透明

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

可以做出的UIColor并获得CGColorRef从这样的:

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

当我包含你的代码时,我的应用程序退出。你不知道另一种方式?就像我上面发布的那样,我可以使用strokeColor.setOpacity(0.2))。但是当我以这种方式使用它时,我得到如下结果:i55.tinypic.com/zufbbq.png – klaasdm 2011-04-19 08:29:52

+0

对不起,我没有仔细查看你的代码。很明显,我的方法仍然需要保留CGColor对象。 – gerry3 2011-04-19 18:59:28

+0

你知道我该如何解决这个问题吗?i55.tinypic.com/zufbbq.png。我尝试了一切,但我无法修复它(球中的球)。非常感谢 – klaasdm 2011-04-19 20:07:46

0

的不透明度被设为与笔划颜色的α值(在本例0.5):

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

我试过了,但它说我不能使用它。错误:'CGColorCreateGenericRGB'不可用(在/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGColor.h:29中声明)。我也尝试设置笔画不透明度(不是你的方式,只是strokColor.setOpacity(0.2)),我得到了这样的结果:http://i55.tinypic.com/zufbbq.png。 – klaasdm 2011-04-18 21:58:11

+0

您的结果显示不透明度正在工作。你可能不喜欢的是不完整的结果。这很可能是由于绘制了几条单独的线段而不是一条连续的路径。使用单独的线段时,重叠线段的颜色会相加并导致黑色斑点。要避免它,请将所有点添加到路径并绘制路径。 – Codo 2011-04-19 06:00:50

+0

这对我有用:CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0f,1.0f,0.0f,0.2f);但是我得到了如上图所示的相同问题。我添加了用于在视图上绘制的代码。你能解释我如何解决这个问题吗?提前致谢! – klaasdm 2011-04-19 09:49:07