UIView动画在iOS 7中无效 [英] UIView animation not working in iOS 7

查看:90
本文介绍了UIView动画在iOS 7中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个示例项目,其中我有一个垂直 scrollview 和一个水平 scrollview 。垂直 scrollview 有许多子视图。在 scrollviewDidScroll 我正在执行一些操作。与此同时,我现在想要在屏幕上显示特定子视图时为垂直滚动视图内的子视图设置动画。为此,我正在做一个正确的计算。动画如下:

I am working on a sample project in which I have a vertical scrollview and a horizontal scrollview. The vertical scrollview has a number of subviews. In scrollviewDidScroll I am performing some actions. Along with this I now want to animate a subview inside the vertical scrollview when that particular subview is visible on the screen. For this I am doing a certain calculation which is correct. The animation is as follows:

子视图包含多个自定义视图。我试图在某个特定时间序列中为每个视图设置动画(减少然后再次增加视图的alpha值)(以便动画看起来按顺序)。为此我发布通知给视图和动画序列和逻辑是完美的根据我想要的。

The subview contains multiple custom views. I am trying to animate(reducing and then again increasing alpha value for the view) each of these views in some particular time sequence(so that the animation looks to be in sequence). For this I am posting notification to the views and the animation sequence and logic is perfect according to what I want.

但我面临的问题是代码执行时我把断点,但动画没有出现。如果我在停止滚动后发布通知,则动画完全正常。但我想要的是动画即使在我滚动并且视图在屏幕上时也会发生。

But I am facing the problem that the code is executing when I put the breakpoint, but the animation does not show up. In case if I post the notification after I stop scrolling then the animation happens perfectly fine. But I want is the animation to happen even when I am scrolling and the view is on screen.

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

I am adding my code snippet as below:

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

上述子视图中的子视图:

Subviews inside the above 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) {
}];
}




<$ c $内有两种情况c> - (void)animateSequenceTemplate :


  1. 我称之为 [self animateSequenceSlides] ; 。在这种情况下,动画不会显示。

  2. [self performSelector:@selector(animateSequenceSlides)withObject:nil afterDelay:0.0f]; 。在这种情况下,动画显示但在 scrollview 休息之后。

  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动画块/或直接调用这些方法,然后再次动画不显示。现在,它在我休息滚动之后显示出来。

I am compelled to use the UIView animation using perform selector because if I remove this and use either nested UIView animate blocks / or directly call these methods, then again animation does not show up. Now at lease it shows up after the I rest scrolling.

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

I would like advice on a solution or any guess on what mistake I might be making.

推荐答案

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

Finally, i got the solution to my question and it is working very fine as expected.

所做的更改:


  1. 来自animateSequenceTemplate,直接调用animateSequenceSlides
    方法

  2. 在上面提到的animateSlideView方法中,我没有使用执行选择器,而是创建了具有所需延迟的NSTimer对象,
    重复设置为NO,并在当前运行循环中添加计时器

  3. 为调用所有四个animationPhase方法做了同样的事情

  4. 代码正常工作

  1. From "animateSequenceTemplate", calling "animateSequenceSlides" method directly
  2. In "animateSlideView" method mentioned above, instead of using perform selector I created NSTimer object with the required delay, repeat set to NO, and adding the timer in the current run loop
  3. Did the same for calling all the four animationPhase methods
  4. Code works fine as accepted

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


我对代码结构的优点不满意。如果你想到其他任何聪明的方法,请分享。

I am not satisfied with the goodness of the code structure. If you think of any other smart way of doing it, please share.

这篇关于UIView动画在iOS 7中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆