我是新来的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];
}
我真的想办法,找出它不会工作,寻觅了很长一段时间就可以了。
当我包含你的代码时,我的应用程序退出。你不知道另一种方式?就像我上面发布的那样,我可以使用strokeColor.setOpacity(0.2))。但是当我以这种方式使用它时,我得到如下结果:i55.tinypic.com/zufbbq.png – klaasdm 2011-04-19 08:29:52
对不起,我没有仔细查看你的代码。很明显,我的方法仍然需要保留CGColor对象。 – gerry3 2011-04-19 18:59:28
你知道我该如何解决这个问题吗?i55.tinypic.com/zufbbq.png。我尝试了一切,但我无法修复它(球中的球)。非常感谢 – klaasdm 2011-04-19 20:07:46