有关创建iPhone Piano UI的问题

时间:2010-10-11 16:45:37

标签: iphone user-interface audio touchesmoved piano

我正在尝试创建一个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 个答案:

答案 0 :(得分:1)

UIButton出口并不意味着处理复杂的触摸交互,例如glissando等所需。

您可以尝试将键盘图形放在自定义UIView子类中,计算所有活动区域的命中矩形(白键的多个矩形),并使用视图子类中的UITouch处理程序跟踪所有触摸单独地,包括他们最初击中的矩形,移动到另一个,它们被释放的位置等等。