呼叫触摸在触摸后结束移动并抬起手指



根据用户按下的按钮,我正在尝试播放不同的动画和声音。按钮的形状各不相同,当用户按下按钮时,我需要播放声音,并在他抬起手指时停止按钮。我以为只要触摸一下就很容易了Began和触摸一下Moved。

但是,如果用户在触摸按钮时移动手指(即使是1像素的移动),则会调用touchesMoved方法。因此,我尝试了一些选项,一旦手指移动,我就可以停止声音(通过调用touchesEnded by self),但这不是完美的解决方案,因为用户移动手指时甚至没有注意到(比如1像素左右),然后在触摸按钮时很难连续播放声音。

所以我想我可以创建两个整数,我将在touchesBegan中设置一个整数的值,然后在touchesMoved中设置另一个整数,最后比较它们,检查移动是否在同一视图中(按钮)-如果不是,则调用touchesEnded。然而,它有一个问题,那就是如果用户将手指放在按钮上,然后移动它(仍然在同一个按钮上),然后抬起,则不会调用touchesEnded,因为他在同一视图中启动和移动。

在用户移动手指后抬起手指后,我应该如何调用touchesEnded方法?

这是我的代码(忽略那些阿尔法设置,播放声音等):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
leftArmBtn.alpha = 0;
leftLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"leftArmPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTARM"];
touchParent = 1;
} else if ([touch view] == mouthBtn) {
mouthBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"mouthPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"MOUTH"];
touchParent = 2;
} else if ([touch view] == rightArmBtn) {
rightArmBtn.alpha = 0;
righLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"rightArmPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTARM"];
touchParent = 3;
} else if ([touch view] == leftLegBtn) {
leftLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"leftLegPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTLEG"];
touchParent = 4;
} else if ([touch view] == righLegBtn) {
righLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"rightLegPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTLEG"];
touchParent = 5;
} else if ([touch view] == vakBtn) {
vakBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"vakPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"VAK"];
touchParent = 6;
} else {
touchParent = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
leftLegBtn.alpha = 1;
leftArmBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTARM"];
} else if ([touch view] == mouthBtn) {
mouthBtn.alpha = 1;
mainImageView.image = defaultImage;
} else if ([touch view] == rightArmBtn) {
rightArmBtn.alpha = 1;
righLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTARM"];
} else if ([touch view] == leftLegBtn) {
leftLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTLEG"];
} else if ([touch view] == righLegBtn) {
righLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTLEG"];
} else if ([touch view] == vakBtn) {
vakBtn.alpha = 1;
mainImageView.image = defaultImage;
} else {
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
movingTouch = 1;
} else if ([touch view] == mouthBtn) {
movingTouch = 2;
} else if ([touch view] == rightArmBtn) {
movingTouch = 3;
} else if ([touch view] == leftLegBtn) {
movingTouch = 4;
} else if ([touch view] == righLegBtn) {
movingTouch = 5;
} else if ([touch view] == vakBtn) {
movingTouch = 6;
} else {
movingTouch = 10;
}
if (touchParent != movingTouch) {
[self touchesEnded:touches withEvent:event];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}

好的,所以发现将touchesBegan、touchesMoved和touchesEnded添加到UIButton子类并不能按预期工作,所以我切换到为每个UIControl操作添加目标。

我使用了这里的代码当UIButton被触发时,你如何获得触摸Began坐标?

将其用于每个按钮,并对其进行了一点修改,如下所示:

UIButton *aButton = [UIButton ......
[aButton addTarget:self action:@selector(aButtonTouch) forControlEvents:UIControlEventTouchDown];
[aButton addTarget:self action:@selector(aButtonTouchEnd) forControleEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

然后

-(void)aButtonTouch{
//do something
}
-(void)aButtonTouchEnd {
//do something
}

最新更新