2

所以我打算制作一个几乎与底部的锚点成一条线的视图,从右到左摆动并在达到最大角度时播放声音。(节拍器臂的实现)

我的方法是:

-(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 度角时,它会发生,但它会播放一个额外的滴答声,这不是要播放的。

任何想法如何解决这一问题 ?

提前谢谢

更新截图:

在此处输入图像描述

4

1 回答 1

0

在我看来,这正在发生:

  1. 你叫 [self stopWobbling]
  2. stopWobbling 在变换上调用 animateWithDuration
  3. 这将导致调用正在运行的动画的完成块(完成 = NO)
  4. 在该块中,isWobbling 仍然为 true(因为您尚未将其设置为 false),因此您播放声音(并尝试反转方向)。

尝试这个:

-(void) stopWobbling {
    isWobbling = NO;
    [UIView animateWithDuration:0.1 animations:^{
        metronomeSlider.transform = vertical;
        [self stopMetronomeSound];
}]; }
于 2013-09-16T11:34:21.707 回答