所以我打算制作一个几乎与底部的锚点成一条线的视图,从右到左摆动并在达到最大角度时播放声音。(节拍器臂的实现)
我的方法是:
-(void)goForward :(UIView*)view{
CGAffineTransform rightWobble = CGAffineTransformMakeRotation(RADIANS(120));
[UIView animateWithDuration:duration animations:^{
view.transform=rightWobble;
} completion:^(BOOL finished) {
NSLog(@"go back duration : %f",duration);
if (isWobbling) {
[self goBack:view];
[self performSelector:@selector(playMetronomeSound) withObject:nil afterDelay:duration];
}
else if (!isWobbling){
[self stopWobbling];
[self performSelector:@selector(stopMetronomeSound) withObject:nil afterDelay:0.0];
}
}]; }
和
-(void)goBack :(UIView*)view{
CGAffineTransform leftWobble = CGAffineTransformMakeRotation(RADIANS(60));
[UIView animateWithDuration:duration animations:^{
view.transform = leftWobble;
} completion:^(BOOL finished) {
NSLog(@"go forward duration: %f",duration);
if (isWobbling) {
[self goForward:view];
[self performSelector:@selector(playMetronomeSound) withObject:nil afterDelay:duration];
}
else if (!isWobbling){
[self stopWobbling];
[self performSelector:@selector(stopMetronomeSound) withObject:nil afterDelay:0.0];
}]; }
和
-(void) stopWobbling{
[UIView animateWithDuration:0.1 animations:^{
metronomeSlider.transform = vertical;
[self stopMetronomeSound];
}]; }
和
-(void) playMetronomeSound{
alSourcePlay(mySoundSource);
}
-(void) stopMetronomeSound{
alSourceStop(mySoundSource);
}
持续时间变量确定动画的持续时间。当我点击一个看起来像这样的播放按钮时,动画就会发生:
-(void)playButtonAction {
if (_metronomeIsAnimatingAndPLaying == NO)
{
[self goForward:metronomeSlider];
[_playButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
[self performSelector:@selector(playMetronomeSound) withObject:nil afterDelay:duration];
_metronomeIsAnimatingAndPLaying = YES;
isWobbling = YES;
NSLog(@"DURATION IS : %f",duration);
}
else if (_metronomeIsAnimatingAndPLaying == YES)
{
[_playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
[self stopWobbling];
_metronomeIsAnimatingAndPLaying = NO;
isWobbling = NO;
} }
我的问题是每当我点击播放/停止按钮使动画停止并且我的视图返回到 90 度角时,它会发生,但它会播放一个额外的滴答声,这不是要播放的。
任何想法如何解决这一问题 ?
提前谢谢
更新截图: