仅对 UIBarButtonItem 中的图像进行动画处理 [英] Animating only the image in UIBarButtonItem

查看:33
本文介绍了仅对 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 中的图像进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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