动画期间的 UIView 缩放

UIView scaling during animation(动画期间的 UIView 缩放)

本文介绍了动画期间的 UIView 缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 UIView 来显示平铺图像.

I've got a custom UIView to show a tiled image.

    - (void)drawRect:(CGRect)rect
    {
        ...
        CGContextRef context = UIGraphicsGetCurrentContext();       
        CGContextClipToRect(context,
             CGRectMake(0.0, 0.0, rect.size.width, rect.size.height));      
        CGContextDrawTiledImage(context, imageRect, imageRef);
        ...
    }

现在我正在尝试为该视图的调整大小设置动画.

Now I am trying to animate the resize of that view.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];

// change frame of view

[UIView commitAnimations];

我所期望的是平铺区域只会增长,而平铺大小保持不变.相反,当区域增长时,视图的原始内容被缩放到新的大小.至少在动画期间.所以动画的开头和结尾都很好.在动画期间,瓷砖会变形.

What I would expect is for the tiled area just to grow, leaving the tile sizes constant. What happens instead is that while the area grows the original content of the view is scaled to the new size. At least during the animation. So the at the beginning and the end of the animation all is good. During the animation the tiles are distorted.

CA 为什么要尝试扩展?我怎样才能防止它这样做?我错过了什么?

Why is CA trying to scale? How can I prevent it from doing so? What did I miss?

推荐答案

如果 Core Animation 必须为每个动画帧回调你的代码,它永远不会像现在这样快,而且自定义属性的动画已经很久了Mac 上 CA 的请求功能和常见问题解答.

If Core Animation had to call back to your code for every animation frame it would never be as fast as it is, and animation of custom properties has been a long requested feature and FAQ for CA on the Mac.

使用 UIViewContentModeRedraw 是正确的,也是您从 CA 获得的最好的.问题是从 UIKit 的角度来看,框架只有两个值:过渡开始时的值和过渡结束时的值,这就是您所看到的.如果您查看 Core Animation 架构文档,您会看到 CA 如何拥有所有层属性的私有表示形式,并且它们的值随时间变化.这就是帧插值发生的地方,您无法在发生更改时收到通知.

Using UIViewContentModeRedraw is on the right track, and is also the best you'll get from CA. The problem is from the UIKit point of view the frame only has two values: the value at the beginning of the transition and the value at the end of the transition, and that's what you're seeing. If you look at the Core Animation architecture documents you'll see how CA has a private representation of all layer properties and their values changing over time. That's where the frame interpolation is happening, and you can't be notified of changes to that as they happen.

所以唯一的方法是使用 NSTimer(或 performSelector:withObject:afterDelay:)随时间改变视图框架,这是老式的方式.

So the only way is to use an NSTimer (or performSelector:withObject:afterDelay:) to change the view frame over time, the old fashioned way.

这篇关于动画期间的 UIView 缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:动画期间的 UIView 缩放

基础教程推荐