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

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

问题描述

我试图让图片视图(下面的 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);
                 }];

或者,如果您想要更多地控制选项等, p>

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

但是如果你不使用autolayout,你的代码应该工作。这只是上面的语法是首选的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.

如果您使用autolayout,您(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

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

要添加 IBOutlet

顺便说一句,如果你'重新制作一个约束,对您可能链接到该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,然后选择文件检查器(最右侧面板上的第一个选项卡,或您可以通过按选项 + 命令 + 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之前的版本,请务必关闭autolayout。自动展开是一项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天全站免登陆