【发布时间】:2014-01-08 03:05:13
【问题描述】:
我正在创建一个钢琴应用程序(用于 OSX),它有一个屏幕键盘,可以显示用户在他们的合成器键盘上演奏的内容。一切都使用 CoreMIDI 框架连接。我通过在一个名为PianoKey 的类中继承NSButton 创建了一些自定义按钮。该类可以在下面看到:
#import "PianoKey.h"
@implementation PianoKey
//updates the core animation layer whenever it needs to be changed
- (BOOL)wantsUpdateLayer {
return YES;
}
- (void)updateLayer {
//tells you whether or not the button is pressed
if ([self.cell isHighlighted]) {
NSLog(@"BUTTON PRESSED");
self.layer.contents = [NSImage imageNamed:@"buttonpressed.png"];
}
else {
NSLog(@"BUTTON NOT PRESSED");
self.layer.contents = [NSImage imageNamed:@"button.png"];
}
}
@end
“钢琴键”是以编程方式创建的。未按下时,钢琴键为蓝色,按下时为粉红色(只是临时颜色)。如果我单击屏幕上的按钮,颜色开关可以正常工作,但是当我尝试通过我的 MIDI 键盘弹奏时它们不会改变。
注意事项: 1) MIDI 键盘工作 2) 我成功地从键盘获取 MIDI 数据。
a) 这个 MIDI 数据被传递给一个名为 midiInputCallback 的回调函数,如下所示: [来自 AppController.m 类]
void midiInputCallback(const MIDIPacketList *list, void *procRef, void *srcRef) {
PianoKey *button = (__bridge PianoKey*)procRef;
UInt16 nBytes;
const MIDIPacket *packet = &list->packet[0]; //gets first packet in list
for(unsigned int i = 0; i < list->numPackets; i++) {
nBytes = packet->length; //number of bytes in a packet
handleMIDIStatus(packet, button);
packet = MIDIPacketNext(packet);
}
}
对象 button 是对我以编程方式创建的按钮的引用,在 AppController.h 中定义为:
@property PianoKey *button; //yes, this is synthesized in AppController.m
b) 回调函数调用一堆处理 MIDI 数据的函数。如果检测到正在播放一个音符,这就是我将自定义按钮设置为突出显示的地方...这可以在下面的函数中看到:
void updateKeyboardButtonAfterKeyPressed(PianoKey *button, int key, bool keyOn) {
if(keyOn)
[button highlight:YES];
else
[button highlight:NO];
[button updateLayer];
}
[button updateLayer] 从 PianoKey 调用我的 updateLayer() 方法,但它不会更改图像。有什么我想念的吗?似乎视图或窗口没有被更新,即使我的按钮说它是“突出显示的”。请帮忙!在此先感谢:)
【问题讨论】: