Exothug
Exothug

Reputation: 339

How do I animate my subviews on adding them?

I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.

But they just kind of pop in which looks sloppy.

is there a way to animate them?

here is the method that adds them (it's called on clicking the button) :

-(void)showPicker
{
    [self.view addSubview:_fontPicker];
    [self.view addSubview:pickerTB];

}

Upvotes: 1

Views: 1409

Answers (4)

MZimmerman6
MZimmerman6

Reputation: 8623

You can animate them in a couple of ways, but probably the easiest is to just fade them in.

Fade In

[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations]; 

Fade Out

float fadeDuration = 0.5;
[UIView beginAnimations:@"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:@selector(removeView)];
[UIView commitAnimations];


-(void) removeView {
    [someView removeFromSuperview];
}

Something as simple as that.

Upvotes: 2

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Initially set your picker view frame and tool bar frame like this one..

-(void)viewDidLoad
{
    [super viewDidLoad];
    pickerTB.frame = CGRectMake(0,568,320,44);
    fontPicker.frame = CGRectMake(0, 568, 320, 216);
}

-(void)showPicker
{
      [UIView beginAnimations:nil context:nil];
      [UIView setAnimationDuration:.30];
       if(isiPhone5)
       {
            pickerTB.frame = CGRectMake(0,288,320,44);
            fontPicker.frame = CGRectMake(0, 332, 320, 216);
        }else{

        pickerTB.frame = CGRectMake(0,200,320,44);
        fontPicker.frame = CGRectMake(0,244, 320, 216);
        }
    [UIView commitAnimations];
}

Upvotes: 0

Ravi_Parmar
Ravi_Parmar

Reputation: 12329

You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.

-(void) showPicker{

[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0 
                 animations:^{
                     geopointView.frame = // its final location
                 }];
}

Upvotes: 0

JohnVanDijk
JohnVanDijk

Reputation: 3581

Depends on what type of animation you want to use. For example, this would look like dissolve. Anyways look into UIView animateWithDuration...

pickerTB.alpha = _fontPicker.alpha = 0;  
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];

You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)

Upvotes: 0

Related Questions