CGAffineTransform缩放和翻译 - 在动画之前跳转 [英] CGAffineTransform scale and translation - jump before animation

查看:134
本文介绍了CGAffineTransform缩放和翻译 - 在动画之前跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决有关CGAffineTransform缩放和平移的问题,当我在已经有变换的视图上的动画块中设置变换时,视图会在动画之前跳转一点。

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 view jumps a bit before animating.

示例:

// somewhere in view did load or during initialization
var view = UIView()
view.frame = CGRectMake(0,0,100,100)
var scale = CGAffineTransformMakeScale(0.8,0.8)
var translation = CGAffineTransformMakeTranslation(100,100)
var concat = CGAffineTransformConcat(translation, scale)
view.transform = transform

// called sometime later
func buttonPressed() {
    var secondScale = CGAffineTransformMakeScale(0.6,0.6)
    var secondTranslation = CGAffineTransformMakeTranslation(150,300)
    var secondConcat = CGAffineTransformConcat(secondTranslation, secondScale)
    UIView.animateWithDuration(0.5, animations: { () -> Void in 
         view.transform = secondConcat
    })

}

现在当buttonPressed()被称为视图在开始动画之前跳转到左上角大约10个像素。我只用一个concat变换来见证这个问题,只使用一个转换变换工作正常。

Now when buttonPressed() is called the view jumps to the top left about 10 pixels before starting to animate. I only witnessed this issue with a concat transform, using only a translation transform works fine.

编辑:因为我已经做了很多关于此事的研究我认为我应该提一下,无论是否打开了自动布局,都会出现此问题

Since I've done a lot of research regarding the matter I think I should mention that this issue appears regardless of whether or not auto layout is turned on

推荐答案

我遇到了同样的问题,但不能找不到问题的确切来源。跳转似乎只出现在非常特定的条件下:如果视图从转换 t1 动画到转换 t2 并且两种变换都是规模和翻译的结合(这正是你的情况)。鉴于以下解决方法对我来说没有意义,我认为它是Core Animation中的一个错误。

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.

首先,我尝试使用 CATransform3D 而不是 CGAffineTransform

First, I tried using CATransform3D instead of CGAffineTransform.

旧代码:

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

新代码:

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

新代码应该相当于旧的(第四个参数设置为 1.0 0 ,以便<$ c中没有缩放/翻译$ c> z 方向),实际上它显示了s ame跳跃。然而,这里出现了黑魔法:在比例转换中,将 z 参数更改为与 1.0 不同的任何内容,如这个:

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.

这篇关于CGAffineTransform缩放和翻译 - 在动画之前跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆