Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
画一条路径-iPhone_Iphone_Objective C_Xcode_Cgcontext_Cakeyframeanimation - Fatal编程技术网

画一条路径-iPhone

画一条路径-iPhone,iphone,objective-c,xcode,cgcontext,cakeyframeanimation,Iphone,Objective C,Xcode,Cgcontext,Cakeyframeanimation,我正在iPhone上沿路径设置对象动画。使用cakeyKeyMeanimation,该代码非常有效。出于调试目的,我想在屏幕上画一条线。我似乎无法使用当前的CGContextRef实现这一点。无论我看到哪里,它都说需要对UIView进行子类化,然后重写drawRect方法在屏幕上绘制。这有什么关系吗?如果不是,如何将数据传递给drawRect方法,以便它知道要绘制什么 编辑: 好啊我最终对UIView进行了子类化,并在drawRect方法中实现了绘图。我可以通过在子类视图(称为drawPath)

我正在iPhone上沿路径设置对象动画。使用
cakeyKeyMeanimation
,该代码非常有效。出于调试目的,我想在屏幕上画一条线。我似乎无法使用当前的
CGContextRef
实现这一点。无论我看到哪里,它都说需要对UIView进行子类化,然后重写drawRect方法在屏幕上绘制。这有什么关系吗?如果不是,如何将数据传递给drawRect方法,以便它知道要绘制什么

编辑:
好啊我最终对UIView进行了子类化,并在drawRect方法中实现了绘图。我可以通过在子类视图(称为drawPath)中创建另一个方法,设置一个实例变量,然后调用setNeedsDisplay,将其绘制到屏幕上。这反过来激发drawRect方法,该方法使用实例变量绘制到屏幕。这是最好的做法吗?如果我想画20多条路径会发生什么。我不应该为所有这些创建属性

如果要使用核心图形绘图,请使用
CALayer
。如果不想将其子类化,请将图形委托给视图

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
       CALayer* myLayer = [CALayer new];
       myLayer.delegate = self;
       self.layer = myLayer;
    }
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
  // draw your path here
}

不要忘记调用
[self.layer setNeedsDisplay]当需要重新绘制时。

如果要使用核心图形绘制,请使用
CALayer
。如果不想将其子类化,请将图形委托给视图

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
       CALayer* myLayer = [CALayer new];
       myLayer.delegate = self;
       self.layer = myLayer;
    }
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
  // draw your path here
}

不要忘记调用
[self.layer setNeedsDisplay]当您需要重新绘制它时。

在UIView的drawRect方法中放置一些如下代码

    CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 0,0 , 0.0);
CGContextSetLineWidth(context, 3.f);

CGContextBeginPath(context);
CGContextMoveToPoint(context,x1,y1);

CGContextAddLineToPoint(context, x2 , y2);
CGContextStrokePath(context);

在UIView的drawRect方法中,输入如下代码

    CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 0,0 , 0.0);
CGContextSetLineWidth(context, 3.f);

CGContextBeginPath(context);
CGContextMoveToPoint(context,x1,y1);

CGContextAddLineToPoint(context, x2 , y2);
CGContextStrokePath(context);

您可以在viewDidLoad方法中实现它。将图层设置为视图,委托给视图控制器。您可以在viewDidLoad方法中实现它。将图层设置为视图,将其委托给视图控制器。