【问题标题】:Blinking animation issue闪烁动画问题
【发布时间】:2023-03-29 17:24:01
【问题描述】:

我的视图控制器中的动画有问题。我有两个UIViews:蓝色视图和绿色视图。蓝色视图位于绿色视图之上。在绿色视图中,我有一个UIView,它是一个动画正弦波,另一个UIView 称为“recordingIndicatorView”,它由一个实现了闪烁动画的文本标签组成。当我向上滑动蓝色视图时,会显示绿色视图,正弦波视图开始动画,recordingIndicatorView 开始闪烁。当我向下滑动蓝色视图时,动画结束,正弦视图和recordingIndicatorView 被隐藏。

但是,如果我向上滑动蓝色视图,向下滑动,然后快速向上滑动蓝色视图,闪烁动画会被丢弃并开始更快地闪烁。

- (IBAction)handleUpSwipe:(UISwipeGestureRecognizer *)recognizer
{

    if(viewIsUp == NO)
    {        

         [self.sineWave animateWave];

         [self showSine];

         [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

             [self moveViewsUp];

         } completion:^(BOOL finished){

         }];

         stopBlinking = NO;

        [self.recordingIndicatorView setHidden:NO];
        [self flashOn:self.recordingIndicatorView];

     }
 }

- (IBAction)handleDownSwipe:(UISwipeGestureRecognizer *)recognizer
{

    if(viewIsUp == YES)
    {

         [self stopRecording];

         if(self.panedView.frame.origin.y <0)
         {

             [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

                [self hideSine];
                [self moveViewsDown];

         } completion:^(BOOL finished){

        [self.sineWave.layer removeAllAnimations];

        }];

        }
    }
}

-(void) stopRecording
{
     stopBlinking = YES;
     [self hideSine];
     [self.recordingIndicatorView setHidden:YES];
}


- (void)flashOff:(UIView *)v
{
     [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
        self.recordingIndicatorView.alpha = 0.01;  
         } completion:^(BOOL finished) {

        if(stopBlinking == NO)
        {
             [self flashOn:self.recordingIndicatorView];
        }

    }];
}


- (void)flashOn:(UIView *)v
{
    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
         self.recordingIndicatorView.alpha = 1;
     } completion:^(BOOL finished) {
         [self flashOff:self.recordingIndicatorView];
    }];
}

-(void) moveViewsDown
{
    if(viewIsUp)
    {
    self.panedView.frame = CGRectOffset( self.panedView.frame, 0, 274);
    self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, 246);
    self.micImageView.frame = CGRectOffset(self.micImageView.frame, 0, 85.5);
    viewIsUp = NO;

    }
}

 -(void) moveViewsUp
{
    if(!viewIsUp)
    {
    self.panedView.frame = CGRectOffset(self.panedView.frame, 0, -274);
    self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, -246);
    self.micImageView.frame = CGRectOffset(self.micImageView.frame, 0, -85.5);
    viewIsUp = YES;
    }
}

【问题讨论】:

    标签: ios uiview uianimation


    【解决方案1】:

    我认为你的闪烁动画被添加了不止一次。 viewIsUp 标志可能有些混乱。这是我在没有其他代码的情况下所能找到的。并且尽量不要在块中使用self,这是一个坏习惯,会导致隐藏错误和内存泄漏,请改用weak self,例如:

    __weak typeof(self) wSelf = self;
    [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        [wSelf hideSine];
        [wSelf moveViewsDown];
    } completion:^(BOOL finished){
        [wSelf.sineWave.layer removeAllAnimations];
    }];
    

    【讨论】:

    • 我从未听说过使用 self 和 blocks 的问题。感谢您的提醒。当您说动画被多次添加时,您认为当我向上滑动时,“flashOn”会被多次调用吗?
    • 是的,我认为如果 viewIsUp 标志没有正确更新是可能的。
    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2011-11-25
    • 2011-12-02
    • 2012-03-12
    • 2011-03-30
    相关资源
    最近更新 更多