TdoubleG
TdoubleG

Reputation: 489

Objective C How to animate Subview with click in Button

I have a problem :D.

I'm currently using a SplitViewController. At the start I want to hide the TableViewController in the MasterView. So i decided to add a SubView in the Storyboard into the tableView, where a little label will be shown.

So after I make my choice in the DetailView and click the Button I want to hide the subView and want to show the tableView. That all with animations.

So here is my Code:

In the TestTableViewController.m

-(void)AnimateView{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
fixedView.frame=CGRectMake(-500, 0,0,0);
[UIView commitAnimations];
NSLog(@"test");

And in the TestDetailViewController.m

- (IBAction)attractionButton:(UIButton *)sender
{
    TestTableViewController *testTableViewController = [[TestTableViewController alloc]init];
    [testTableViewController AnimateView];
}

I get the NSLog "test" but the Animation is not working. fixedView is my Subview and I drag it to the header file so it's an IBOutlet.

Thank you so far.

Upvotes: 0

Views: 1451

Answers (3)

Unheilig
Unheilig

Reputation: 16292

The problem is, you are not referencing the same master view controller in your details view controller.

You are creating another one in your IBAction.

In order to get a hold of your (the "original" one) master view controller, do this in your detail view controller:

Create a ivar:

TestTableViewController *masterViewController;

And then to reference it:

masterViewController = (TestTableViewController*)[[[self.splitViewController.viewControllers lastObject] viewControllers] objectAtIndex:0];

For example:

- (IBAction)attractionButton:(UIButton *)sender
{
    masterViewController = (TestTableViewController*)[[[self.splitViewController.viewControllers lastObject] viewControllers] objectAtIndex:0];
    [masterViewController AnimateView];
}

And finally in your AnimateView method, make change to the following:

[UIView animateWithDuration:0.1f animations:^
{
     fixedView.frame = CGRectMake(-500, 0, 0, 0);
}
completion:nil];

Hope this helps.

Upvotes: 0

JWKot
JWKot

Reputation: 360

What you could try is this:

[UIView animateWithDuration:0.15f animations:^{
     fixedView.frame=CGRectMake(-500, 0,0,0);
}completion:^(BOOL finished){

}];

Are you sure you can call [UIView beginAnimations: ....] without a first argument?

Upvotes: 0

tanz
tanz

Reputation: 2547

So you seem to instantiate a new ViewController and trying to perform this animation straight away:

TestTableViewController *testTableViewController = [[TestTableViewController alloc]init];
[testTableViewController AnimateView];

The problem is that you don't even present that view controller (its view is not on screen anywhere) so it's normal you don't see any animation.

From my understanding you already have another instance of this view controller presented on screen (am I right?) - so that is the one that you need to use. Pass its pointer to TestDetailViewController in some way (e.g. a property) and then use that object to call your animation method.

PS. I would recommend naming all your methods starting with a lower case letter ;)

Upvotes: 1

Related Questions