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

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

问题描述

我试图使图像视图(标志以下),100像素向上滑动。我用这code,但没有任何反应都:

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];

这code是在 viewDidLoad中方法。具体来说, logo.center = ... 不能正常工作。其他的事情(比如改变阿尔法)做的。也许我没有使用正确的code向上滑动?

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?

推荐答案

对于非自动布局故事板/发钞银行,你的code是罚款。顺便说一句,它现在一般建议您使用动画<一个href=\"http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW109\">blocks:

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];

但你的code应,如果你不使用自动布局工作。这只是上面的语法是iOS 4的,后来preferred。

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.

如果您在使用自动布局,你(一)创建一个 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

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

要添加 IBOutlet中为约束,只是<大骨节病>控制 -drag从助理编辑约束你.H:

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

顺便说一句,如果你是动画约束,是你可能会链接到ImageView的任何其他限制敏感。通常,如果你把正确的东西下面的图片,这将有其约束链接到图像,所以您可能需要确保你没有与任何约束的其他控件图像(除非你想让他们搬家,太)

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,然后选择文件检查器(最右边的面板上的第一个选项卡,也可以通过pressing拉起<大骨节病>选项​​ + <大骨节病>命令 + <大骨节病> 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")):

记住,如果你在支持pre-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天全站免登陆