- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Setup up our key images.
UIColor * whiteColorUp = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha:1.0];
UIColor * whiteColorDown = [UIColor colorWithRed: 0.8 green: 0.8 blue: 0.8 alpha:1.0];
UIColor * blackColorUp = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha:1.0];
UIColor * blackColorDown = [UIColor colorWithRed: 0.6 green: 0.6 blue: 0.6 alpha:1.0];
CGSize whiteKeySize = CGSizeMake(44.0, 100.0);
UIImage * whiteKeyCUp = [ViewController imageColored: whiteColorUp size: whiteKeySize];
UIImage * whiteKeyCDown = [ViewController imageColored: whiteColorDown size: whiteKeySize];
CGSize blackKeySize = CGSizeMake(44.0, 50.0);
UIImage * blackKeyCUp = [ViewController imageColored: blackColorUp size: blackKeySize];
UIImage * blackKeyCDown = [ViewController imageColored: blackColorDown size: blackKeySize];
// Setup individual keys with pressed and unpressed images.
UIImageView * whiteKeyC = [[UIImageView alloc] initWithImage: whiteKeyCUp highlightedImage: whiteKeyCDown];
UIImageView * whiteKeyF = [[UIImageView alloc] initWithImage: whiteKeyCUp highlightedImage: whiteKeyCDown];
UIImageView * blackKeyC = [[UIImageView alloc] initWithImage: blackKeyCUp highlightedImage: blackKeyCDown];
// White key 1. Will associate with the blue button.
CGRect frame = whiteKeyC.frame;
frame.origin.x = 50.0; frame.origin.y = 100.0;
whiteKeyC.frame = frame;
[self.view addSubview: whiteKeyC];
// White key 2. Will NOT associate with the blue button.
frame = whiteKeyF.frame;
frame.origin.x = whiteKeyC.frame.origin.x + whiteKeyC.frame.size.width + 1.0; frame.origin.y = 100.0;
whiteKeyF.frame = frame;
[self.view addSubview: whiteKeyF];
// Black key that we will associate with the blue button.
frame = blackKeyC.frame;
frame.origin.x = 50.0 + 25.0; frame.origin.y = 100.0;
blackKeyC.frame = frame;
[self.view addSubview: blackKeyC];
// Out blue button.
buttonC = [UIButton buttonWithType: UIButtonTypeCustom];
buttonC.frame = CGRectMake(50.0, 65.0, 75.0, 33.0);
[buttonC setTitle: @"C" forState: UIControlStateNormal];
buttonC.layer.borderColor = [[UIColor blueColor] CGColor];
buttonC.layer.borderWidth = 1.0;
buttonC.layer.cornerRadius = 5.0;
// Set the action for the blue button.
[buttonC addTarget: self action: @selector(toggleKeys:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: buttonC];
// Piano keys I want to associate with a button.
NSArray * cKeys = @[whiteKeyC,blackKeyC];
// Using the button title as a key a dictionary key to associate keys to it.
buttonsToKeysDict = [[NSDictionary alloc] initWithObjectsAndKeys:cKeys, buttonC.titleLabel.text, nil];
}
// This is simplistic code. You'd have to alter other piano keys to reset them when selecting a new set.
- (void) toggleKeys: (id) sender
{
NSString * buttonTitleKey = [(UIButton*)sender titleLabel].text;
NSArray * theKeys = [buttonsToKeysDict valueForKey: buttonTitleKey];
for (UIImageView * iv in theKeys) {
iv.highlighted = (iv.highlighted == YES) ? NO : YES;
}
}
// Hack to create some UIImages for testing.
+ (UIImage *)imageColored:(UIColor *)color size:(CGSize)size {
if (CGSizeEqualToSize(size, CGSizeZero)) return nil;
if (!color) return nil;
UIImage *newImage = nil;
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}