iOS同步翻译和缩放问题(CAAnimation)

时间:2013-05-28 15:20:51

标签: ios translation scaling simultaneous

我有一种方法可以翻译视图,并可以在翻译时对其进行缩放。我正在使用动画组并根据需要添加所需的动画。以下是完美运作的动画案例。

  1. 翻译(x或y轴或两者)(确定)
  2. 缩放(OK)
  3. 翻译 缩放(MOSTLY)
  4. 然而,在缩放时进行转换仅在起始比例因子为1时才起作用。当视图已经缩放时,我注意到大部分过渡跳跃而不是动画,尽管缩放的最终结果并且翻译是正确的。

    我发布了部分代码以便澄清。仅当previousZoomScale不是1时才会出现此问题!我感谢你指点我的任何指示。


    -(void)performAnimationForView: (AnimationType)animationType
                            WithDX: (CGFloat)dx
                             AndDY: (CGFloat)dy
                             Scale: (CGFloat)scale
                          Duration: (CGFloat)duration
    {
        CABasicAnimation *translateXAnimation = nil,     *translateYAnimation = nil, *scaleAnimation = nil;
        NSString* animationGroupName;
    
        CGPoint newDxDy;
    
        switch (animationType)
        {
    
            case kAnimationTranslateAndScale:
                // set animation key
                animationGroupName = @"translatescale";
    
                translateXAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
                translateXAnimation.repeatCount = 0;
                translateXAnimation.autoreverses = NO;
                translateXAnimation.removedOnCompletion = NO;
                translateXAnimation.fillMode = kCAFillModeForwards;
                translateXAnimation.fromValue = [NSNumber numberWithFloat: self.previousDxDy.x]; // previous point we're tranlsating from
                translateXAnimation.toValue = [NSNumber numberWithFloat:dx]; // destination point
    
                translateYAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
                translateYAnimation.repeatCount = 0;
                translateYAnimation.autoreverses = NO;
                translateYAnimation.removedOnCompletion = NO;
                translateYAnimation.fillMode = kCAFillModeForwards;
                translateYAnimation.fromValue = [NSNumber numberWithFloat: self.previousDxDy.y]; 
                translateYAnimation.toValue = [NSNumber numberWithFloat:dy]; 
    
                newDxDy = CGPointMake(dx, dy);
                self.previousDxDy = newDxDy;
    
    // scaling animation
                scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
                scaleAnimation.autoreverses = NO;
                scaleAnimation.repeatCount = 0;
                scaleAnimation.removedOnCompletion = NO;
                scaleAnimation.fillMode = kCAFillModeForwards;
                scaleAnimation.fromValue = [NSNumber numberWithDouble: self.previousZoomScale]; // initial zoom scale
                scaleAnimation.toValue = [NSNumber numberWithDouble:scale]; // target scale
    
                self.previousZoomScale = scale;
    
            break;
        }
    
        NSMutableArray* animationArray = [[NSMutableArray alloc] initWithObjects: nil];
    
        if (translateXAnimation != nil)
            [animationArray addObject: translateXAnimation];
    
        if (translateYAnimation != nil)
            [animationArray addObject: translateYAnimation];
    
        if (scaleAnimation != nil)
            [animationArray addObject: scaleAnimation];
    
        CAAnimationGroup *theGroup = [CAAnimationGroup animation];
    
        theGroup.duration = duration;
        theGroup.removedOnCompletion = NO;
        theGroup.fillMode = kCAFillModeForwards;
        theGroup.repeatCount = 0;
        theGroup.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
        theGroup.animations = animationArray;
        theGroup.delegate = self;
    
        [theGroup setValue: animationGroupName forKey: @"animationtype"];
    
        [self.backgroundImageView.layer addAnimation: theGroup forKey: animationGroupName];
    }
    

0 个答案:

没有答案