【发布时间】:2016-08-23 15:10:47
【问题描述】:
我正在制作自定义 UIControl,我希望控件的一部分包含图像。目前,该控件能够从服务器下载图像,我目前在drawRect中这样做没有效果:
NSURL *url = [NSURL URLWithString:self.imageData];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = image;
我相信这是因为我的控件没有将 imageView 添加为子视图,但我不知道该怎么做,因为这是 UIControl 而不是 UIView。
类如下所示:
@interface RDWebButton : UIControl
/** The color of the button*/
@property (nonatomic) IBInspectable UIColor *buttonColor;
#if TARGET_INTERFACE_BUILDER
@property (nonatomic) IBInspectable NSInteger buttonShape;
#else
@property (nonatomic) RDShape buttonShape;
#endif
@end
- (void)drawRect:(CGRect)rect {
// do drawing
NSLog(@"drawRect...");
self.backgroundColor = [UIColor clearColor];
[self.buttonColor setFill];
switch (self.buttonShape) {
case RDShapeSquare: {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
[path fill];
break;
}
case RDShapeRoundedRect: {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10];
[path fill];
break;
}
case RDShapeCircle: {
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
[path fill];
break;
}
}
NSURL *url = [NSURL URLWithString:self.imageData];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = image;
}
我现在只想让我的图像出现在绘制的形状上方。我将超类设置为 UIControl 而不是 UIButton 因为我可能想要更改它以让用户拖动项目以显示菜单或其他内容,但我很确定 UIControl 应该能够完成 UIButton 可以做的所有事情以及更多。我的挑战只是让图像加载到绘制的形状上。
【问题讨论】:
-
它是什么的子类?按钮?你能带我们看看全班吗?
-
添加了一些代码来澄清
标签: ios uiimageview uicontrol