创建iPhone钢琴用户界面的问题
我正在尝试创建一个iPhone钢琴应用程序。 我在Interface Builder中创建了14个不同的UIImageView来表示键。触摸开始、触摸移动、触摸结束和触摸取消,然后我尝试播放音频文件 因此,一旦你移动手指时播放声音(触摸移动),我就用NSMutableArray解决了这个问题 代码(我只复制了C键的代码,其他键的代码相同)如下所示: 我的.h文件:创建iPhone钢琴用户界面的问题,iphone,user-interface,audio,touchesmoved,piano,Iphone,User Interface,Audio,Touchesmoved,Piano,我正在尝试创建一个iPhone钢琴应用程序。 我在Interface Builder中创建了14个不同的UIImageView来表示键。触摸开始、触摸移动、触摸结束和触摸取消,然后我尝试播放音频文件 因此,一旦你移动手指时播放声音(触摸移动),我就用NSMutableArray解决了这个问题 代码(我只复制了C键的代码,其他键的代码相同)如下所示: 我的.h文件: @interface PianoViewController : UIViewController { IBOutlet U
@interface PianoViewController : UIViewController {
IBOutlet UIImageView *pianoButtonC;
IBOutlet UIImageView *pianoButtonCC;
IBOutlet UIImageView *pianoButtonD;
IBOutlet UIImageView *pianoButtonDb;
IBOutlet UIImageView *pianoButtonDbDb;
IBOutlet UIImageView *pianoButtonE;
IBOutlet UIImageView *pianoButtonEb;
IBOutlet UIImageView *pianoButtonF;
IBOutlet UIImageView *pianoButtonG;
IBOutlet UIImageView *pianoButtonGb;
IBOutlet UIImageView *pianoButtonH;
IBOutlet UIImageView *pianoButtonHb;
CGPoint location;
NSMutableArray *lastButtons;
UITouch *touch;
}
@end
我的.m文件:
#import "PianoViewController.h"
@implementation PianoViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self.view setMultipleTouchEnabled:YES];
lastButtons = [[NSMutableArray alloc] init];
[lastButtons insertObject:@"notPressedC" atIndex:0];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for(touch in [event allTouches]){
if(CGRectContainsPoint(pianoButtonC.frame, [touch locationInView:touch.view]) && [lastButtons containsObject:@"notPressedC"]) {
[pianoButtonC setHighlighted: YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pianoC"ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
[lastButtons replaceObjectAtIndex:0 withObject:@"pressedC"];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for(touch in [event allTouches]){
if(CGRectContainsPoint(pianoButtonC.frame, [touch locationInView:touch.view]) && [lastButtons containsObject:@"notPressedC"]) {
[pianoButtonC setHighlighted: YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pianoC"ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
[lastButtons replaceObjectAtIndex:0 withObject:@"pressedC"];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[lastButtons replaceObjectAtIndex:0 withObject:@"notPressedC"];
[pianoButtonC setHighlighted: NO];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight
);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[lastButtons release];
}
@end
(我将用OpenAL或其他东西替换“AudioServicesPlaySystemSound”)
现在我的问题是:
如果您将手指移动到另一个键,而不将其抬起,则会播放声音,但该键仍高亮显示。此外,如果你再次向后移动手指,则不会播放任何声音
此外,当你按下两个手指时,也会出现问题,只需将其中一个手指移开即可。
然后再次播放第一个键的音调
当您按下其中一个黑键时,您也会听到声音,同时也会听到相邻白键之一的声音
总的来说,我已经用这个方法创建了一个钢琴UI,但我无法单独解决很多问题
是否有其他方法来编程整个应用程序?
或者有没有人能给我一些解决问题的建议
谢谢你,乔乔
PS:对不起,我的英语不好,我是德国人。UIButton插座不适用于处理复杂的触摸交互,如glissando等
您可以尝试将键盘图形放入自定义UIView子类中,计算所有活动区域的点击矩形(白色键的多个矩形),并在view子类中使用UITouch处理程序单独跟踪所有触摸,包括它们最初点击的点击矩形,移动到另一个,发布地点等。感谢您的快速回复!那么我如何突出显示关键点呢?你可以给我一个代码示例,因为我还是编程的初学者?那太好了。谢谢