将图像添加到 UIView 时出现问题

发布于 2024-11-29 13:35:12 字数 750 浏览 1 评论 0原文

我正在制作一个带有钢琴的应用程序,每个键都是它自己的子类。

我尝试将按键设为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2024-12-06 13:35:12

您应该使用 UIImageView。

图像视图对象提供了一个基于视图的容器用于显示
单个图像或一系列图像的动画。

http://developer.apple.com/ library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

也许一个 UIView 作为容器,带有一个带有钢琴键图像的 UIImageView,还有附加的除此之外,UIViews 取决于您需要做什么?

You should be using UIImageView.

An image view object provides a view-based container for displaying
either a single image or for animating a series of images.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文