简体   繁体   中英

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.

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 (eg 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 ;)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM