Tag: core graphics

检测贝塞尔path内的水龙头

我有一个UIView作为子视图添加到我的视图控制器。 在这个观点上,我画了一条更加缓慢的道路。 我的drawRect实现如下 – (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); UIBezierPath *bpath = [UIBezierPath bezierPath]; [bpath moveToPoint:CGPointMake(50, 50)]; [bpath addLineToPoint:CGPointMake(100, 50)]; [bpath addLineToPoint:CGPointMake(100, 100)]; [bpath addLineToPoint:CGPointMake(50, 100)]; [bpath closePath]; CGContextAddPath(context, bpath.CGPath); CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor); CGContextSetLineWidth(context, 2.5); CGContextStrokePath(context); UIColor *fillColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.5 alpha:0.7]; [fillColor setFill]; [bpath fill]; } 我想检测这个贝塞尔path内部的点击,而不是UIView内部和path外部的点。 例如,在这种情况下,如果我的触摸坐标是(10,10),则不应该检测到。 我知道关于CGContextPathContainsPoint,但是当触摸在path中时,它不起作用。 有没有办法检测贝塞尔path内的触摸事件?

如何绘制一个三angular形的UIButton

我真的挖了IFTTT右下angular的button,如图(右下)所示。 我尝试过使用CGContext和UIBezierPath来匹配效果,但是我根本不知道如何正确使用它。 有没有人有一些想法/代码如何做到这一点? 谢谢!

CGContext的静态背景

这是我的第一个问题,所以大家好。 我花了一些时间来思考以下问题,不能提出解决scheme。 如果有人能指出我正确的方向,我会很高兴。 我使用CGContext来绘制各种图层的实时数据表示图层,应该以至less50fps的合理帧率刷新。 我想使用静态图像作为背景。 使用每个周期的上下文重新绘制此图像会显着降低帧速率。 我想过使用UIImageView来显示图像一次。 这里的问题是,我想要使用CGContext提供的blendmodes来实现更复杂的覆盖,而不仅仅是为每个图层设置一个alpha值。 由于显示背景图像的UIImageView不是上下文的一部分,因此不受混合模式的影响。 有没有人有一个想法如何实现所需的行为,而不会影响帧率太多? 谢谢你的帮助。

EXC_BAD_ACCES绘制阴影

我想添加一个阴影到我的UIView,但在我的drawRect方法,我得到一个EXC_BAD_ACCESS。 (我正在使用ARC) -(void) drawRect:(CGRect)rect { CGColorRef lightColor = [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8].CGColor; CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4].CGColor; CGContextRef context = UIGraphicsGetCurrentContext(); // Draw shadow CGContextSaveGState(context); CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), 10, shadowColor); CGContextSetFillColorWithColor(context, lightColor); CGContextFillRect(context, _coloredBoxRect); CGContextRestoreGState(context); } 错误信息:线程1:编程接收到的信号:“EXC_BAD_ACCESS”。 行: CGContextSetFillColorWithColor(context, lightColor); 当我将这一行更改为: [[UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:0.8] setFill]; 我得到同样的错误,但在这一行: CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), […]

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处理,但是实现如此简单的操作看起来过于复杂。

绘制CGContextRef以消除像素化

目前,我正在绘制小点graphics问题。 我注意到,在大多数专业日历应用程序中,事件日历标识是一个小点,其颜色是事件日历颜色。 我目前在我的应用程序点,我需要画一个更好的点。 下面是我的意思的照片。 在这里可能不太明显,但是在我的手机上,这个彩色的点是非常像素的。 我想删除像素化。 我经历了Ray Wenderlich的网站Ray Wenderlich Core Graphics的教程。 下面是我画出来的点子: UIGraphicsBeginImageContext(cell.imageViewPic.bounds.size); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]); CGContextFillEllipseInRect(contextRef,(CGRectMake (0.f, 0.f, cell.imageViewPic.bounds.size.width, cell.imageViewPic.bounds.size.height))); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); cell.imageViewPic.image = image; 所以这实际上是实现了这个圆,但是我加了这个在圆周上划了一条小线来消除像素,但是没有去。 CGPoint startPoint = CGPointMake(cell.imageViewPic.frame.origin.x, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height – 1); CGPoint endPoint = CGPointMake(cell.imageViewPic.frame.origin.x + cell.imageViewPic.frame.size.width – 1, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height – […]

CGContext:如何擦除位图上下文之外的像素(例如kCGBlendModeClear)?

我正在尝试使用Core Graphics构build一个橡皮擦工具,而且我发现制作高性能橡皮的难度令人难以置信 – 这一切都归结为: CGContextSetBlendMode(context, kCGBlendModeClear) 如果你谷歌周围如何与核心graphics“擦除”,几乎每个答案都回来这个片段。 问题是它只是(显然)在位图上下文中工作。 如果你正在尝试实现交互式擦除,我不明白kCGBlendModeClear如何帮助你的 – 就我所知,你或多或less被locking在擦除UIImage / CGImage的屏幕上和在着名的非高性能[UIView drawRect] 。 这是我所能做到的最好的: -(void)drawRect:(CGRect)rect { if (drawingStroke) { if (eraseModeOn) { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); [eraseImage drawAtPoint:CGPointZero]; CGContextAddPath(context, currentPath); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, lineWidth); CGContextSetBlendMode(context, kCGBlendModeClear); CGContextSetLineWidth(context, ERASE_WIDTH); CGContextStrokePath(context); curImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [curImage drawAtPoint:CGPointZero]; } else { [curImage drawAtPoint:CGPointZero]; […]

在iOS中擦除绘图

我正在制作一个绘图应用程序,我有一个UIBezeirPath,用它在touchesMoved中绘制,并将其转换为CGPath,然后绘制到CGlayer, 这是我的代码 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { self.currentPath = [[DrawingPath alloc] init]; if(m_eraseButtonClicked) { [self.currentPath setPathColor:[UIColor backgroundColor]]; } else { [self.currentPath setPathColor:self.lineColor]; } CGPathRef cgPath = self.currentPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(cgPath); } – (void)Erase { CGContextRef layerContext = CGLayerGetContext(self.DrawingLayer); CGContextSetBlendMode(layerContext,kCGBlendModeClear); CGContextSetLineWidth(layerContext, self.eraseWidth); CGContextBeginPath(layerContext); CGContextAddPath(layerContext, mutablePath); CGContextStrokePath(layerContext); } – (void)UndoRedoFunction { //Undo/Redo for(int i =0; i<[undoArray count];i++) […]

如何在Swift中用draw(_ rect:CGRect)绘制一个不同的混合模式的UILabel

所以我有一个UILabel正在绘制的梯度图像(这是一个UIImageView )的顶部。 它看起来像这样: 我试图在UILabel的draw(_ rect: CGRect)函数中更改graphics上下文的blendMode ,以便绘制标签,但是使用softLight混合模式与背景混合。 这是我想要的样子: 这里是我在draw(_ rect: CGRect)函数中的代码: override func draw(_ rect: CGRect) { // Drawing code if let context = UIGraphicsGetCurrentContext() { context.setBlendMode(.softLight) self.textColor.set() self.drawText(in: rect) } } 此代码只呈现顶部图片而不是混合版本。 这并不是我所尝试过的,我尝试了很多其他的东西,但是我不明白CGContext和draw函数。 在SO上的大多数答案要么在旧的Objective-C中,并且被弃用/破坏或者太快和太复杂(例如使用Metal)。 我只想在上下文中绘制文本,并将上下文的混合模式设置为柔和光线。 必须有一个简单的方法! 任何帮助如何解决这个将不胜感激。 谢谢!

drawRect和Interface Builder之间的颜色差异?

简单地说,我在界面生成器中有两个视图,一个是使用界面生成器中的RGB滑块设置为99,99,99的颜色。 另一个视图以编程方式着色以达到一定的形状。 我用它填充它: //Obviously, this is in drawRect. [[UIColor leadColor] set]; CGContextEOFillPath(myContext); //And this is a category on UIColor + (UIColor *)leadColor { return [UIColor colorWithWhite:99/255.0 alpha:1.0]; } 结果: 为什么这种差异存在? 编辑:(不必要的drawRect代码删除) EDIT2: 所以,我在这里对自己说谎。“接口build设者显示RGB 99,99,99为80,80,80,我敢打赌,它抵消了数字19。 > … …一个绝望的人使用Xcode认为这样的疯狂的东西..结果: 完善!! ,但为什么 ? 另一个Xcode错误? 我发现过去一个月里有10个人