Pergunta

i wanted to use animations to load a view from the bottom of the window, i know this can be done via presentmodalViewController but in as per the requirement of my app its not valid as i only want to load the view at half of the window. So i used Animations and heres a look at what i did

-(void) displayPicker
    {
        UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [done setFrame:CGRectMake(197, 199, 103, 37)];
        [done setBackgroundImage:[UIImage imageNamed:@"button_0.png"] forState:UIControlStateNormal];
        [done setTitle:@"Done" forState:UIControlStateNormal];
        [done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [done addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];

        [pickerDisplayView addSubview:datePicker];
        [pickerDisplayView addSubview:done];

    //animating here

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
        [self.view addSubview:pickerDisplayView];
        [UIView commitAnimations];

    }

The view called pickerDisplayView has two components called Picker and Button, but the problem is that Picker is not working correctly and neither the view (pickerDisplayView) is loading in a smooth way.


@Matt: I have followed ur instructions and did as u advised here's what i did

-(void) animationIn
{
    CGPoint p = {x:0,y:185};

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    //pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
    [pickerDisplayView setCenter:p];
    [UIView commitAnimations];

}

But the prob is that the view is going at the top of the window and is not coming at exact y axis of 185. I took these coordinates from IB. Please help me sir

Foi útil?

Solução

You will have to add the view offscreen and then animate it in.

// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];

[self.view addSubview:pickerDisplayView];
[self performSelector:@selector(animateIn) withObject:nil afterDelay:0.25];

// Declare animateIn
- (void)animateIn
{
    UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [pickerDisplayView setCenter:onscreenPosition];
    [UIView commitAnimations];
}

The value offscreenRect is a CGRect for the size and position where you want the view to start. onscreenPosition is a CGPoint for the position where you want the animation to finish. Animating the view's center is adequate since you don't want to change the size of the view while animating.

Also, I placed the animation in a separate method that gets called with -performSelector:withObject:afterDelay to ensure that the animation with fire. Sometimes you won't see the animation run if you start it in the same run loop where you added the view you're animating.

Outras dicas

You need to init and addSubview before beginAnimations:. When you init pickerDisplayView position it so that the top of it is inline with the bottom of the superview. The animation will then slide up pickerDisplayView. Ideally you should create the view in a nib or in the loadView method. (I strongly recommend using nibs over 'loadView').

Do this in loadView (or move to a nib):

CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];

This is the animation:

[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];

Also I'd advice against using 'magic numbers' in the frame. Instead you can derive the pickerDisplayView frame from the superviews frame. If you do use 'magic' numbers be sure explain them in some way (ie assigning them to a appropriately named variable or a comment).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top