繁体   English   中英

如何在iOS中删除CGContext的行?

[英]How to remove CGContext for line in ios?

我正在移动并在这两行中添加行。

  CGContextMoveToPoint(context, 20, 185);
  CGContextAddLineToPoint(context, 20, 185);

我想知道如何删除行(例如CGContextAddLineToPoint(context,20,185); // addline)。 我想在移动时删除线条,或者如果我的数组== 2,则清除线条颜色;

那么当我向前移动时,线条颜色将变为清晰的颜色,或者线条将删除该位置。

任何想法或建议都将受到欢迎。

只要您在drawRect:方法中,就可以只画线(如果需要),而无需清除它。 在开始绘制之前, drawRect:方法被调用了很多次,并且视图实质上已被“清除”。 因此,您的代码可能看起来像这样:

- (void)drawRect:(CGRect)rect {
    // other drawing code and declaration of array variable...
    if ([array count] == 2) {
        CGContextMoveToPoint(context, 20, 185);
        CGContextAddLineToPoint(context, 20, 185);
    }
}

我遇到过同样的问题。 我直接无法做到这一点,因此我使用了如下一些棘手的逻辑并为我工作。 我只是暗示一下,希望这种逻辑对您有帮助。

我有使用drawRect方法绘制的带有下划线的按​​钮,因此在某些匹配的情况下,我需要将其删除,所以我喜欢这样:

注意:“ isRemoveUnderLine”是我的自定义按钮类中的Bool属性

if ([array count] == 2) {
     myButton.isRemoveUnderLine = YES;
     [myButton setNeedsDisplay];  //it will update your button context and call drawRect method again...
} 

// drawRect方法的代码如下:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();

    if(_isRemoveUnderLine)
        CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
    else
        CGContextSetStrokeColorWithColor(context, self.currentTitleColor.CGColor);

...
...
}

希望您能得到一些提示以修正您的逻辑!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM