使用UILabel字母实现旋转,移动,缩放动画效果。迅速

时间:2016-01-13 18:11:07

标签: swift text rotation uilabel transform

我正在尝试旋转,移动和缩放字母。像这个视频中的东西,但有UILabels(每个标签一个字母) https://www.youtube.com/watch?v=rwE6tAQPFuY&feature=youtu.be

一个360度旋转,平移运动和同时缩放到正常尺寸。

通过执行以下操作,我设法通过正常的UIImageView实现此效果

UIView.animateWithDuration(0.5,延迟:0,选项:UIViewAnimationOptions.CurveLinear,动画:             {                 image.frame = CGRectMake(middlePosX,middlePosY,imageWidth *(1 + ranScale)/ 2,imageHeight *(1 + ranScale)/ 2)                 image.transform = CGAffineTransformRotate(letterImage.transform,CGFloat(M_PI))

        }, completion:
        { _ in
            UIView.animateWithDuration( 0.5, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations:
                {

                    image.frame = CGRectMake( finalPosX, finalPosY, imageWidth, imageHeight )
                    image.transform = CGAffineTransformRotate( letterImage.transform, CGFloat( M_PI ) )
                },
                completion: nil )

    } )

然而,当使用UILabel时,框架不会重置文本的大小。所以我尝试了一个初始转换,然后是

    letterImage.transform = CGAffineTransformScale( letterImage.transform, ranScale, ranScale )

UIView.animateWithDuration(0.5,延迟:0,选项:UIViewAnimationOptions.CurveLinear,动画:         {

        letterImage.frame = CGRectMake( middlePosX, middlePosY, imageWidth, imageHeight )

        var rotateTransform = CGAffineTransformMakeRotation( CGFloat( M_PI ) )
        var scaleTransform = CGAffineTransformMakeScale( ( 1 + ranScale ) / 2, ( 1 + ranScale ) / 2 )
        letterImage.transform = CGAffineTransformConcat( rotateTransform, scaleTransform )

    }, completion:
    { _ in
        UIView.animateWithDuration( 0.5, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations:
        {

            letterImage.frame = CGRectMake( finalPosX, finalPosY, imageWidth, imageHeight )
            letterImage.transform = CGAffineTransformIdentity
        },
        completion: nil )

    } )

但是它没有达到我想要的效果。当我将旋转与运动结合起来时,它以不同的方式旋转,然后在前一次和最后有时它甚至会出现毛刺跳跃。 看起来像这样 https://www.youtube.com/watch?v=CaD1BTwN8Yg&feature=youtu.be

如何使用UILabel字母达到预期的效果?

2 个答案:

答案 0 :(得分:0)

简答:不要。

更长的答案:使用带有text layerCore Animation的自定义UIView。

答案 1 :(得分:0)

这似乎可以解决问题。 4种不同的CABasicAnimations

    let scaleAnimation = CABasicAnimation( keyPath: "transform.scale" )
    scaleAnimation.fromValue = ranScale
    scaleAnimation.toValue = 1
    scaleAnimation.duration = 1
    textLayer.addAnimation( scaleAnimation, forKey: "transform.scale" )


    let rotateAnimation = CABasicAnimation( keyPath: "transform.rotation.z" )

    rotateAnimation.fromValue = 2 * M_PI
    rotateAnimation.toValue = 0
    rotateAnimation.duration = 1
    textLayer.addAnimation( rotateAnimation, forKey: "transform.rotation.z" )

    let moveXAnimation = CABasicAnimation( keyPath: "position.x" )
    moveXAnimation.fromValue = ranX
    moveXAnimation.toValue = textLayer.position.x
    moveXAnimation.duration = 1
    textLayer.addAnimation( moveXAnimation, forKey: "position.x" )


    let moveYAnimation = CABasicAnimation( keyPath: "position.y" )
    moveYAnimation.fromValue = ranY
    moveYAnimation.toValue = textLayer.position.y
    moveYAnimation.duration = 1
    textLayer.addAnimation( moveYAnimation, forKey: "position.y" )