【问题标题】:How to remove CGContext for line in ios?如何在ios中删除行的CGContext?
【发布时间】:2013-09-04 13:15:10
【问题描述】:

我正在用这两条线移动和添加线。

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

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

那么我的线条颜色将是清晰的颜色或线条在我前进时移除位置。

非常欢迎任何想法或建议。

【问题讨论】:

标签: iphone ios ipad uitouch uibezierpath


【解决方案1】:

只要您处于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);
    }
}

【讨论】:

    【解决方案2】:

    我遇到过同样的问题。直接我做不到,所以我使用了如下一些棘手的逻辑并为我工作。我只是给出提示,希望这个逻辑对你有帮助。

    我有使用 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);
    
    ...
    ...
    }
    

    希望你能得到一些提示来修正你的逻辑!!!

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 2017-04-13
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多