如何在iPhone中使用核心graphics制作思想泡泡
我正在用核心graphics制作各种形状的泡泡。
但是,我不确定如何做一个思想泡泡。
我从矩形做了圆angular矩形。 我是否使用相同的方法
做一个思想泡泡还是有其他方法?
我认为你可以用这个作为初步的指导,然后build立它:
如何在iPhone上以编程方式绘制椭圆形对话框?
这是samfu_1从那个post的回答
我会做两次迭代。 首先获取上下文并开始一个path。 填充一个椭圆,然后用三条线包围一个三angular形的自定义path。 我假设以下尺寸:70宽度,62高度。 在UIView的子类中重写绘制矩形,并在子类UIViewController中实例化:
-(void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0); CGContextFillEllipseInRect(ctx, CGRectMake(0.0, 0.0, 70.0, 50.0)); //oval shape CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, 8.0, 40.0); CGContextAddLineToPoint(ctx, 6.0, 50.0); CGContextAddLineToPoint(ctx, 18.0, 45.0); CGContextClosePath(ctx); CGContextFillPath(ctx); }
当在灰色的背景下添加时,在iPhone模拟器中产生:
第二个代码示例将几乎重复上面生成的内容。 我实现了这个使用灵活的大小,可以提供给UIView框架,当你实例化。 从本质上说,讲话泡泡的白色部分是用黑色的笔划画出来的。
-(void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect aRect = CGRectMake(2.0, 2.0, (self.bounds.size.width * 0.95f), (self.bounds.size.width * 0.60f)); // set the rect with inset. CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); //white fill CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0); //black stroke CGContextSetLineWidth(ctx, 2.0); CGContextFillEllipseInRect(ctx, aRect); CGContextStrokeEllipseInRect(ctx, aRect); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, (self.bounds.size.width * 0.10), (self.bounds.size.width * 0.48f)); CGContextAddLineToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); CGContextAddLineToPoint(ctx, 20.0, (self.bounds.size.height *0.70f)); CGContextClosePath(ctx); CGContextFillPath(ctx); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, (self.bounds.size.width * 0.10), (self.bounds.size.width * 0.48f)); CGContextAddLineToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); CGContextStrokePath(ctx); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); CGContextAddLineToPoint(ctx, 20.0, (self.bounds.size.height *0.70f)); CGContextStrokePath(ctx); }
编辑:
你也可以在这篇文章中参考Brad Larson的答案
如何在iPhone上绘制“语音泡泡”?
希望这可以帮助你。
让我知道如果你需要更多的帮助。