【问题标题】:Moving and animating a UIView移动和动画 UIView
【发布时间】:2014-08-21 07:42:47
【问题描述】:

我正在尝试制作一个简单的动画。我想通过使用 [UIView animateWithDuration....

将 UIImageView 移动到右侧

这是我的代码:

文件.h

{
 IBOutlet UIImageView *moveImage;
}

文件.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Move Image

    moveImage.center = CGPointMake(moveImage.center.x, moveImage.center.y);
    [UIView animateWithDuration:2.0
                     animations:^{ moveImage.center = CGPointMake(moveImage.center.x + 60.0, moveImage.center.y); }];

}

当我运行 iOS 模拟器时,它会在新的 x 坐标处显示图像,但它不会 显示动画。任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

  • 我认为它的自动布局问题。你在使用自动布局吗?
  • 不,我没有使用自动布局。我解决了这个问题,谢谢!

标签: ios objective-c iphone animation uiview


【解决方案1】:

您应该使用 ViewDidAppear 而不是 ViewDidLoad

ViewDidLoad 表示 View 已加载到内存,但未绘制。所以那时你看不到动画。

viewDidAppear 表示视图已绘制。因此,您可以在显示视图后制作动画。

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


        moveImage.center = CGPointMake(moveImage.center.x, moveImage.center.y);
        [UIView animateWithDuration:2.0
                         animations:^{ moveImage.center = CGPointMake(moveImage.center.x + 60.0, moveImage.center.y); }];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 2010-12-25
    • 2015-01-17
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多