有关创建iPhone Piano UI的问题
我正在尝试创建一个iPhone钢琴应用程序。
我在Interface Builder中创建了14个不同的UIImageViews来表示键。使用touchesBegan,touchesMoved,touchesEnded和touchesCancelled然后我尝试播放音频文件。
因此,一旦你移动手指播放声音(touchesMoved),我已经用NSMutableArray解决了。
代码(我只复制了C Key,其他键的代码是相同的)如下所示:
我的.h文件:
@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很多问题,我无法单独解决。
是否有其他方法可以对整个应用程序进行编程?
或者有任何关于我如何解决问题的提示?
谢谢Jojoce。
PS:抱歉英语不好,我是德国人。
没有找到相关结果
已邀请:
1 个回复
恋裂