iphone - 使用固定中心点为 UIButton 中的宽度变化设置动画

标签 iphone objective-c cocoa-touch uikit core-animation

这个问题我已经遇到过好几次了,但是我在任何地方都找不到确切的答案。

我想为 UIButton 的宽度变化设置动画,类似于在 App Store 中按下 FREE 按钮时它会变为 INSTALL 时的效果。触摸后,按钮会展开但不会移动。在应用商店中,按钮只是向左扩展:我希望我的按钮向两个方向扩展。

我试过将 button.frame = ... 或 button.bounds = ... 包装在 [UIView beginAnimations:context:] block 中,但这不起作用,即使我也将中心设置在 block 内。不管我做什么,按钮似乎只是动画到一个新的位置,然后捕捉到新的宽度。我在这里遗漏了什么吗?

作为附录,谷歌搜索发现很多人都有类似的问题,而且他们往往只是放弃他们试图做的任何动画,这对我来说似乎很奇怪。这应该是一个简单的解决方案,对吧? :)

谢谢!

最佳答案

我刚刚测试过它,它运行良好:

// Button setup (to keep text central during animation)
button.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

// Animate
[UIView beginAnimations:nil context:nil];
CGRect old = button.frame;
CGFloat diff = 60;
button.frame = CGRectMake(old.origin.x-diff/2.0, old.origin.y, old.size.width+diff, old.size.height);
[UIView commitAnimations];

关于iphone - 使用固定中心点为 UIButton 中的宽度变化设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198866/

相关文章:

iphone - 有或没有 UITextFieldDelegate?

iphone - 可以在运行时使用 Objc/iPhone 代码针对 xsd 验证 xml

iphone - 使用 sendAsynchronousRequest :queue:completionHandler:? 时跟踪重定向

iphone - iOS 应用程序在升级时崩溃

iphone - 如何将 Yelp APi 集成到 iPhone 应用程序中?

iphone - AVQueuePlayer是否支持异步添加AVPlayerItems?

cocoa-touch - 取消 UIView animateWithDuration 中的 block

html - 为什么 border-radius 在 MobileSafari 中的文本输入上不能正常工作?

ios - 我想在 UIView 上绘制像苹果形状这样的自定义形状

objective-c - 设置 NSTask 工作目录