2010-02-07 49 views
2

我有两个按钮(按钮1和按钮2),如果我按下按钮1,注1开始,如果我按下按钮2,注意2点开始。幻灯片不抬起手指

所以我尝试做的是:按一个按钮(注1开始),并滑动到BUTTON2比应注2开始。如您所说,您滑动时不会将手指放在钢琴键盘上,并且所有音符都会从C到H发出声音。

我这样做有UIImageViews和我做了也UIButtons

如果我按c1pianoview它听起来“C”。如果我按下d1pianoview这听起来是“d”。

但是,如果我滑动没有举起我的手指从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彼此。两个白色钢琴键上的黑色钢琴键。如果我按下黑键,它也会从它下面的白键发出音符。

我该如何预防?

回答

0

不要你所需要的:

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

好的,我用touchesMoved做了它..但是现在每次我在我的ImageView中滑动音符声音。但我希望只有一次音符响起。我错了什么? thx求助! – user268086 2010-02-07 12:51:46

+0

@franhu - Dimitris打我吧 – willcodejavaforfood 2010-02-07 13:06:52

0

你或许可以使用的UIButton的“触地”和“触摸拖动内部”事件实现这一点很容易。

如果您想使用-touchesMoved:方法,您可以使用变量来跟踪触发声音的最后一个按钮,并且只在当前按钮不是最后播放声音的那个按钮时播放声音。

的详细信息:

中声明你的头一个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 ...你能帮我touchys移动..极其可爱吗? thx – user268086 2010-02-07 13:22:47

+0

我已编辑我的帖子。让我知道如果这有助于:) – Dimitris 2010-02-07 16:45:42

+0

对不起,但它仍然无法正常工作。如果我移动到c1pianoview或d1pianoview中,它会通过每一个小动作发出声音。所以,如果我从c1pianoview滑到d1pianoview,那么听起来有10个c-notes和10个d-notes。 =)但我只想听到一个c-note和一个d-note!知道为什么我不工作? thx帮助! greez – user268086 2010-02-07 19:48:42

1

[合并进一步的问题进入原帖]

5

我认为一个简单的解决方案是创建一个上海华拦截所有触摸并决定要做什么。然后在IB中,您将您的视图设为PianoView的一个实例,并创建所有关键字子视图。每个琴键都有一个标签,用于识别它是哪个琴键,所以PianoView知道要弹奏什么音符。 PianoView有一个currentView属性,它跟踪touchesMoved中的最后一个视图,因此它不会继续尝试播放相同的键。我有一个具有类似但不同要求的键盘,这种方法效果很好。下面

的样本代码在拦截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 2011-12-15 02:52:47

+0

发现了一个导致'touchesMoved'内部崩溃的错误。第二个'[(UIControl *)oldKey setHighlighted:YES];'应该是'[(UIControl *)view setHighlighted:YES];'。除此之外,这似乎确实起作用。 – Jonny 2011-12-15 04:56:16

+0

感谢您的修复,更新了我的答案。 – 2011-12-15 07:40:20