0

我正在尝试创建一个 iPhone 钢琴应用程序。我在 Interface Builder 中创建了 14 个不同的 UIImageViews 来表示键。使用 touchesBegan、touchesMoved、touchesEnded 和 touchesCancelled 然后我尝试播放音频文件。

因此,当您移动手指(touchesMoved)时播放声音后,我已经使用 NSMutableArray 解决了。

代码(我只复制了 C 键,其他键的代码相同)如下所示:

我的 .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 有很多我无法单独解决的问题。

是否有另一种方式来编写整个应用程序?或者有任何关于我如何解决问题的提示吗?

谢谢乔乔斯。

PS:对不起英语不好,我是德国人。

4

1 回答 1

1

UIButton 插座并不意味着处理复杂的触摸交互,例如滑音等所需的。

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

于 2010-10-11T16:53:15.450 回答