可以在Cocoa Touch中对可拉伸按钮图像的大小进行动画处理吗? [英] Can the size of a stretchable button image be animated in Cocoa Touch?

查看:90
本文介绍了可以在Cocoa Touch中对可拉伸按钮图像的大小进行动画处理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 stretchableImageWithLeftCapWidth:topCapHeight:



创建一个UIButton,我想像这样用Core Animation平滑地更改它的大小:

  floatrinkFactor = 0.2; 
NSTimeInterval slowSpeed = 5.0;

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

UIButton * thisButton =(UIButton *)[self.view viewWithTag:2];
thisButton.frame = CGRectMake(thisButton.frame.origin.x,thisButton.frame.origin.y,buttonStretchedWidth *收缩因子,thisButton.frame.size.height);

[UIView commitAnimations];

按钮图像的大小没有动画,只是可以捕捉到新的大小。



当使按钮的 backgroundColor 可见时,我看到框架本身具有正确的动画效果。



我丢失了某些东西还是图像无法动画显示?

解决方案

我从来没有得到过图像拉伸以连续更新。尝试将图像拆分为顶部和中心区域,并使用适当的 autoresizingMask 设置创建八个或九个 UIImageView 子视图。例如。右上角的视图应具有 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin ,底部中心视图(得到拉伸)应该具有 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin ,依此类推。创建iPhone视图后,视图就非常便宜了,因此,除非您一次为六个按钮设置动画,否则我认为您不会看到性能受到影响。不过,请确保使任何不透明的内容都变得不透明-合成不透明的视图非常昂贵。


I'm creating a UIButton with a stretchableImageWithLeftCapWidth:topCapHeight:

I would like to change it's size smoothly with Core Animation like so:

float shrinkFactor = 0.2;
NSTimeInterval slowSpeed = 5.0;

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

UIButton *thisButton = (UIButton *)[self.view viewWithTag:2];
thisButton.frame = CGRectMake(thisButton.frame.origin.x, thisButton.frame.origin.y, buttonStretchedWidth * shrinkFactor, thisButton.frame.size.height);

[UIView commitAnimations];

The button image size doesn't animate, it just snaps to the new size.

When I make the backgroundColor of the button visible, I see the frame itself animates correctly.

Am I missing something or is the image stretching not animatable?

解决方案

I've never been able to get image stretching to update continuously. Try splitting your image up into the caps and center areas and create eight or nine UIImageView subviews with the appropriate autoresizingMask set. E.g. the top-right corner view should have UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin, the bottom-center view (getting stretched) should have UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin, and so on. Views are pretty cheap on the iPhone OS, once they're created, so I don't think you'll see a performance hit unless you're animating a half-dozen buttons at once. Make sure to make whatever you can opaque, though—compositing non-opaque views is pretty expensive.

这篇关于可以在Cocoa Touch中对可拉伸按钮图像的大小进行动画处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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