【问题标题】:How do I animate the movement of a view in iOS?如何在 iOS 中为视图的移动设置动画?
【发布时间】:2013-01-02 23:36:28
【问题描述】:

我是一个非常初学者的程序员,我正在尝试找到最简单的方法来做到这一点。我以前从未做过动画。我想尝试在我的应用程序中为图像设置动画,但遇到了一些问题。这是之前有人建议我使用的代码(来自另一个问题):

imageView.image = yourLastImage;  
// Do this first so that after the animation is complete the image view till show your last image.

NSArray * imageArray  = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"image1.png"],  
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],                         
[UIImage imageNamed:@"image4.png"],
nil]; 

// Note: here you may instead want to use something like [UIImage imageWithContentsOfFile:[self localImagePath:NO]] instead depending upon your targeted iOS version.



UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:
    CGRectMake(100, 125, 150, 130)];
animatedImageView.animationImages = imageArray;
animatedImageView.animationDuration = 1.1;
    myAnimation.animationRepeatCount = 1;
animatedImageView.contentMode = UIViewContentModeBottomLeft;

[self.view addSubview:animatedImageView];
[animatedImageView startAnimating];

嗯,首先,这是否实用?我想为从屏幕中间到屏幕底部的东西设置动画。这是否意味着我必须有 500 张图像,每张图像相距 1 个像素?最佳像素间隔是多少,这样它看起来流畅、均匀且不需要大量工作?有没有比上述方式更好的动画制作方法?有没有什么程序可以制作动画,以后可以添加到 Xcode 中?

另外,有人可以解释一下上面的代码吗?我是动画新手,我真的不明白这意味着什么。

对不起,如果这是一个愚蠢的问题,我在开幕式上说我是初学者。感谢您提供的任何帮助!

【问题讨论】:

    标签: objective-c ios xcode arrays animation


    【解决方案1】:

    对于大多数动画,您可以简单地更改 UIView 动画块内的视图属性。 UIView 类文档列出了哪些属性是可动画的。

    [UIView animateWithDuration:0.5f animations:^{
        aView.frame = CGRectOffset(aView.frame, 0, 250); 
    }];
    

    这是一个示例项目,演示如何在按下按钮时触发动画。您可以将此动画代码放在许多其他地方,因此除非您提供有关所需内容的更多详细信息,否则对于您关于将代码放在哪里的问题没有很好的答案。

    https://github.com/MaxGabriel/AnimationDemonstration

    您采用的方法是为UIImageView 设置动画。我从来没有这样做过,但我的理解是这就像制作一个动画 GIF。对于移动视图或淡出视图等操作,您需要使用上面显示的动画块方法。

    【讨论】:

    • 如果你想在动画完成后做一些事情你应该使用这个方法和下一个参数(void(^)(BOOL completion)。这是sn-p:pastebin.com/wfGTrLu9
    • 这个应该放在 main.m、appName.h 还是 appName.m 中?再说一次,我是初学者。
    • @user1917407 此代码应放入您正在使用的UIViewController 子类中。你几乎从未将代码放入main.m。您可以将此代码放在viewDidAppear: 中。唯一重要的是,您不要在将动画视图添加到屏幕之前输入此代码(这就是为什么您不会将其放入 main.m 的原因)。
    • @user1917407 您可能会将其放入您的应用程序使用的UIViewController 子类中。我将演示动画的示例项目上传到 Github。
    • @user1917407 我应该注意我只是在我的示例项目中为标准UIView 设置动画。如果要移动图像,请将图像放入UIImageView,然后移动UIImageView,就像在示例项目中移动UIView一样。
    【解决方案2】:

    在iOS中做动画很简单。

    CGRect basketTopFrame = self.upperview.frame;
    basketTopFrame.origin.y = -basketTopFrame.size.height;
    
    CGRect basketBottomFrame = self.lowerview.frame;
    basketBottomFrame.origin.y = self.view.bounds.size.height;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
    self.upperview.frame = basketTopFrame;
    self.lowerview.frame = basketBottomFrame;
    
    [UIView commitAnimations];
    

    【讨论】:

    • zeeshan,我在 ViewController.m 和 ViewController.h 中尝试了您的代码作为方法,我还设置了 @property (nonatomic, strong) UIView *upperview; @property (nonatomic, strong) UIView *lowerview; 我如何测试这个?我应该看到什么?
    • 它的前后显示图像在这个过程中你需要有三个图像第一个两个是动画后隐藏第三个是动画后显示谢谢
    【解决方案3】:

    以下代码对我有用,可以在打开键盘或其他东西时移动视图。

    [UIView animateWithDuration:(timeYouWantAnimate - 0.3f) animations:^{
     [nameOfView setFrame:CGRectMake(0,spaceYouWantMove like -100, self.view.with, self.view.height)];
    }
    

    希望也能帮助到你:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多