标签: drawrect

使用CGContextAddLineToPoint和CGContextSetLineWidth绘制一条非常细的线

我想在我的UIView的drawRect方法中绘制一条非常细的线条粗线.我看到的CGContextSetLineWidth值为0.5的行与用于绘制边框CALayer的相同1.0宽度值不匹配.

您可以看到两者之间的差异 - 红线(宽度= 1)比紫/蓝线(宽度= 0.5)薄得多.

用CALayer绘制的边框

这是我绘制伪1.0宽度水平线的方式:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetLineWidth(ctx, 0.5); // I expected a very thin line

CGContextMoveToPoint(ctx, 0, y);
CGContextAddLineToPoint(ctx, self.bounds.size.width, y);

CGContextStrokePath(ctx);
Run Code Online (Sandbox Code Playgroud)

这是相同视图的边框,这次使用1.0边框宽度:

UIView *myView = (UIView *)self;
CALayer *layer = myView.layer;
layer.borderColor = [UIColor redColor].CGColor;
layer.borderWidth = 1.0;
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来绘制我自己的自定义行,其宽度与CALayer版本相同?

core-graphics drawrect ios ios5 ios6

15
推荐指数
2
解决办法
1万
查看次数

UIImageView和drawInRect有什么区别?

我想在表格单元格中显示这么多图像.我知道两种显示图像的方法.

一个是为UIImageView创建一个实例并显示它

CGRect rect=CGRectMake(x,y,width,height);
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
[image setImage:[UIImage imageNamed:@"sample.jpg"]];
Run Code Online (Sandbox Code Playgroud)

另一种方法是,

CGRect rect=CGRectMake(x,y,width,height);
[[UIImage imageNamed:@"sample.jpg"] drawInRect:rect];
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,这两者有什么区别?哪一个有效?还是有其他功能比这更好?

提前致谢....

iphone uiimageview uiimage drawrect

14
推荐指数
1
解决办法
7777
查看次数

CALayer vs CGContext,这是一种更好的设计方法吗?

我一直在尝试iOS绘图.为了做一个实际练习,我写了一个BarChart组件.以下是类图(好吧,我不允许上传图像)所以让我用文字写.我有一个NGBarChartView,它继承自UIView有2个协议NGBarChartViewDataSource和NGBarChartViewDelegate.代码位于https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart/NGBarChartView.m

为了绘制barChart,我创建了每个barChart作为不同的CAShapeLayer.我这样做的原因有两个,首先我可以创建一个UIBezierPath并将其附加到CAShapeLayer对象和两个,我可以通过使用[Layer hitTest]方法轻松跟踪是否触摸了barItem.该组件运行良好.但是,我对绘制barCharts的方法感到不舒服.因此这个说明.我需要以下专家意见

  1. 通过使用CAShapeLayer并创建BarItems我真的没有使用UIGraphicsContext,这是一个很好的设计吗?
  2. 我的方法将在UIView中创建几个CALayers.根据性能,是否可以在UIView中创建CALayer的数量.
  3. 如果一个好的选择是使用CGContext*方法然后,有什么正确的方法来识别是否已触摸特定路径
  4. 从动画的角度来看,例如当你点击它时,条形图闪烁,更好的是图层设计或更好的CGContext设计.

非常感谢帮助.顺便说一句,你可以自由地查看我的代码和评论.我很乐意接受任何改进的建议.

最好的,Murali

calayer cgcontext drawrect cashapelayer ios

12
推荐指数
1
解决办法
4419
查看次数

Swift/UIView/drawrect - 如何在需要时获取drawrect进行更新

我是学习Swift的新手,我正试图让一个非常简单的应用程序运行.我想要做的就是在按下按钮时让UIView.drawRect更新.它会在应用程序首次加载时更新/绘制,然后在我尝试之后无需更新/绘制.几天来,我一直在反对这一点,我找不到任何帮助.

我建立:

  • 单一视图应用程序

  • 一个按钮,链接到视图控制器作为动作

  • 一个新类,Test_View,子类化UIView

ViewController代码:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var f = Test_View()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func Button_Pressed(sender: AnyObject) {
        var f = Test_View()
        f.setNeedsDisplay()
        NSLog("Button pressed.")
    }

}
Run Code Online (Sandbox Code Playgroud)

Test_View代码:

class Test_View: UIView {

    override func drawRect(rect: CGRect) {
        let h = rect.height
        let w …
Run Code Online (Sandbox Code Playgroud)

updating uiview drawrect swift

12
推荐指数
1
解决办法
4万
查看次数

MKAnnotationView drawRect:没有被调用

我已经实现了一个从MKAnnotation派生的自定义注释,称为ContainerAnnotation,以及一个从MKAnnotationView派生的自定义注释视图和一个名为ContainerAnnotationView的drawRect:方法.由于某种原因,drawRect:方法没有被调用,我无法弄清楚为什么.

这是我的注释视图的源代码.

ContainerAnnotationView.h:

@interface ContainerAnnotationView : MKAnnotationView
{
}

@end
Run Code Online (Sandbox Code Playgroud)

ContainerAnnotationView.m:

@implementation ContainerAnnotationView

- (void) drawRect: (CGRect) rect
{
    // Draw the background image.
    UIImage * backgroundImage = [UIImage imageNamed: @"container_flag_large.png"];
    CGRect annotationRectangle = CGRectMake(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height);
    [backgroundImage drawInRect: annotationRectangle];

    // Draw the number of annotations.
    [[UIColor whiteColor] set];
    UIFont * font = [UIFont systemFontOfSize: [UIFont smallSystemFontSize]];
    CGPoint point = CGPointMake(2, 1);
    ContainerAnnotation * containerAnnotation = (ContainerAnnotation *) [self annotation];
    NSString * text = [NSString stringWithFormat: @"%d", containerAnnotation.annotations.count];
    [text …
Run Code Online (Sandbox Code Playgroud)

iphone drawrect mkannotationview

11
推荐指数
1
解决办法
4204
查看次数

drawRect绘制'透明'文本?

我希望绘制一个UILabel(最好通过子类)作为透明标签,但具有坚实的背景.我画了一个简单的例子(对不起,这很难看,但它得到的分数:)).

基本上我有一个UILabel,我希望背景是一个设置颜色,文本应该是透明的.我不想用视图背景为文本着色,而是让它是100%透明的,因为我在背景中有一个纹理,我想确保标签内外的线条.

我一直在深夜浏览SO并在Google上搜索,但我找不到有用的消息来源.我对CG绘图没有太多经验,所以我很感激任何链接,帮助,教程或示例代码(也许Apple有一些我需要看一下?).

谢谢你!

在此输入图像描述

cgcontext uilabel drawrect

11
推荐指数
1
解决办法
6416
查看次数

UIBezierPath绘制具有不同笔划的圆

基本上我需要有一个不同颜色的笔划圆,大小相等.例如,1/2是蓝色,1/2是红色.图片(对不起这么糟糕的图片):

例

我怎么画这样的东西?

drawrect ios uibezierpath

11
推荐指数
1
解决办法
1万
查看次数

Swift 3:绘制一个矩形

我快3天了,我正在试图弄清楚如何画一个矩形.我对语言太新了,不知道要扩展的类和覆盖的方法,我已经四处寻找示例代码,但似乎没有任何工作(我将其归因于我对swift 3的使用).

我现在正在尝试的是:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        let k = Draw(frame: CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)))

        k.draw(CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)));
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
class Draw: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override …
Run Code Online (Sandbox Code Playgroud)

graphics draw drawrect swift swift3

11
推荐指数
2
解决办法
5万
查看次数

drawRect图纸的动画

我的应用程序的一部分是统计功能.我在一条UIView路径中绘制并用颜色填充它...现在我想制作一个动画,路径UIView从左侧到右侧.

我读到了CALayer,但我不确定它是如何工作的......一些示例代码或链接会很棒.

animation calayer drawrect ios

10
推荐指数
1
解决办法
2万
查看次数

绘制MKMapView Overlay,如Google Maps Directions

iOS 5改变了内置Google Maps App绘制路线的方式:

Apple的产品线

我现在想在我自己的应用程序中复制路径叠加的设计,但我目前只能绘制一条纯蓝线.我想添加渐变,边框和发光的3D效果.有关如何实现这一目标的任何想法?

目前我正在使用以下代码:

CGContextSetFillColorWithColor(context, fillColor.CGColor);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, lineWidth);
CGContextAddPath(context, path);
CGContextReplacePathWithStrokedPath(context);
CGContextFillPath(context);
Run Code Online (Sandbox Code Playgroud)

导致一条相当难看的线:

我的路线

谢谢!

更新:该解决方案应适用于iOS 4.0及更高版本.

core-graphics drawrect mapkit mkmapview ios

10
推荐指数
1
解决办法
2668
查看次数