Tag: drawrect

从端点调整行大小

我在一个视图上绘制注释。 线条注释引起问题; 我有一个父类的形状(从UIView扩展)。 所有的注释都是形状的子类。 在每个注解类中,我都覆盖了drawRect方法。 DrawingCanvas类(从UIView扩展,是我的viewController的父视图的子视图)处理平移手势。 这是一段代码 -(void) panGestureRecognizer: (UIPanGestureRecognizer *) panGesture { static CGPoint initialPoint; CGPoint translation = [panGesture translationInView:panGesture.view]; static Shape *shape; if(panGesture.state == UIGestureRecognizerStateBegan) { initialPoint = [panGesture locationInView:panGesture.view]; if(selectedShape == nil) { if([selectedOption isEqualToString:@"line"]) { shape = [[Line alloc] initWithFrame:CGRectMake(initialPoint.x, initialPoint.y, 10, 10) isArrowLine:NO]; ((Line *)shape).pointStart = [panGesture.view convertPoint:initialPoint toView:shape]; } […]

iOS 5 UIView drawRect覆盖不能在设备上工作

我正在准备我的iPhone应用程序发布iOS 5 GM,并遇到与UIView的错误。 当我重载子类的drawRect方法时,模拟器显示所需的结果,但是当我尝试在实际设备上进行testing时,drawRect覆盖没有任何效果。 我甚至在一个drawRect覆盖内放置了一个日志语句,并确认它正在被调用。 有没有人注意到这个问题? 以下是我正在使用的覆盖代码: – (void)drawRect:(CGRect)rect { DebugLog( @"*** calling drawRect ***" ); CGContextRef context = UIGraphicsGetCurrentContext(); // draw the dark gray background CGContextSetFillColor(context, [SkinManager getWhiteColor]); CGContextFillRect(context, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height)); // draw the text field background in white CGContextSetFillColor(context, [SkinManager getDarkGray]); CGContextFillRect(context, CGRectMake(2.0, 2.0, rect.size.width – 4, rect.size.height – 4)); } […]

从一个UIColor手动颜色渐变到另一个

我试图在drawRect淡入一个UIColor到另一个UIColor 。 我创build了这个函数来计算一定比例的颜色: – (UIColor *)colorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor percent:(float)percent { float dec = percent / 100.f; CGFloat fRed, fBlue, fGreen, fAlpha; CGFloat tRed, tBlue, tGreen, tAlpha; CGFloat red, green, blue, alpha; if(CGColorGetNumberOfComponents(fromColor.CGColor) == 2) { [fromColor getWhite:&fRed alpha:&fAlpha]; fGreen = fRed; fBlue = fRed; } else { [fromColor getRed:&fRed green:&fGreen blue:&fBlue alpha:&fAlpha]; } if(CGColorGetNumberOfComponents(toColor.CGColor) […]

UIView中的黑色背景?

我在网上跟随一个教程来绘制一个子类UIView。 本教程展示了一个带有白色背景的UIView,我通过简单的改变super的bg颜色来解决这个问题。 问题是,当触摸结束时,背景不清晰。 我不知道。 我只是试图设置填充颜色[uicolor clearcolor]; 不成功。 这是我正在使用的代码: @implementation CachedLIView { UIBezierPath *path; UIImage *incrementalImage; // (1) } – (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self setMultipleTouchEnabled:NO]; [self setBackgroundColor:[UIColor whiteColor]]; path = [UIBezierPath bezierPath]; [path setLineWidth:2.0]; } return self; } – (void)drawRect:(CGRect)rect { [incrementalImage drawInRect:rect]; // (3) [path stroke]; } – (void)touchesBegan:(NSSet […]

animationUIView的CoreGraphics / drawRect内容

是否有可能animationUIView的CoreGraphics内容? 说我有一个名为MyView的UIView子类,实现了像这样的drawRect:方法: – (void) drawRect: (CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(c, someColor); CGContextSetLineWidth(c, someWidth); CGContextMoveToPoint(c, leftOfMyUIView, topOfMyUIView); CGContextAddLineToPoint(c, leftOfMyUIView, bottomOfMyUIView); CGContextStrokePath(c); } 换句话说,它在UIView的子类的左边画一条垂直线。 现在有一个简单的方法来animation这个UIView,使行从视图的左侧移动到右侧? 我希望它从左到右“漂移”。 要清楚 – 由于我不会去的原因,我不能移动/animationMyView的实例。 我能想到的最好的解决scheme是添加一个新的UIView子类。 像LineView extends UIView东西LineView extends UIView ,然后使LineView我要绘制的行的确切尺寸,使用自己的drawRect方法填充它,然后将LineView的实例添加到MyView作为子视图。 这将允许我使用animation块对LineView对象的位置进行animation处理,但是实现如此简单的操作看起来过于复杂。

UIView覆盖drawRect会导致视图不服从masksToBounds

我想重写我的自定义视图中UIView的drawRect:方法。 但是,我的观点有一个边界半径定义为: sub = [[[NSBundle mainBundle] loadNibNamed:@"ProfileView" owner:self options:nil] objectAtIndex:0]; [self addSubview:sub]; [sub setUserInteractionEnabled:YES]; [self setUserInteractionEnabled:YES]; CALayer *layer = sub.layer; layer.masksToBounds = YES; layer.borderWidth = 5.0; layer.borderColor = [UIColor whiteColor].CGColor; layer.cornerRadius = 30.0; 这完美地工作,并在我的视图周围放置一个带有边框半径的漂亮边框(不要介意后面的对angular/直线白线,它们与此视图无关): 但是,当我尝试重写我的视图中的drawRect:方法时,我可以看到一个黑色的背景不掩盖边界。 我没有做任何事情 (目前),这是我的代码: -(void)drawRect:(CGRect)rect{ [super drawRect:rect]; } 结果如下: 我只改变了绘制方法。 我怎样才能重写绘制方法,同时保持我的观点服从angular圆angular面具? 这是在iOS的错误,或者我错过了什么?

在iOS上,setNeedsDisplay真的不会导致drawRect被调用…除非CALayer的显示或drawInContext终于调用drawRect?

我不太了解CALayer的display和drawInContext如何与视图中的drawRect相关联。 如果我有一个每隔1秒设置[self.view setNeedsDisplay]的NSTimer,那么drawRect每1秒调用一次,如drawRect的NSLog语句所示。 但是,如果我inheritance一个CALayer并将其用于视图,如果我使display方法为空,那么现在drawRect永远不会被调用。 更新:但display每1秒调用一次,如NSLog语句所示。 如果我删除那个空的display方法并添加一个空的drawInContext方法, drawRect再也不会被调用。 更新:但是drawInContext每隔1秒调用一次,如NSLog语句所示。 究竟发生了什么? 看起来display可以select性地调用drawInContext , drawInContext可以select性地调用drawRect (怎么样?),但是这里的真实情况是什么? 更新:有更多的线索回答: 我将CoolLayer.m代码更改为以下内容: -(void) display { NSLog(@"In CoolLayer's display method"); [super display]; } -(void) drawInContext:(CGContextRef)ctx { NSLog(@"In CoolLayer's drawInContext method"); [super drawInContext:ctx]; } 所以,如果在视图中的位置(100,100)处有一个月亮(如Core Graphics绘制的圆圈),现在我将其更改为位置(200,200),自然,我将调用[self.view setNeedsDisplay] ,现在,CALayer将根本没有caching新的视图图像,因为我的drawRect指出现在应该如何显示月亮。 即使如此,入口点是CALayer的display ,然后CALayer的drawInContext :如果我在drawRect设置一个断点,调用堆栈显示: 所以我们可以看到CoolLayer的display先input,然后进入CALayer的display ,然后是CoolLayer的drawInContext ,然后是CALayer的drawInContext ,即使在这种情况下,新图像也不存在这样的caching。 最后,CALayer的drawInContext调用委托的drawLayer:InContext 。 委托是视图( drawLayer:InContext或UIView)…和drawLayer:InContext是UIView中的默认实现(因为我没有覆盖它)。 最后是drawLayer:InContext调用drawRect 。 所以我猜两点:为什么它进入CALayer即使没有caching的图像? 因为通过这种机制,图像被绘制在上下文中,最后返回display ,CGImage从这个上下文创build,然后它被设置为新的图像caching。 […]

用CGBitmapContext绘制速度太慢

所以我在这个过程中有一个基本的绘图应用程序,可以让我画线。 我画到一个屏幕位图,然后在drawRect呈现图像。 它的工作方式太慢了,在用手指画出来之后更新约半秒钟。 我拿了代码,并从这个教程中调整它, http://www.youtube.com/watch?v= UfWeMIL-Nu8&feature=relmfu,你可以在评论中看到人们也说它太慢, t回应。 那我该如何加速呢? 还是有更好的方法来做到这一点? 任何指针将不胜感激。 下面是我的DrawView.m的代码。 -(id)initWithCoder:(NSCoder *)aDecoder { if ((self=[super initWithCoder:aDecoder])) { [self setUpBuffer]; } return self; } -(void)setUpBuffer { CGContextRelease(offscreenBuffer); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); offscreenBuffer = CGBitmapContextCreate(NULL, self.bounds.size.width, self.bounds.size.height, 8, self.bounds.size.width*4, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); CGContextTranslateCTM(offscreenBuffer, 0, self.bounds.size.height); CGContextScaleCTM(offscreenBuffer, 1.0, -1.0); } -(void)drawToBuffer:(CGPoint)coordA :(CGPoint)coordB :(UIColor *)penColor :(int)thickness { CGContextBeginPath(offscreenBuffer); […]

CALayer与CGContext,这是一个更好的devise方法?

我一直在做一些iOS绘图的实验。 为了做一个实际的练习,我写了一个BarChart组件。 下面是类图(呃,我不允许上传图片),所以让我用文字来写。 我有一个NGBarChartViewinheritance自UIView有2个协议NGBarChartViewDataSource和NGBarChartViewDelegate。 代码是在https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart/NGBarChartView.m 为了绘制barChart,我创build了每个barChart作为不同的CAShapeLayer。 我这样做的原因有两个,首先我可以创build一个UIBezierPath并将其附加到一个CAShapeLayer对象和两个,我可以通过使用[Layer hitTest]方法轻松跟踪是否触摸barItem。 该组件工作得很好。 然而,我不习惯用我绘制barCharts的方法。 因此,这个笔记。 我需要关于以下方面的专家意见 通过使用CAShapeLayer和创buildBarItems我真的不使用UIGraphicsContext,这是一个很好的devise? 我的方法将在UIView中创build几个CALayers。 基于性能的限制,你可以在UIView中创buildCALayers的数量。 如果一个好的select是使用CGContext *方法,那么怎样才能确定一个特定的path是否被触及 从animation的angular度来看,比如当你点击它时Bar闪烁,是Layerdevise更好还是CGContextdevise更好。 非常感谢帮助。 顺便说一句,你可以自由地看我的代码和评论。 我会很乐意接受任何改善build议。 最好的,Murali

UIBezierPath绘制不同笔画的圆

基本上我需要有一个不同颜色的笔画,大小相等。 例如,1/2是蓝色,1/2是红色。 图片(对不起,这么糟糕的形象): 我怎样才能画出这样的东西?