0

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

     #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
4

2 回答 2

1

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

更新

从您的努力和评论来看,您的问题既是“如何调试”,也是“我的代码出了什么问题”(除了您调用它的地方,我看不出您的代码有任何问题!)。

总而言之
-您已将代码移至单独的方法(实际上您提到了“自定义无效语句”-我不确定您的意思,请按照下面的代码使用方法...)
-您调用来自viewDiDAppear和来自按钮操作的相同方法[self animate]; - 调用 from 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;
   }];

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

您需要就错误找到一些更好的线索,因为它不在您发布的代码中。正如我所说,我已经测试了您的确切代码viewWillAppear并且viewDiDAppear它工作正常。也许您的方法签名不正确并且没有被调用?

当你有了更好的想法时,如果你仍然无法解决它,那么要么编辑你的问题,要么开始一个新的问题。如果您编辑您的问题,请添加您正在使用的确切测试代码,包括方法调用/签名、记录结果。

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

祝你好运

于 2013-08-26T03:19:49.183 回答
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;
    }];
}
于 2013-08-26T04:54:54.647 回答