动画期间的UIView缩放 [英] UIView scaling during animation

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

问题描述

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

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

现在我想动画该视图。

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

//改变视图框架

[UIView commitAnimations];

我期望的是平铺区域只是增长,而是,当区域增长时,视图的原始内容被缩放到新的大小。至少在动画期间。所以在开始和结束的动画一切都是好的。在动画过程中,图块被扭曲。



为什么CA要缩放?如何防止它这样做?

解决方案

如果Core Animation必须为每个动画帧回调您的代码,



使用 UIViewContentModeRedraw 是在正确的轨道,也是你会从CA获得的最好的。问题是从UIKit的角度来看,框架只有两个值:转换开始时的值和转换结束时的值,这就是你看到的。如果您查看Core Animation架构文档,您将看到CA如何对所有图层属性及其值随时间变化的私有表示。



因此,唯一的方法是使用 NSTimer (或 performSelector:withObject:afterDelay:)更改视图帧。 b $ b

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.

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

解决方案

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.

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.

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缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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