我有两个按钮(按钮 1 和按钮 2),如果我按按钮 1,则注释 1 启动,如果我按按钮 2,则注释 2 启动。
所以我尝试做的是:按下按钮一(note1 启动)并滑动到按钮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
。一个黑色钢琴键位于两个白色钢琴键之上。如果我按下黑键,它下面的白键也会发出音符。
如何防止这种情况发生?
最佳答案
我认为一个更简单的解决方案是创建一个 super View 来拦截所有触摸并决定要做什么。然后在 IB 中,将 View 设为 PianoView 的实例,并将所有键设为 subview 。每个键都有一个标签来标识它是哪个键,因此 PianoView 知道要弹奏哪个音符。 PianoView 有一个 currentView 属性,它跟踪 TouchMoved 中的最后一个 View ,因此它不会一直尝试弹奏相同的键。我有一个具有相似但不同要求的键盘,这种方法效果很好。
下面的示例代码拦截 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
关于iphone - 无需松开手指即可从一个按钮滑动到另一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216610/