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

查看:90
本文介绍了仅对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,

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天全站免登陆