我有一个iPad应用程序,在这个应用程序中,我沿着x轴绘制一个日期网格,并沿着y轴绘制时间。然后,我在这个网格的特定部分(参见图像)绘制彩色条形图(使用CGContext方法)。
当我试图重新画网格时,旧的条子还在那里!我怎么才能把旧的酒吧弄出去?我试过了在谷歌上找到的所有东西,但似乎都没有用。
更新1:这里是“驾驶代码”..。请注意,没有使用CGRect
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,使其透明并绘制了条形图.还是没有用新的代替旧的东西。
发布于 2014-02-25 03:37:22
来自苹果开发者论坛:
为了让这一切发挥作用,viewController需要参与进来。viewController需要一个IBOutlet来访问故事板中创建的实际WeeksAppts视图。您可以通过控件-从情节提要中的视图拖动到IBOutlet源代码来创建viewController。视图控制器中的IBOutlet将如下所示
@property (weak, nonatomic) IBOutlet WeeksAppts *weeksApptsView;
当某些事情发生变化时,视图控制器需要强制使用如下代码行对视图进行更新
[self.weeksApptsView setNeedsDisplay];
发布于 2014-02-23 22:29:11
您应该使用CGContextClearRect
清除以前的绘图,但是对于任何一种严肃的答案,请在这里提供您的代码。
发布于 2015-11-13 17:51:00
经过许多考验和磨难,我找到了解决你所面临的同样问题的办法。我曾经在StackOverflow上看过其他类似的问题,但是non已经消失了。因此,这个概念如下:
-- CGContext的清晰程序
结果:现在视图中有一个红色三角形和一个黄色方块(蓝色圆圈被移除)
注意:通过使用CGContextBeginTransparencyLayer和CGContextEndTransparencyLayer,您可以避免在应用程序上下文中的每个图形中都会出现一个透明的漏洞,如果您使用的是CGContextClearRect而不使用上面描述的TransparencyLayer过程的话。
一个有效的Swift游乐场示例:
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
https://stackoverflow.com/questions/21973470
复制相似问题