代码之家  ›  专栏  ›  技术社区  ›  Lukasz

iPhone动画问题模拟器与真实设备

  •  2
  • Lukasz  · 技术社区  · 16 年前

    这是我第一个问题,所以请温柔一点: 我有以下动画代码在模拟器和真实设备上运行平稳(我在iPhone3GS 3.1.2上测试)。 动画是两个视图之间的简单转换,类似于翻页。

    模拟器和真实设备之间的一个不同之处(我无法研究解决的问题)是,当动画完成时,在真实设备上——在旋转完成后,动画视图闪烁(显示几秒钟),然后隐藏。在模拟器上,这种“意外”的闪烁不会发生。

    下面是动画代码:

    -(void)flip{
        UIView *animatedView;
    
        // create an animation to hold the page turning
        CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
        transformAnimation.removedOnCompletion = NO;
        transformAnimation.delegate = self;
        transformAnimation.duration = ANIMATION_TIME;
        transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
        // this is the basic rotation by 90 degree along the y-axis
        CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f,
                                                               0.0f,
                                                               -1.0f,
                                                               0.0f);
        // these values control the 3D projection outlook
        endTransform.m34 = 0.001f;
        endTransform.m14 = -0.0015f;
    
    
        // start the animation from the current state
        transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
        transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform];
        animatedView = screenShot;
    
        // Create an animation group to hold the rotation and possibly more complex animation in the future
        CAAnimationGroup *theGroup = [CAAnimationGroup animation];
    
        // Set self as the delegate to receive notification when the animation finishes
        theGroup.delegate = self;
        theGroup.duration = ANIMATION_TIME;
        // CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag
        // to identify the animation later when it finishes
        [theGroup setValue:[NSNumber numberWithInt:animatedView.tag] forKey:@"animated"];
        // Here you could add other animations to the array
        theGroup.animations = [NSArray arrayWithObjects:transformAnimation,nil];
        theGroup.removedOnCompletion = NO;
        // Add the animation group to the layer
        if (animatedView.layer.anchorPoint.x != 0.0f) 
        {
            animatedView.layer.anchorPoint = CGPointMake(0.0f, 0.5f);
            float yy = animatedView.center.x - (animatedView.bounds.size.width / 2.0f);
            animatedView.center = CGPointMake(yy, animatedView.center.y);
        }
    
        if(![animatedView isDescendantOfView:self.view])[self.view addSubview:animatedView];
        screenShot.hidden = NO;
        animatedView.hidden = NO;
    
    
        [animatedView.layer addAnimation:theGroup forKey:@"flip"];
    
    }
    
    - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    
        screenShot.hidden = YES;
    
    }
    
    3 回复  |  直到 16 年前
        1
  •  5
  •   Brad Larson    16 年前

    尝试将组的和/或TransformAnimation的FillMode设置为kCaFillModeForwards:

    theGroup.fillMode = kCAFillModeForwards;
    

    这将导致动画在活动持续时间完成后保持不变。我以前用它来消除动画闪烁的结束。

        2
  •  0
  •   TechZen    16 年前

    我不确定我完全理解你看到的是什么,但你可能看到的是由设备和模拟器之间的速度差引起的。模拟器要比设备快得多,所以一些“闪烁”得很快的东西可能会使人眼在模拟器上看不到。

        3
  •  0
  •   Ian Henry    16 年前

    OOF。这很烦人。我认为在动画停止和这行代码执行之间有一个分秒。如果有一个 animationWillStop: 方法!你可以试试,而不是 screenShot.hidden = YES , screenShot.alpha = 0 . 我怀疑这是否能在速度上有所不同,但这可能值得一试?你也可以淡出视图作为动画的一部分,但我不认为你想这样做。

    我唯一能想到的是 NSTimer 在你的动画时间下面有一段时间间隔。类似:

    [NSTimer scheduledTimerWithTimeInterval:(ANIMATION_TIME - .001) target:self selector:@selector(hideTheView) userInfo:nil repeats:NO];
    

    然后实施 hideTheView 当然是方法。