2

2 つのボタン (ボタン 1 とボタン 2) があり、ボタン 1 を押すとノート 1 が開始し、ボタン 2 を押すとノート 2 が開始します。

だから私がやろうとしているのは、ボタン1を押して(note1が開始する)、ボタン2にSLIDEすると、note2が開始するはずです。のように、ピアノの鍵盤の上で指を離さずにスライドすると、c から h までのすべての音が鳴ります。

私はこれを でUIImageViews行い、また で行いましUIButtonsた。

を押すc1pianoviewと「c」と鳴ります。を押すとd1pianoview「ド」と鳴ります。

しかし、指を離さずにスライドするc1pianoviewd1pianoview、「c」としか聞こえません。私は何を間違えたのですか?でこれを行うこともできますUIButtonsか?

タッチダウンはできますが、スライドは機能しません。

誰か助けてくれませんか?ここに私のコード:

-(void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(c1pianoview.frame, location))
    { 
      NSString *path = [[NSBundle mainBundle]
      pathForResource: @"c1piano" ofType:@"mp3"];
      AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] 
          initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
      [theAudio play];
    }

    if(CGRectContainsPoint(d1pianoview.frame, location))
    { 
      NSString *path = [[NSBundle mainBundle]
      pathForResource: @"d1piano" ofType:@"mp3"];
      AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] 
          initWithContentsOfURL: [NSURL fileURLWithPathath] error:NULL];
      [theAudio play];
    }
}

アップデート:

さらに質問があります。今、私はUIImageVIewsお互いに2つ持っています。2 つの白いピアノ キーの上に黒いピアノ キー。黒鍵を押すと、その下の白鍵の音も鳴ります。

これを防ぐにはどうすればよいですか?

4

4 に答える 4

5

より簡単な解決策は、すべてのタッチを傍受して何をすべきかを決定するスーパービューを作成することだと思います。次に IB で、ビューを PianoView のインスタンスにし、すべてのキー サブビューを作成します。各キーには、それがどのキーであるかを識別するタグがあるため、PianoView は再生するノートを認識します。PianoView には、 touchesMoved の最後のビューを追跡する currentView プロパティがあるため、同じキーを再生しようとしません。要件は似ているが異なるキーボードがあり、このアプローチはうまく機能します。

以下のサンプル コードは、 でタッチをインターセプトしhitTest:withEvent:ます。次に touches* メソッドで、UIView のデフォルトの実装を使用して、hitTest:withEvent:どのキーが再生されているかを判断します。マルチタッチと同時にキーを演奏することをサポートしたい場合は、少し変更する必要があります。

@implementation PianoView

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // intercept touches
    if ([self pointInside:point withEvent:event]) {
        return self;        
    }
    return nil;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view = [super hitTest: [[touches anyObject] locationInView: self] withEvent: nil];
    // highlight subview under touch
    if (view != nil && view != self) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIControl *)view setHighlighted:YES];
        }
        [self setCurrentView:view];
        [self playKey:view];
    }
}

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    // un-highlight everything
    for (UIView *subview in [self subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [(UIControl *)subview setHighlighted:NO];
        }
    }
    [self stopPlaying];
    [self setCurrentView:nil];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view = [super hitTest: [[touches anyObject] locationInView: self] withEvent: nil];
    if (view != [self currentView]) {
        UIView *oldKey = [self currentView];
        // un-highlight
        if ([oldKey isKindOfClass:[UIButton class]]) {
            [(UIControl *)oldKey setHighlighted:NO];
        }    
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIControl *)view setHighlighted:YES];
        }    
        [self stopPlaying];
        [self playKey:view];
        [self setCurrentView:view];
    }
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view = [super hitTest: [[touches anyObject] locationInView: self] withEvent: nil];
    for (UIView *subview in [self subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [(UIControl *)subview setHighlighted:NO];
        }
    }
    [self stopPlaying];
    [self setCurrentView:nil];
}

@end
于 2011-04-07T19:45:24.107 に答える
1

[追加の質問を元の投稿にマージ]

于 2010-02-07T22:15:36.310 に答える
0

あなたは必要ありませんか:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
于 2010-02-07T12:25:10.417 に答える
0

UIButton の "Touch Down" および "Touch Drag Inside" イベントを使用すると、おそらくこれを簡単に実現できます。

この-touchesMoved:アプローチを使用する場合は、変数を使用してサウンドをトリガーした最後のボタンを追跡し、現在のボタンが最後にサウンドを再生したボタンでない場合にのみサウンドを再生できます。

さらに詳細に:

UIView *lastButton;ヘッダーでa を宣言してから、次のようにします。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(c1pianoview.frame, location) && lastButton != c1pianoview) {
    //play c1
    lastButton = c1pianoview;
}
else if(CGRectContainsPoint(d1pianoview.frame, location) && lastButton != d1pianoview) {
    //play d1
    lastButton = d1pianoview;
}
}

- (void)touchedEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    lastButton = nil;
}

- (void)touchedCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}
于 2010-02-07T13:04:24.850 に答える