2010-10-11 51 views
0

我正在嘗試創建iPhone鋼琴應用程序。 我在界面生成器中創建了14個不同的UIImageView,來表示鍵。隨着touchesBegan,touchesMoved,touchesEnded和touchesCancelled我試圖播放音頻文件。有關創建iPhone鋼琴UI的問題

所以一旦你移動你的手指(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 

(我將取代 「AudioServicesPlaySystemSound」 與OpenAL的或別的什麼)

現在我的問題:

如果您將手指移動到另一個鍵而沒有擡起,則會播放聲音,但該鍵保持高亮顯示。此外,如果您再次移回手指,則不會播放聲音。

另外,當你按兩個手指時,有一些問題,只要拿走一個。 然後再次播放第一個音的音調。

當您按下其中一個黑鍵時,也會聽到相鄰的白鍵之一。總的來說,我用這個方法來創建一個鋼琴UI很多問題,我不能單獨解決。

是否有另一種方式來編程整個應用程序? 或有任何提示我如何解決問題?

謝謝Jojoce。

PS:對不起英語,我是德語。

回答

1

的UIButton的出口並不意味着處理等所需的滑音複雜的觸摸交互等

你可以嘗試把你的鍵盤的圖形中的自定義UIView子類,計算命中矩形的所有活動區域(用於白鍵的多個矩形)以及在視圖子類中使用UITouch處理程序來單獨跟蹤所有觸摸,包括它們最初命中的命中矩形,移動到另一個,它們被釋放的位置等。

+0

感謝您的快速回復! 然後我如何突出顯示鍵? 你可以給我一個代碼示例,因爲我仍然是編程初學者?那會非常好。 謝謝 – Jojoce 2010-10-11 17:35:04