iOS动态移动的图片不会留下来

时间:2013-12-10 20:52:47

标签: ios objective-c

我正在为iPhone编写一个应用程序,在加载后,将一些UIImages移动到屏幕上的特定位置。当应用程序第一次出现时,一切都在我想要的地方。但是如果我显示一个模态页面,当该页面正在转换时,图像会在页面出现时“弹回”到它们的旧位置一瞬间。 (即使我在同一事件中再次调用图像移动代码)。然后,当我关闭模态页面时,图像将根本不会移动,即使它确实显示我的图像移动器事件在日志中运行。 这是我的代码:

-(void)ajustScreen
{
NSLog(@"Move Images");
self.imageA.frame = CGRectMake(0, 0, 80, 80);
self.imageB.frame = CGRectMake(0, 0, 80, 80);
self.imageA.center = CGPointMake(100, 100);
self.imageB.center = CGPointMake(180, 180);
}

我这里有这个代码:

[self performSelector:@selector(ajustScreen) withObject:nil afterDelay:0.0];

在屏幕旋转时触发,在viewWillAppear,viewWillDisappear,viewDidLoad中,然后在NSNotificationCenter中,当模式被解除时应该调用它。

-(void)sendToAbout
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ajustScreen) name:@"SecondViewControllerDismissed" object:nil];
[self performSegueWithIdentifier: @"goabout" sender: self];
}

(在模态中):

- (IBAction)BackButton:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"SecondViewControllerDismissed"
                                                    object:nil
                                                  userInfo:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

让我感到困惑的是,没有错误,每次,日志都会打印出它已经运行,但它不能始终如一地工作。

1 个答案:

答案 0 :(得分:1)

尝试使用UIViewController方法viewDidLayoutSubviews。类似的东西:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.imageA.frame = CGRectMake(0, 0, 80, 80);
    self.imageB.frame = CGRectMake(0, 0, 80, 80);
    self.imageA.center = CGPointMake(100, 100);
    self.imageB.center = CGPointMake(180, 180);
}

旁注:
而不是使用通知中心来通知关闭模态并允许模态自行解散,最好为模态创建协议。然后将视图控制器设置为委托,并从委托方法中解除模态控制器。

它肯定会按照您的方式工作,但它会强制您使用通知中心。另外,如果使用完成块,则在块完成它的过程之前,不存在将对象从内存中删除的风险。