Reputation: 373
I want to move my buttons and labels to specific locations when the view loads. At first everything is in the center and when it loads everything is supposed to expand to designated locations. I have this but the icons and labels don't move. It works if I make a button in the same view and put the code in it though.
#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
Upvotes: 0
Views: 346
Reputation: 31745
You shouldn't put geometry-related code in viewDIdLoad
as view frames are not yet set at that point. Also animation code makes no sense before the view is onscreen - try moving it to viewDidAppear
.
update
Judging by your efforts and comments, your issue is as much 'how to debug' as it is 'what's wrong with my code' (other than where you call it, i can't see any problem with your code!).
IN summary
- you have moved the code to a separate method (in fact you mention a "custom void statement " - I am not sure what you mean by that, use a method as per my code below...)
- you call the same method from viewDiDAppear
and from a button action with [self animate];
- called from viewDidAppear
, it doesn't run. Called from a button action, it does.
So you need to find out exactly where the code is failing.
First I suggest reducing your code to a minimum for testing. Animate just one object:
- (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];
}
Do a bit of logging before and after you call the method:
NSLog(@"pre-animated frame: %@",NSStringFromCGRect(homeIcon.frame);
[self animate];
NSLog(@"post-animated frame: %@",NSStringFromCGRect(homeIcon.frame);
From which you can tell whether the frame is getting set in the animaton method. You don't make it clear in your comments - with a failed animation is the frame also failing to get set? If it is just the animation that is failing do you have any other animation code running that may be interfering with it? Are you transitioning to this view in an unusual way?
Try calling the animation method from other methods, for example viewWillAppear:animated
and viewWillLayoutSubviews
. Are you certain that your viewDidAppear
method is definitely getting called?
Put this log statement at the start of all of your relevant methods:
NSLog(@"%s",__func__);
It will show you exactly which methods are called, when.
Try putting a breakpoint at the start of viewWillAppear
and step through, watching the variables to see what is getting set where. Is it just the animation that is failing?
If so try using an animation block to trigger the animations:
[UIView animateWithDuration:0.5 animations:^{
homeIcon.frame = framehi;
}];
(that shouldn't make the difference but its the more modern way)
You need to come up with some better clues as to the error because it is not in the code you have posted. As I said, I have tested your exact code in viewWillAppear
and viewDiDAppear
and it works fine. Perhaps your method signatures are not correct and they are not getting called?
When you get a better idea, if you still can't solve it, then either edit your question or start a new one. If you edit your question, add the exact test code you are using including method calls/signatures, log results.
But don't add results as comments, it's harder for other people to help out.
Good luck
Upvotes: 1
Reputation: 1299
Remove everything from viewDidLoad as for now and then change your code in viewDidAppear to the following:
- (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;
}];
}
Upvotes: 0