首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用CGContext方法清除旧数据的绘制?

如何使用CGContext方法清除旧数据的绘制?
EN

Stack Overflow用户
提问于 2014-02-23 19:39:08
回答 4查看 8.7K关注 0票数 0

我有一个iPad应用程序,在这个应用程序中,我沿着x轴绘制一个日期网格,并沿着y轴绘制时间。然后,我在这个网格的特定部分(参见图像)绘制彩色条形图(使用CGContext方法)。

当我试图重新画网格时,旧的条子还在那里!我怎么才能把旧的酒吧弄出去?我试过了在谷歌上找到的所有东西,但似乎都没有用。

更新1:这里是“驾驶代码”..。请注意,没有使用CGRect

代码语言:javascript
运行
复制
CGContextRef currentContext = UIGraphicsGetCurrentContext();  // Get the current graphics context
// Start the line at this point (x,y)
CGContextMoveToPoint(currentContext, column, startPosY);

// compute end point  (additional fDurationSegments takes line width into consideration)
CGContextAddLineToPoint(currentContext, column,  startPosY + (fDurationSegments * FIFTEEN_MINUTE_INCREMENT));

//  draw the colored appointment line
CGContextSetLineDash(currentContext, 0, nil, 0);  //  reset dashed line to straight line
CGContextSetLineWidth(currentContext, LINE_WIDTH);  // Set the width for the lines

CGContextStrokePath(currentContext);  //  draw 'em

更新2: --我在网格视图上放置了另一个UIView,使其透明并绘制了条形图.还是没有用新的代替旧的东西。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-02-25 03:37:22

来自苹果开发者论坛:

为了让这一切发挥作用,viewController需要参与进来。viewController需要一个IBOutlet来访问故事板中创建的实际WeeksAppts视图。您可以通过控件-从情节提要中的视图拖动到IBOutlet源代码来创建viewController。视图控制器中的IBOutlet将如下所示

代码语言:javascript
运行
复制
@property (weak, nonatomic) IBOutlet WeeksAppts *weeksApptsView;

当某些事情发生变化时,视图控制器需要强制使用如下代码行对视图进行更新

代码语言:javascript
运行
复制
[self.weeksApptsView setNeedsDisplay];
票数 1
EN

Stack Overflow用户

发布于 2014-02-23 22:29:11

您应该使用CGContextClearRect清除以前的绘图,但是对于任何一种严肃的答案,请在这里提供您的代码。

票数 1
EN

Stack Overflow用户

发布于 2015-11-13 17:51:00

经过许多考验和磨难,我找到了解决你所面临的同样问题的办法。我曾经在StackOverflow上看过其他类似的问题,但是non已经消失了。因此,这个概念如下:

-- CGContext的清晰程序

  1. 画一个红色三角形
  2. CGContextBeginTransparencyLayer(上下文,零)
  3. 画一个蓝色的圆
  4. CGContextClearRect(上下文、边界)//将只在CGContextBeginTransparencyLayer调用之后生成清晰的图形
  5. 画一个黄色正方形
  6. CGContextEndTransparencyLayer(上下文)

结果:现在视图中有一个红色三角形和一个黄色方块(蓝色圆圈被移除)

注意:通过使用CGContextBeginTransparencyLayer和CGContextEndTransparencyLayer,您可以避免在应用程序上下文中的每个图形中都会出现一个透明的漏洞,如果您使用的是CGContextClearRect而不使用上面描述的TransparencyLayer过程的话。

一个有效的Swift游乐场示例:

代码语言:javascript
运行
复制
import Cocoa
import XCPlayground

class A:Shape{
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        let nsctx:NSGraphicsContext/**/ = NSGraphicsContext.currentContext()!
        let context/**/  = nsctx.CGContext

        let path:CGMutablePathRef  = CGPathCreateMutable();
        let rectangle:CGRect = CGRectMake(0.0, 0.0,200.0, 200.0);
        CGPathAddRect(path,nil, rectangle);
        CGContextAddPath(context,path)
        CGContextSetFillColorWithColor(context,NSColor.greenColor().CGColor)
        CGContextDrawPath(context, CGPathDrawingMode.Fill)

    }
}
class B:Shape{
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        let nsctx:NSGraphicsContext/**/ = NSGraphicsContext.currentContext()!
        let context/**/  = nsctx.CGContext//was graphicsPort

        CGContextBeginTransparencyLayer(context, nil);

        CGContextAddPath(context,CircleParser.circlePath(100,100,100))
        CGContextSetFillColorWithColor(context,NSColor.purpleColor().CGColor)
        CGContextDrawPath(context, CGPathDrawingMode.Fill)

        CGContextClearRect(context, NSMakeRect(0, 0, dirtyRect.width, dirtyRect.height))

        CGContextAddPath(context,CircleParser.circlePath(60,60,50))
        CGContextSetFillColorWithColor(context,NSColor.blueColor().CGColor)
        CGContextDrawPath(context, CGPathDrawingMode.Fill)

        CGContextEndTransparencyLayer(context);
    }
}

class Shape:FlippedView{
    init() {
        let frame = NSRect(x: 0, y: 0, width: 300, height: 300)
        super.init(frame: frame)
    }
    /*
     * Required by super class
     */
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
class Container:FlippedView{
    init() {
        let frame = NSRect(x: 0, y: 0, width: 500, height: 500)
        super.init(frame: frame)
        //self.wantsLayer = true
    }
    /*
     * Required by super class
     */
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class FlippedView:NSView {//Organizes your view from top to bottom
    override var flipped:Bool {
        get {
            return true
        }
    }
}

let container = Container()
let a = A()
let b = B()
container.addSubview(a)
container.addSubview(b)

XCPlaygroundPage.currentPage.liveView = container
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21973470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档