动画图像视图向上滑动 [英] Animating an image view to slide upwards

查看:26
本文介绍了动画图像视图向上滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使图像视图(下面的logo)向上滑动 100 像素.我正在使用此代码,但什么也没有发生:

I am attempting to make an image view (logo below) slide upwards by 100 pixels. I am using this code, but nothing happens at all:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
logo.center = CGPointMake(logo.center.x, logo.center.y - 100);
[UIView commitAnimations];

此代码位于 viewDidLoad 方法中.具体来说,logo.center = ... 不起作用.其他事情(如更改 alpha)也可以.也许我没有使用正确的代码向上滑动它?

This code is in the viewDidLoad method. Specifically, the logo.center = ... is not functioning. Other things (like changing the alpha) do. Maybe I'm not using the right code to slide it upwards?

推荐答案

对于非自动布局故事板/NIB,您的代码没问题.顺便说一句,现在通常建议您使用 :

For non-autolayout storyboards/NIBs, your code is fine. By the way, it's now generally advised that you animate using blocks:

[UIView animateWithDuration:3.0
                 animations:^{
                     self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100.0);
                 }];

或者,如果您想对选项等进行更多控制,可以使用:

Or, if you want a little more control over options and the like, you can use:

[UIView animateWithDuration:3.0
                      delay:0.0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                     self.logo.center = CGPointMake(self.logo.center.x, self.logo.center.y - 100);
                 }
                 completion:nil];

但是如果您不使用自动布局,您的代码应该可以工作.只是上述语法更适用于 iOS 4 及更高版本.

But your code should work if you're not using autolayout. It's just that the above syntax is preferred for iOS 4 and later.

如果您使用自动布局,您 (a) 为您的垂直空间限制创建一个 IBOutlet(见下文),然后 (b) 您可以执行以下操作:

If you're using autolayout, you (a) create an IBOutlet for your vertical space constraint (see below), and then (b) you can do something like:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    static BOOL logoAlreadyMoved = NO; // or have an instance variable

    if (!logoAlreadyMoved)
    {
        logoAlreadyMoved = YES; // set this first, in case this method is called again

        self.imageVerticalSpaceConstraint.constant -= 100.0;
        [UIView animateWithDuration:3.0 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
}

要为约束添加IBOutlet,只需control-将约束从约束拖动到辅助编辑器中的.h:

To add an IBOutlet for a constraint, just control-drag from the constraint to your .h in the assistant editor:

顺便说一下,如果您正在为约束设置动画,请注意您可能已链接到该图像视图的任何其他约束.通常,如果您将某些东西放在图像正下方,它会将其约束链接到图像,因此您可能必须确保没有任何其他控件对图像进行约束(除非您也希望它们移动).

By the way, if you're animating a constraint, be sensitive to any other constraints that you might have linked to that imageview. Often if you put something right below the image, it will have its constraint linked to the image, so you may have to make sure you don't have any other controls with constraints to your image (unless you want them to move, too).

您可以通过打开故事板或 NIB,然后选择文件检查器"(最右侧面板上的第一个选项卡,或者您可以通过按 option 将其拉起)来判断您是否使用了自动布局.kbd>+command+1(数字1"):

You can tell if you're using autolayout by opening your storyboard or NIB and then selecting the "file inspector" (the first tab on the right most panel, or you can pull it up by pressing option+command+1 (the number "1")):

请记住,如果您打算支持 iOS 6 之前的版本,请务必关闭自动布局".自动布局是 iOS 6 的一项功能,不适用于较早版本的 iOS.

Remember, if you planning on supporting pre-iOS 6, make sure to turn off "autolayout". Autolayout is an iOS 6 feature and won't work on earlier versions of iOS.

这篇关于动画图像视图向上滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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