gpt4 book ai didi

ios - 动画 Objective-C

转载 作者:行者123 更新时间:2023-12-01 16:47:31 25 4
gpt4 key购买 nike

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

我的方法是:

-(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;
} }

我的问题是,每当我点击播放/停止按钮使动画停止并且我的 View 返回到 90 度角时,它就会发生,但它会播放一个不应该播放的额外滴答声。

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

提前谢谢

更新截图:

enter image description here

最佳答案

在我看来,这正在发生:

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

  • 试试这个:
    -(void) stopWobbling {
    isWobbling = NO;
    [UIView animateWithDuration:0.1 animations:^{
    metronomeSlider.transform = vertical;
    [self stopMetronomeSound];
    }]; }

    关于ios - 动画 Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826031/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com