ios - UIView 动画在 iOS 7 中不起作用

标签 ios objective-c ipad ios7 xcode5

我正在开发一个示例项目,其中有一个垂直 scrollview 和一个水平 scrollview。垂直 scrollview 有许多 subview 。在 scrollviewDidScroll 中,我正在执行一些操作。与此同时,我现在想在垂直 ScrollView 中的 subview 在屏幕上可见时为该 subview 设置动画。为此,我正在做一些正确的计算。动画如下:

subview 包含多个自定义 View 。我正在尝试以某种特定的时间顺序对这些 View 中的每一个进行动画处理(减少然后再次增加 View 的 alpha 值)(以便动画看起来是按顺序排列的)。为此,我将通知发布到 View ,动画序列和逻辑根据我的需要是完美的。

但是我面临的问题是当我设置断点时代码正在执行,但是动画没有出现。如果我在停止滚动后发布通知,则动画效果非常好。但我希望动画即使在我滚动并且 View 在屏幕上时也能发生。

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

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

上面 subview 中的 subview :

- (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) {
}];
}

There occurs two scenarios inside - (void)animateSequenceTemplate:

  1. I call as [self animateSequenceSlides];. In this case the animation does not show up.
  2. [self performSelector:@selector(animateSequenceSlides) withObject:nil afterDelay:0.0f];. In this case the animation shows up but after the scrollview rests.

我不得不使用执行选择器来使用 UIView 动画,因为如果我删除它并使用嵌套的 UIView 动画 block /或直接调用这些方法,那么动画不会再次显示。现在至少它会在我休息滚动后显示。

我想要有关解决方案的建议或对我可能犯的错误的任何猜测。

最佳答案

您可能不在主队列上执行它。

dispatch_async(dispatch_get_main_queue, block()) 可能有帮助。 在处理动画时,主队列是执行代码的唯一位置,它使动画发生。

编辑:

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

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

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

我不确定延迟,这就是为什么我更喜欢 GCD 函数和 block 。

关于ios - UIView 动画在 iOS 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24139402/

相关文章:

iphone - 洪水填充方法因错误访问错误而崩溃有什么想法吗?

ios - ipa 或 apk 中的最大文件数

ios - 有没有办法知道 iOS 键盘是否真的被隐藏了(被用户解雇了)?

ios - 访问webViewDidFinishLoad中请求的URL?

iPhone:如何构建自己的 TabBar?

iOS Parse.com 更新对象

ios - 天球中恒星的OpenGL可视化

ios - 如何使状态栏不透明( Storyboard)

ios - FCM 通知不会出现在 iOS 12 中

ios - 如何在 UIView 上使用 UIPanGestureRecognizer 时禁用滚动