Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/45.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 在UIView子类中调用drawRect_Iphone_Objective C_Ios_Ipad - Fatal编程技术网

Iphone 在UIView子类中调用drawRect

Iphone 在UIView子类中调用drawRect,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,我已经对UIView进行了子类化,并实现了drawRect。我基本上希望在设置CGPoint之后调用这个drawRect,CGPoint是这个UIView的一个属性。我该怎么做?这是我的密码: -(id) initWithCoder:(NSCoder *)aDecoder { if(self = [super initWithCoder:aDecoder]) { point_.x = 0; self.page_ = [UIImage imageNamed:

我已经对UIView进行了子类化,并实现了drawRect。我基本上希望在设置CGPoint之后调用这个drawRect,CGPoint是这个UIView的一个属性。我该怎么做?这是我的密码:

-(id) initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder]) {
        point_.x = 0;
        self.page_ = [UIImage imageNamed:@"pages"];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    if (point_.x != 0){
        [self.page_ drawInRect:rect];
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineWidth(context, 1.0f);
        CGContextMoveToPoint(context, 40, point_.y); 
        CGContextAddLineToPoint(context, 420, point_.y);
        CGContextStrokePath(context);
    }
}
当我设置此UIView的点并在另一个UIViewController中调用drawRect时,如下所示:

 self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
    [self.page_ drawRect:self.page_.frame];
它给了我所有这些错误:

  <Error>: CGContextSaveGState: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetBlendMode: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetAlpha: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextTranslateCTM: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextScaleCTM: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextDrawImage: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextRestoreGState: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetLineWidth: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextMoveToPoint: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextAddLineToPoint: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextDrawPath: invalid context 0x0
:CGContextSaveGState:无效的上下文0x0
二月22日20:24:38:CGContextSetBlendMode:无效上下文0x0
二月22日20:24:38:CGContextSetAlpha:无效上下文0x0
二月22日20:24:38:CGContextTranslateCm:无效上下文0x0
二月22日20:24:38:CGContextScaleCTM:无效上下文0x0
二月22日20:24:38:CGContextDrawImage:无效上下文0x0
二月22日20:24:38:CGContextRestoreGState:无效上下文0x0
二月22日20:24:38:CGContextSetStrokeColorWithColor:无效上下文0x0
二月22日20:24:38:CGContextSetLineWidth:无效上下文0x0
二月22日20:24:38:CGContextMoveToPoint:无效上下文0x0
二月22日20:24:38:CGContextAddLineToPoint:无效上下文0x0
二月22日20:24:38:CGContextDrawPath:无效上下文0x0
这是为什么?

您永远不应该直接调用
-drawRect:

改用
-setNeedsDisplay

self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
[self.page_ setNeedsDisplay];
如果需要自定义绘图区域(即将传递给
-drawRect:
CGRect
),可以使用
-setNeedsDisplayInRect:

如前所述,您可以覆盖
属性的setter:

- (void)setPoint_:(CGPoint)point {
   if(!CGPointEqualToPoint(point_, point)) {
      point_ = point;
      [self setNeedsDisplay];
   }
}
您永远不应该直接调用
-drawRect:

改用
-setNeedsDisplay

self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
[self.page_ setNeedsDisplay];
如果需要自定义绘图区域(即将传递给
-drawRect:
CGRect
),可以使用
-setNeedsDisplayInRect:

如前所述,您可以覆盖
属性的setter:

- (void)setPoint_:(CGPoint)point {
   if(!CGPointEqualToPoint(point_, point)) {
      point_ = point;
      [self setNeedsDisplay];
   }
}

将图形代码包装到
UIGraphicsPushContext(context)

UIGraphicsPopContext()
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
    [self setMultipleTouchEnabled:NO]; // (2)
    [self setBackgroundColor:[UIColor whiteColor]];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:2.0]; 
}
return self;
}


- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(context, kCGPathFillStroke);    //CGContextSetRGBFillColor doesn't work either
CGContextBeginPath(context);
CGContextMoveToPoint(context, 100.0, 60.0);
CGRect rectangle = {100.0, 60.0, 120.0, 120.0};
CGContextAddRect(context, rectangle);

CGContextStrokePath(context);
CGContextFillPath(context);
}
this code implement in create new uiview and add this code i hope that code you usefull.
UIGraphicsPushContext(context)

UIGraphicsPopContext()
以避免此错误消息。

如果
point
的值实际已更改,则最好调用
point
属性设置程序
setNeedsDisplay
。啊哈..这是个好主意。。因此,在我的setter中,我将调用setNeedsDisplay。介意解释一下为什么这样更好吗?如果
的值实际发生了更改,那么最好进行
属性设置程序调用
设置需要显示。啊哈..这是个好主意。。因此,在我的setter中,我将调用setNeedsDisplay。介意解释一下为什么这样更好吗?
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
    [self setMultipleTouchEnabled:NO]; // (2)
    [self setBackgroundColor:[UIColor whiteColor]];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:2.0]; 
}
return self;
}


- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(context, kCGPathFillStroke);    //CGContextSetRGBFillColor doesn't work either
CGContextBeginPath(context);
CGContextMoveToPoint(context, 100.0, 60.0);
CGRect rectangle = {100.0, 60.0, 120.0, 120.0};
CGContextAddRect(context, rectangle);

CGContextStrokePath(context);
CGContextFillPath(context);
}
this code implement in create new uiview and add this code i hope that code you usefull.