Problem adding image to UIView
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 viewContro开发者_如何学运维ller
- (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?
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?
精彩评论