在viewDidLoad中移动图像

时间:2014-02-23 16:51:22

标签: ios uiimageview

我尝试在viewDidLoad中移动球像,如下所示。我跑了Log显示x值从160变为50,正如我预期的那样。但是,iOS模拟器上的图像仍然位于Interface Builder的原始位置,其中x为160.为什么?

如何在图像显示在屏幕上之前移动图像的位置?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"%f", Ball.center.x);
    Ball.center = CGPointMake(50, 50);
    NSLog(@"%f", Ball.center.x);
}

3 个答案:

答案 0 :(得分:1)

而不是viewDidLoad将您的代码添加到viewDidLayoutSubviews方法

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    Ball.center = CGPointMake(50, 50);
}

答案 1 :(得分:0)

调用[self.view setNeedsDisplay];方法,因为在视图实际加载并在设备上绘制后,您的viewDidLoad触发器,并且您更改了图像位置,您应该告诉设备使用新图像重绘视图位置。

答案 2 :(得分:0)

一些建议:

1. First check that `Ball` is connected to IBOutlet in interface builder.

2. To change the frame of a UIImageView, I would suggest to use UIView animation 
   blocks for smooth animation effects

3. You can also use `performSelector` and delay the animation to e.g., `0.1` sec etc.

希望这一点可以指导您解决此问题。