UIViewanimation不适用于iOS 7

我正在一个示例项目中,我有一个垂直scrollview和一个水平scrollview 。 垂直scrollview视图有一些子视图。 在scrollviewDidScroll我正在执行一些操作。 除此之外,我现在想要在屏幕上看到特定的子视图时,为垂直滚动视图内的子视图设置animation。 为此,我正在做一个确定的计算。 animation如下:

子视图包含多个自定义视图。 我试图animation(减less,然后再次增加视图的alpha值)在某些特定的时间序列(以便animation看起来顺序)这些视图中的每一个。 为此我张贴通知的意见和animation的顺序和逻辑是完美的,根据我想要的。

但是我面临的问题是代码正在执行时,我把断点,但animation不显示。 如果我停止滚动后发布通知,则animation发生完全正常。 但我想要的是animation即使在滚动和视图在屏幕上时也会发生。

我正在添加我的代码片段如下:

SubView :(在我的scrollview

 - (void)animateSequenceTemplate { if (!hasSequenceTemplateAnimated) { [self performSelector:@selector(animateSequenceSlides) withObject:nil afterDelay:0]; hasSequenceTemplateAnimated = YES; } else { return; } } - (void)animateSequenceSlides { NSDictionary *userInfo = [NSDictionary dictionaryWithObject:imageAsset forKey:@"assetMO"]; [[NSNotificationCenter defaultCenter]postNotificationName:AnimateSequenceSlide object:nil userInfo:userInfo]; } 

上述子视图中的子视图:

 - (void)animateSlideView:(NSNotification *)notification { NSDictionary *userInfo = notification.userInfo; if ([userInfo objectForKey:@"assetMO"] == assetMO) { [[NSNotificationCenter defaultCenter]removeObserver:self name:AnimateSequenceSlide object:nil]; CGFloat duration = 0.035; float delay = (self.slideCount * duration); CGFloat timeDelay = 0; [self performSelector:@selector(animationPhase1) withObject:nil afterDelay:delay]; timeDelay = delay + duration; [self performSelector:@selector(animationPhase2) withObject:nil afterDelay:timeDelay]; if (self.slideCount == (self.maxSlideCount - 1)) { timeDelay += duration * 18; //No animation till 18 frames } else { timeDelay += duration; } [self performSelector:@selector(animationPhase3) withObject:nil afterDelay:timeDelay]; timeDelay += duration; [self performSelector:@selector(animationPhase4) withObject:nil afterDelay:timeDelay]; } } - (void)animationPhase1 { [UIView animateWithDuration:0.035 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^ { [self setAlpha:0.85]; }completion:^(BOOL finished) { }]; } 

里面出现了两个场景- (void)animateSequenceTemplate

  1. 我称之为[self animateSequenceSlides]; 。 在这种情况下,animation不显示。
  2. [self performSelector:@selector(animateSequenceSlides) withObject:nil afterDelay:0.0f]; 。 在这种情况下,animation显示,但在scrollviewrest。

我不得不使用执行select器使用UIViewanimation,因为如果我删除这个并使用嵌套的UIViewanimation块/或直接调用这些方法,然后再次animation不显示。 现在至less在我rest后滚动显示。

我想就解决scheme提出build议,或者猜测我可能犯的错误。

有可能,你不是在主队列上执行它。

dispatch_async(dispatch_get_main_queue, block())可能有帮助。 处理animation时,主队列是唯一执行代码的地方,这就使animation发生了。

编辑:

 [self performSelector:@selector(animationPhase1) withObject:nil afterDelay:delay]; 

我不确定,在哪里发生,但我不认为主线。 尝试这个:

 [self performSelectorOnMainThread:@selector(animationPhase1) withObject:nil waitUntilDone:YES]; 

我不确定延迟,所以我更喜欢GCDfunction和块。

最后,我得到了我的问题的解决scheme,并按预期工作非常好。

所作更改:

  1. 从“animateSequenceTemplate”直接调用“animateSequenceSlides”方法
  2. 在上面提到的“animateSlideView”方法中,不是使用执行select器,而是使用所需的延迟创buildNSTimer对象,重复设置为NO,并在当前运行循环中添加定时器
  3. 调用所有的四个animationPhase方法也一样
  4. 代码工作正常接受

      - (void)animateSequenceTemplate { if (!hasSequenceTemplateAnimated && self.isSequenceSlideCreated) { hasSequenceTemplateAnimated = YES; [self animateSequenceSlides]; } } - (void)animateSlideView:(NSNotification *)notification { NSDictionary *userInfo = notification.userInfo; if ([userInfo objectForKey:@"assetMO"] == assetMO) { [[NSNotificationCenter defaultCenter]removeObserver:self name:AnimateSequenceSlide object:nil]; CGFloat duration = 0.035; float delay = (self.slideCount * duration); CGFloat timeDelay = 0; NSTimer *animate1 = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(animationPhase1) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop]addTimer:animate1 forMode:NSRunLoopCommonModes]; timeDelay = delay + duration; NSTimer *animate2 = [NSTimer scheduledTimerWithTimeInterval:timeDelay target:self selector:@selector(animationPhase2) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop]addTimer:animate2 forMode:NSRunLoopCommonModes]; if (self.slideCount == (self.maxSlideCount - 1)) { timeDelay += duration * 18; //No animation till 18 frames } else { timeDelay += duration; } NSTimer *animate3 = [NSTimer scheduledTimerWithTimeInterval:timeDelay target:self selector:@selector(animationPhase3) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop]addTimer:animate3 forMode:NSRunLoopCommonModes]; timeDelay += duration; NSTimer *animate4 = [NSTimer scheduledTimerWithTimeInterval:timeDelay target:self selector:@selector(animationPhase4) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop]addTimer:animate4 forMode:NSRunLoopCommonModes]; } } 

我不太满意代码结构的好处。 如果您想到其他任何聪明的做法,请分享。