use*_*086 2 iphone sliding touchesmoved
我有两个按钮(按钮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];
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
我还有一个问题.现在我有两个UIImageVIews在一起.两个白色钢琴键上的黑色钢琴键.如果我按下黑键,它也会从它下面的白键发出音符.
我该如何防止这种情况?
我认为更简单的解决方案是创建一个截取所有触摸并决定做什么的超级视图.然后在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
Run Code Online (Sandbox Code Playgroud)