0

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?

Mahir
  • 1,684
  • 5
  • 31
  • 59

1 Answers1

2

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?

csano
  • 13,266
  • 2
  • 28
  • 45
  • The image shows up w/ a UIImageView, but I can't override the touches began method like I could with a UIView – Mahir Aug 15 '11 at 02:28
  • @Mahir E -- take a look at the accepted answer for the following SO question: http://stackoverflow.com/questions/855095/how-can-i-detect-the-touch-event-of-an-uiimageview – csano Aug 15 '11 at 03:01
  • Yeah, i've added [self.userInteractionEnabled:YES]; [self.canBecomeFirstResponder:YES]; – Mahir Aug 15 '11 at 03:08