在CABasicAnimations之后,UIScrollview变慢

时间:2012-01-10 20:10:23

标签: iphone objective-c performance uiscrollview cabasicanimation

正如标题所示,我的UIScrollview出现了问题,在播放CABasicAnimation后,这些动作变得不稳定。动画将屏幕上的UI元素(最多10个)翻转,同时淡出它们。为此,我使用下面的代码:

-(void)flipLayers:(UIView *)view {

    CGFloat subviewX = ((1/view.frame.size.width)*(view.frame.origin.x+200));
    CGFloat subviewY = 0.5;
    [self setAnchorPoint:CGPointMake(-subviewX, subviewY) forView:view];

    CALayer *layer = view.layer;
    layer.shouldRasterize = YES;

    CATransform3D aTransform = CATransform3DIdentity;
    CGFloat zDistance = 2000;
    aTransform.m34 = 1.0 / -zDistance;  
    scrollView.layer.sublayerTransform = aTransform;

    CATransform3D bTransform = CATransform3DIdentity;

    CABasicAnimation *rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform"];

    rotateAnim.fromValue= [NSValue valueWithCATransform3D:bTransform];
    bTransform = CATransform3DRotate(aTransform, -M_PI_2, 0, 1, 0);;
    rotateAnim.duration=0.2;
    rotateAnim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
    rotateAnim.toValue=[NSValue valueWithCATransform3D:bTransform];
    layer.transform = bTransform;
    rotateAnim.removedOnCompletion = YES;
    [layer addAnimation:rotateAnim forKey:nil];

    CABasicAnimation *fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnim.duration = 0.12;
    fadeAnim.removedOnCompletion = YES;
    fadeAnim.beginTime = CACurrentMediaTime() + 0.08;
    fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
    fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
    [layer addAnimation:fadeAnim forKey:nil];

 }

要以类似的方式恢复UI元素原始位置,我使用此代码

-(void)flipLayersBackwards:(UIView *)view  {

    CALayer *layer = view.layer;

    CATransform3D aTransform = CATransform3DIdentity;
    CGFloat zDistance = 2000;
    aTransform.m34 = 1.0 / -zDistance;  
    scrollView.layer.sublayerTransform = aTransform;

    CATransform3D bTransform = CATransform3DIdentity;

    CABasicAnimation *rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    rotateAnim.fromValue= [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 1, 0)];
    bTransform = CATransform3DRotate(aTransform, 0, 0, 1, 0);;
    rotateAnim.duration=0.2;
    rotateAnim.removedOnCompletion = YES;
    rotateAnim.toValue=[NSValue valueWithCATransform3D:bTransform];
    rotateAnim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut];
    layer.transform = bTransform;
    [layer addAnimation:rotateAnim forKey:@"rotateAnim"];

    CABasicAnimation *fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnim.duration = 0.064;
    fadeAnim.removedOnCompletion = YES;
    fadeAnim.beginTime = CACurrentMediaTime() + 0.02;
    fadeAnim.fromValue = [NSNumber numberWithFloat:0.0];
    fadeAnim.toValue = [NSNumber numberWithFloat:1.0];
    [layer addAnimation:fadeAnim forKey:nil];

    layer.shouldRasterize = NO;

}

使用动画的次数越多,滚动视图的移动就越快。有谁知道可能导致这种情况的原因是什么?任何有关如何解决这个问题的帮助将不胜感激。在此先感谢:)

编辑:我发现透视变换是造成波动的原因,但我不知道该怎么办

0 个答案:

没有答案