Animating only the image in UIBarButtonItem(仅对 UIBarButtonItem 中的图像进行动画处理)
问题描述
我在 2 个应用程序中看到了这种效果,我真的很想知道如何做到这一点.
Ive seen this effect in 2 apps and I REALLY want to find how to do it.
动画在 UIBarButtonItem 中,并且只针对图像.图像是 + 符号,它会旋转到 X.
The animation is in a UIBarButtonItem, and is only to the image. The image is a + symbol, and it rotates to a X.
如果您想看到效果,您必须与某人开始对话,然后在文本输入旁边有用于图像和表情符号的 + 按钮.或者这是另一个应用程序中效果的视频,在他点击栏按钮后,您会看到它旋转到 X,http://www.youtube.com/watch?v=S8JW7euuNMo.
If you want to see the effect you have to start a conversation with someone and next to the text input theres the + button for images and emoji's. Or heres a video of the effect in another app, after he taps the bar button you see it rotate to a X, http://www.youtube.com/watch?v=S8JW7euuNMo.
我已经找到了如何实现效果,但仅在 UIImageView 上,我必须关闭所有自动调整大小并且视图模式必须居中,然后对其应用旋转变换.我已经尝试了许多尝试让它在栏项目中工作的方法,到目前为止,最好的方法是添加一个图像视图实例,然后设置它并将视图模式设置为居中并自动调整大小,然后使用该图像视图进行自定义栏项目视图.但是当我这样做时,效果会起作用,除非在它这样做时,图像会稍微偏向一边,而不是停留在它已经在的位置.我尝试在动画之前获取中心并在动画期间设置它,但这没有任何作用.
I have found out how to do the effect but only on a UIImageView, I have to turn off all the autoresizing and the view mode has to be centered, then apply the rotation transform to it. I have tried many ways of trying to have it work in a bar item and so far the best way is adding a image view instance, then setting it up and setting the view mode centered and autoresizing off and then using that image view for a custom bar item view. But when i do this, the effect works except while its doing it, the image will go off to the side a little bit instead of staying where it already is. Ive tried getting the center before the animation and set it during the animation but that doesnt do anything.
推荐答案
所以这个问题的答案是你必须创建一个 Image 视图的实例,然后设置它而不调整大小并且视图模式居中.然后将图像视图添加到自定义类型的 UIButton 中,然后将按钮用作栏项的自定义视图.
So the answer for this is you have to make a instance of the Image view, then set it up with no resizing and view mode is centered. Then add the image view to a UIButton with custom type, and then use the button as the custom view for the bar item.
- (IBAction)animate {
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
} completion:^(BOOL finished) {
imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0));
if ([imageView.image isEqual:[UIImage imageNamed:@"Add.png"]]) {
imageView.image = [UIImage imageNamed:@"Close.png"];
}
else imageView.image = [UIImage imageNamed:@"Add.png"];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Add.png"]];
imageView.autoresizingMask = UIViewAutoresizingNone;
imageView.contentMode = UIViewContentModeCenter;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button addSubview:imageView];
[button addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
imageView.center = button.center;
barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navItem.rightBarButtonItem = barItem;
}
这篇关于仅对 UIBarButtonItem 中的图像进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅对 UIBarButtonItem 中的图像进行动画处理
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01