【问题标题】:UIImageView subclass does not displayUIImageView 子类不显示
【发布时间】:2009-07-27 02:03:03
【问题描述】:

我正在使用 UIImageView 的自定义子类,但我不知道为什么它不显示。

The relevant code from my UIImageView subclass:
-(id) initWithImage:(UIImage*)image{
    if(self = [super initWithImage:image]){

    }
    return self;
}

并且从将显示我的子类的视图的视图控制器:

UIImage *zoomRatateSliderHorizontal = 
       [[UIImage imageNamed:@"ZoomRotate Slider Horizontal.png"]
      stretchableImageWithLeftCapWidth:(75.0)/2-1.0  topCapHeight:0]; 
scaleHorizontalControl = [[ViewTransformationController alloc] 
                     initWithImage:zoomRatateSliderHorizontal];
scaleHorizontalControl.onScreenFrame = CGRectMake(0.0, 5.0,  
                                   self.view.frame.size.width, 
                                   scaleHorizontalControl.image.size.height);
scaleHorizontalControl.frame = scaleHorizontalControl.onScreenFrame;
scaleHorizontalControl.offScreenFrame = CGRectZero;
[self.view addSubview:scaleHorizontalControl];

在我进行子类化之前,我在视图控制器中没有遇到以下问题:

UIImage *zoomRatateSliderVertical = 
                    [[UIImage imageNamed:@"ZoomRotate Slider Vertical.png"]
                    stretchableImageWithLeftCapWidth:0  topCapHeight:(75.0)/2-1.0]; 
scaleOrRatateViewVertical = [[UIImageView alloc] 
                             initWithImage:zoomRatateSliderVertical];
scaleOrRatateViewVertical.frame = CGRectMake(-zoomRatateSliderVertical.size.width,
                                  zoomRatateSliderHorizontal.size.height+5.0+5.0, 
                                  zoomRatateSliderVertical.size.width, 
                           465.0 - zoomRatateSliderHorizontal.size.height-10.0-5.0);
[self.view addSubview:scaleOrRatateViewVertical];

我使用断点检查了传递给我的班级的框架和图像,它们似乎都是有效的和理想的。

任何关于我做错了什么的建议我都非常感谢。

【问题讨论】:

    标签: iphone uiimageview subclassing


    【解决方案1】:

    Apple 文档如下:

    UIImageView 类经过优化,可以将其图像绘制到显示器上。 UIImageView 不会调用 drawRect: 一个子类。如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。

    所以我将 UIView 子类化并从那里开始工作。

    【讨论】:

    • 我对任何自定义绘图代码都不感兴趣。 drawRect: 在我的子类中是空的。
    • 如果您不进行自定义绘图,那么您的子类中根本不应该有 -drawRect:(除非您调用 super)。
    • 啊哈哈!谢谢杰布伦南。您的回答极大地阐明了 mjhoy。新手请注意:子类化时要非常小心覆盖哪些方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多