求助在UIImageView上添加UIView调用drawrect方法没有生效

iOS 码拜 9年前 (2015-05-11) 1209次浏览 0个评论

我最近在做一个ios项目,遇到一个问题就是需要在一个地图上画出路线。地图图片用UIImage存在一个UIImageView上面,我是想在UIImageView上添加一个UIView,需要将UIView大小和UIImage大小保持一样, 然后在UIView上调用drawrect方法画线    这么做了没有生效  求助。
ViewController里添加一个NAMapView实例。NAMapView继承自一个ScrollView

@property (weak, nonatomic) IBOutlet NAMapView *mapView;
[self.view addSubview:self.mapView];
[self.mapView setNeedsDisplay];

在NAMapView中,我们有一个UIImageView存储地图图片,ShowRouteView是一个UIView,用来调用drawrect方法画路线

@property (nonatomic,strong) ShowRouteView *showrouteview;
_showrouteview = [[UIView alloc] initWithFrame:CGRectZero];
[self addSubview:self.showrouteview];
- (void)drawRect:(CGRect)rect{
    NSLog(@"mapview");
    [self.showrouteview setNeedsDisplay];
}

在ShowRouteView类中有drawrect方法如下

- (void)drawRect:(CGRect)rect {
    NSLog(@"routeview");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapSquare);
    CGContextSetLineWidth(context, 1.0);
     CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 0, 150);
    CGContextAddLineToPoint(context, 50, 180);
    CGContextStrokePath(context);
}

最终console里显示,UIView里的drawrect方法没有被调用。求助~

100分
frame 是不是 CGRectZero ?如果是 Zero,是不会调用的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求助在UIImageView上添加UIView调用drawrect方法没有生效
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!