无需抬起手指即可从一个按钮滑动到另一个按钮

时间:2010-02-07 09:57:11

标签: iphone touchesmoved sliding

我有两个按钮(按钮1和按钮2),如果我按下按钮1,则note1开始,如果我按下按钮2,则注2开始。

所以我尝试做的是:按下按钮1(note1启动)和SLIDE到按钮2,然后按下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。两个白色钢琴键上的黑色钢琴键。如果我按下黑键,它也会从它下面的白键发出声音。

如何防止这种情况?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

[将更多问题合并到原帖]

答案 2 :(得分:0)

你不需要:

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

答案 3 :(得分:0)

通过使用UIButton的“Touch Down”和“Touch Drag Inside”事件,您可以轻松实现这一目标。

如果您想使用-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];
}
相关问题