动画帧从viewDidLoad更改

时间:2013-08-26 03:05:43

标签: ios cocoa-touch uiview uiviewcontroller

我希望在加载视图时将我的按钮和标签移动到特定位置。起初一切都在中心,当它加载时,一切都应该扩展到指定的位置。我有这个,但图标和标签不动。如果我在同一视图中创建一个按钮并将代码放入其中,它就可以工作。

     #import "DashViewController.h"

@interface DashViewController ()

@end

@implementation DashViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (IBAction)backb:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)loadanim:(id)sender{
    [self moveIcons];
}


-(void)moveIcons{

    CGRect framehi = homeIcon.frame;
    framehi.origin.x = 15;
    framehi.origin.y = 70;
    CGRect framehl = homeLabel.frame;
    framehl.origin.x = -8;
    framehl.origin.y = 136;

    CGRect framesmi = smediaIcon.frame;
    framesmi.origin.x = 131;
    framesmi.origin.y = 70;
    CGRect framesml = smediaLabel.frame;
    framesml.origin.x = 108;
    framesml.origin.y = 136;

    CGRect framemi = mediaIcon.frame;
    framemi.origin.x = 249;
    framemi.origin.y = 70;
    CGRect frameml = mediaLabel.frame;
    frameml.origin.x = 225;
    frameml.origin.y = 136;

    CGRect frameni = newsIcon.frame;
    frameni.origin.x = 15;
    frameni.origin.y = 211;
    CGRect framenl = newsLabel.frame;
    framenl.origin.x = -8;
    framenl.origin.y = 277;

    CGRect framebi = bullyIcon.frame;
    framebi.origin.x = 131;
    framebi.origin.y = 211;
    CGRect framebl = bullyLabel.frame;
    framebl.origin.x = 108;
    framebl.origin.y = 277;

    CGRect framespi = sportsIcon.frame;
    framespi.origin.x = 249;
    framespi.origin.y = 211;
    CGRect framespl = sportsLabel.frame;
    framespl.origin.x = 225;
    framespl.origin.y = 277;

    CGRect framesti = staffIcon.frame;
    framesti.origin.x = 15;
    framesti.origin.y = 355;
    CGRect framestl = staffLabel.frame;
    framestl.origin.x = -8;
    framestl.origin.y = 421;

    CGRect framehbi = handbookIcon.frame;
    framehbi.origin.x = 131;
    framehbi.origin.y = 355;
    CGRect framehbl = handbookLabel.frame;
    framehbl.origin.x = 108;
    framehbl.origin.y = 421;

    CGRect frameai = aboutIcon.frame;
    frameai.origin.x = 249;
    frameai.origin.y = 355;
    CGRect frameal = aboutLabel.frame;
    frameal.origin.x = 225;
    frameal.origin.y = 421;

    [UIView animateWithDuration:0.3 animations:^{
        homeIcon.frame = framehi;
        homeLabel.frame = framehl;
        smediaIcon.frame = framesmi;
        smediaLabel.frame = framesml;
        mediaIcon.frame = framemi;
        mediaLabel.frame = frameml;
        newsIcon.frame = frameni;
        newsLabel.frame = framenl;
        bullyIcon.frame = framebi;
        bullyLabel.frame = framebl;
        sportsIcon.frame = framespi;
        sportsLabel.frame = framespl;
        staffIcon.frame = framesti;
        staffLabel.frame = framestl;
        handbookIcon.frame = framehbi;
        handbookLabel.frame = framehbl;
        aboutIcon.frame = frameai;
        aboutLabel.frame = frameal;
    }];

}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:1)

您不应将与几何相关的代码放在viewDIdLoad中,因为此时尚未设置视图框。在视图显示在屏幕上之前,动画代码也没有意义 - 尝试将其移动到viewDidAppear

更新

根据您的努力和评论判断,您的问题与“我的代码有什么问题”一样多“如何调试”(除了您调用它之外,我看不到您的代码有任何问题!)

总结
- 您已将代码移至单独的方法(实际上您提到了“自定义无效声明” - 我不确定您的意思,根据我的代码使用方法...)
- 您从viewDiDAppear[self animate];的按钮操作调用相同的方法 - 从viewDidAppear调用,它不会运行。从按钮动作调用,它确实。

所以你需要找到完全代码失败的地方。

首先,我建议将代码减少到最低程度以便进行测试。仅为一个对象设置动画:

- (void) animate {
      CGRect framehi = homeIcon.frame;
      framehi.origin.x = 15;
      framehi.origin.y = 70;

     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration: 0.25];

     homeIcon.frame = framehi;

     [UIView commitAnimations];
}

在调用方法之前和之后进行一些记录:

   NSLog(@"pre-animated frame: %@",NSStringFromCGRect(homeIcon.frame);
   [self animate];
   NSLog(@"post-animated frame: %@",NSStringFromCGRect(homeIcon.frame);

您可以从中判断帧是否在动画方法中设置。你没有在你的评论中说清楚 - 动画失败是框架也未能设置?如果它只是失败的动画你是否有任何其他可能干扰它的动画代码?您是否以不寻常的方式转换到此视图?

尝试从其他方法调用动画方法,例如viewWillAppear:animatedviewWillLayoutSubviews。您某些您的viewDidAppear方法肯定会被调用吗?

将此日志语句放在所有相关方法的开头:       NSLog(@"%s",__func__); 它将在何时确切地显示调用哪些方法。

尝试在viewWillAppear的开头放置一个断点并逐步执行,观察变量以查看设置的位置。是只是失败的动画吗?

如果是这样,请尝试使用动画块来触发动画:

  [UIView animateWithDuration:0.5 animations:^{
          homeIcon.frame = framehi;
   }];

(这不应该有所不同,但它更现代的方式)

您需要提供一些关于错误的更好的线索,因为它不在您发布的代码中。正如我所说,我已经在viewWillAppearviewDiDAppear中测试了您的确切代码,但它运行正常。也许您的方法签名不正确并且没有被调用?

如果您有更好的想法,如果仍然无法解决问题,那么请编辑您的问题或开始新问题。如果您编辑问题,请添加您正在使用的确切测试代码,包括方法调用/签名,日志结果。

但是不要将结果添加为评论,其他人更难以帮助。

祝你好运

答案 1 :(得分:0)

立即从viewDidLoad中删除所有内容,然后将viewDidAppear中的代码更改为以下内容:

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

    CGRect framehi = homeIcon.frame;
    framehi.origin.x = 15;
    framehi.origin.y = 70;
    CGRect framehl = homeLabel.frame;
    framehl.origin.x = -8;
    framehl.origin.y = 136;

    CGRect framesmi = smediaIcon.frame;
    framesmi.origin.x = 131;
    framesmi.origin.y = 70;
    CGRect framesml = smediaLabel.frame;
    framesml.origin.x = 108;
    framesml.origin.y = 136;

    CGRect framemi = mediaIcon.frame;
    framemi.origin.x = 249;
    framemi.origin.y = 70;
    CGRect frameml = mediaLabel.frame;
    frameml.origin.x = 225;
    frameml.origin.y = 136;

    CGRect frameni = newsIcon.frame;
    frameni.origin.x = 15;
    frameni.origin.y = 211;
    CGRect framenl = newsLabel.frame;
    framenl.origin.x = -8;
    framenl.origin.y = 277;

    CGRect framebi = bullyIcon.frame;
    framebi.origin.x = 131;
    framebi.origin.y = 211;
    CGRect framebl = bullyLabel.frame;
    framebl.origin.x = 108;
    framebl.origin.y = 277;

    CGRect framespi = sportsIcon.frame;
    framespi.origin.x = 249;
    framespi.origin.y = 211;
    CGRect framespl = sportsLabel.frame;
    framespl.origin.x = 225;
    framespl.origin.y = 277;

    CGRect framesti = staffIcon.frame;
    framesti.origin.x = 15;
    framesti.origin.y = 355;
    CGRect framestl = staffLabel.frame;
    framestl.origin.x = -8;
    framestl.origin.y = 421;

    CGRect framehbi = handbookIcon.frame;
    framehbi.origin.x = 131;
    framehbi.origin.y = 355;
    CGRect framehbl = handbookLabel.frame;
    framehbl.origin.x = 108;
    framehbl.origin.y = 421;

    CGRect frameai = aboutIcon.frame;
    frameai.origin.x = 249;
    frameai.origin.y = 355;
    CGRect frameal = aboutLabel.frame;
    frameal.origin.x = 225;
    frameal.origin.y = 421;

    [UIView animateWithDuration:0.3 animations:^{
        homeIcon.frame = framehi;
        homeLabel.frame = framehl;
        smediaIcon.frame = framesmi;
        smediaLabel.frame = framesml;
        mediaIcon.frame = framemi;
        mediaLabel.frame = frameml;
        newsIcon.frame = frameni;
        newsLabel.frame = framenl;
        bullyIcon.frame = framebi;
        bullyLabel.frame = framebl;
        sportsIcon.frame = framespi;
        sportsLabel.frame = framespl;
        staffIcon.frame = framesti;
        staffLabel.frame = framestl;
        handbookIcon.frame = framehbi;
        handbookLabel.frame = framehbl;
        aboutIcon.frame = frameai;
        aboutLabel.frame = frameal;
    }];
}