2010-02-07 3 views
2

두 개의 버튼 (버튼 1과 버튼 2)이 있으며 버튼 1을 누르면 노트 1이 시작되고 버튼 2를 누르면 노트 2가 시작됩니다.손가락을 짚지 않고 하나의 버튼에서 다른 것으로 슬라이드

그래서 무엇을하려고합니까 : 버튼 1 (note1이 시작)을 누르고 슬라이드 2는 button2로 이동해야하며 note2가 시작되어야합니다. 마찬가지로, 피아노 건반 위로 손가락을 올리거나 c에서 h까지 모든 음표를 들으면서 움직입니다.

나는 이것을 UIImageViews와 함께했고, 나는 UIButtons도 사용했다.

c1pianoview을 누르면 "c"로 들립니다. d1pianoview을 누르면 "d"소리가납니다.

그러나 손가락을 누르지 않고 슬라이드하면 c1pianoview에서 d1pianoview으로 "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을 서로 가지고 있습니다. 두 개의 흰색 피아노 키를 통해 검은 피아노 키. 검정색 키를 누르면 흰색 키의 음을 들려줍니다.

어떻게 방지합니까?

답변

0

당신은 필요가 없습니다

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
+0

괜찮아요. touchesMoved와 함께했습니다.하지만 지금은 ImageView에서 슬라이드 할 때마다 음이 들립니다. 하지만 한 번만 음표가 들리길 바래. 내가 뭘 잘못 했니? 도움을위한 thx! – user268086

+0

@franhu - Dimitris가 나를 이길 때 – willcodejavaforfood

0

당신은 아마있는 UIButton의 "터치 다운"과 "터치 드래그 내부"이벤트를 사용하여 쉽게이 작업을 수행 할 수 있습니다.

접근 방식을 사용하려는 경우 변수를 사용하여 사운드를 트리거 한 마지막 버튼을 추적하고 현재 버튼이 마지막으로 사운드를 재생 한 버튼이 아닌 경우에만 사운드를 재생할 수 있습니다.

구체적으로 :

는 다음 헤더에 UIView *lastButton;을 선언하고 :

-(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]; 
} 
+0

좋아, TouchDragInside와 함께 작동하지 않는다 ... 터치로 나를 도울 수 있습니까? thx – user268086

+0

내 게시물을 편집했습니다. 그게 도움이되는지 알려주세요. :) – Dimitris

+0

미안하지만 아직도 작동하지 않습니다. 내가 c1pianoview 또는 d1pianoview 내부로 이동하면 모든 작은 움직임으로 음이 들립니다. 따라서 c1pianoview에서 d1pianoview로 슬라이드하면 약 10 개의 c 음표와 10 개의 d 음표가납니다. =)하지만 나는 단지 하나의 c-note와 하나의 d-note 만 듣고 싶다! 내가 왜 일하지 않는지 알아? 도와 주셔서 감사합니다! greez – user268086

1

[원래의 게시물에 병합 추가 질문]

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 
+0

좋은 답변입니다. 아직 테스트하지는 않았지만 제대로 된 것 같습니다. Stackoverflow에 대한 다른 답변이없는 것 같습니다. – Jonny

+0

'touchesMoved'에서 충돌을 일으키는 버그를 발견했습니다. 두 번째'[(UIControl *) oldKey setHighlighted : YES];'는 ([UIControl *) view setHighlighted : YES];'이어야합니다. 그것 이외에, 이것은 실제로 작동하는 것처럼 보입니다. – Jonny

+0

수정 해 주셔서 감사합니다. 내 대답이 업데이트되었습니다. –

관련 문제