CGAffineTransform scale and translation - jump before animation

后端 未结 5 1121
孤独总比滥情好
孤独总比滥情好 2021-02-01 03:43

I am struggling with an issue regarding CGAffineTransform scale and translation where when I set a transform in an animation block on a view that already has a transform the vie

5条回答
  •  無奈伤痛
    2021-02-01 04:29

    I ran into the same issue, but couldn't find the exact source of the problem. The jump seems to appear only in very specific conditions: If the view animates from a transform t1 to a transform t2 and both transforms are a combination of a scale and a translation (that's exactly your case). Given the following workaround, which doesn't make sense to me, I assume it's a bug in Core Animation.

    First, I tried using CATransform3D instead of CGAffineTransform.

    Old code:

    var transform = CGAffineTransformIdentity
    transform = CGAffineTransformScale(transform, 1.1, 1.1)
    transform = CGAffineTransformTranslate(transform, 10, 10)
    view.layer.setAffineTransform(transform)
    

    New code:

    var transform = CATransform3DIdentity
    transform = CATransform3DScale(transform, 1.1, 1.1, 1.0)
    transform = CATransform3DTranslate(transform, 10, 10, 0)
    view.layer.transform = transform
    

    The new code should be equivalent to the old one (the fourth parameter is set to 1.0 or 0 so that there is no scaling/translation in z direction), and in fact it shows the same jumping. However, here comes the black magic: In the scale transformation, change the z parameter to anything different from 1.0, like this:

    transform = CATransform3DScale(transform, 1.1, 1.1, 1.01)
    

    This parameter should have no effect, but now the jump is gone.

提交回复
热议问题