将图像添加到 UIView 时出现问题
我正在制作一个带有钢琴的应用程序,每个键都是它自己的子类。
我尝试将按键设为 UIButton 的子类,但在调用方法时遇到一些问题后,我在网上检查并发现其他人在子类化 UIButton 时也遇到问题。
然后我尝试对 UIView 进行子类化。问题是我尝试向其中添加图像,但它显示为黑框。
这是来自 CustomView 的代码
@implementation Keys
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"whitekey.gif"]];
}
return self;
(我已经确认问题不是whitekey.gif)
这是来自 viewController 的代码
- (void)viewDidLoad
{
[super viewDidLoad];
Keys *whiteKey = [[Keys alloc] initWithFrame:CGRectMake(0, 0, 100, 300)];
[self.view addSubview:whiteKey];
}
如果我子类化 UIImageView 会更好吗?
I'm making an app with a piano and each key will be it's own subclass.
I tried making the keys a subclass of UIButton, but after having some problems getting the methods to be called, I checked online and found out that others were also having problems subclassing UIButtons.
So then I tried subclassing a UIView. The problem was that I tried adding an image to it, but it showed up as a black box.
Here is the code from the CustomView
@implementation Keys
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"whitekey.gif"]];
}
return self;
(I've confirmed that the problem isn't whitekey.gif)
Here is the code from the viewController
- (void)viewDidLoad
{
[super viewDidLoad];
Keys *whiteKey = [[Keys alloc] initWithFrame:CGRectMake(0, 0, 100, 300)];
[self.view addSubview:whiteKey];
}
Is it better if I subclass UIImageView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 UIImageView。
http://developer.apple.com/ library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html
也许一个 UIView 作为容器,带有一个带有钢琴键图像的 UIImageView,还有附加的除此之外,UIViews 取决于您需要做什么?
You should be using UIImageView.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html
Maybe a UIView as a container, with a UIImageView with the piano key image, with additional UIViews on top of that, depending on what you need to do?