简体   繁体   中英

Heavy initialization causes UIView animation to not animate at first try?

Considering that my _palette's frame is like this:

_palette.frame = CGRectMake(0,480,320,200);

I have this code here to slide up/down a UIView:

if(![_pallete superview]) {
[self.view addSubview:_pallete];
[self.view insertSubview:_tempViewPaletteListener belowSubview:_pallete];

[UIView animateWithDuration:0.3 
                 animations:^{
                   _pallete.top -= kPaletteHeight;
                 } 
                 completion:^(BOOL isFinished) {

                 }];

 } else {

[UIView animateWithDuration:0.3 
                 animations:^{
                   _pallete.top += kPaletteHeight;
                 } 
                 completion:^(BOOL isFinished) {
                   [_tempViewPaletteListener removeFromSuperview];
                   [_pallete removeFromSuperview];
                 }];
 }

*the _tempViewPaletteListener is just a view with a tap gesture use to dismiss the palette*

The problem is when I first try to run code here, the _palette view will just stiffly display right away. What I expected is, it should slide up the _palette view.

Though it works fine after the first try

Update:

Changed question title from "UIView animation does not animate at first try?" into "Heavy initialization causes UIView animation to not animate at first try?"

My Code above works fine it's just that before calling the animation I layout the palette view and add in RSColorPicker

- (void)layoutPaletteView {

  if(!_colorPicker && !_brightnessSlider) {

    // Create Color Picker and Brightness Slider
  }

  if(!_pallete) {
    // Style the Palette View and add color picker and brightness slider as subview
  }
}

and I noticed that when I dont add a color picker and just a plain view to animate, the animation works fine (of course as simple as that, it should)..

The problem is the creation of color picker is just too heavy that i guess, is the cause why it cant show the animation of sliding up

As borrrden has said in the comments, it looks like there is no animation to display because when you add _pallete to the view it is already in the finished animation position: Try this:

if(![_pallete superview]) {
    _pallete.top += kPaletteHeight;

    [self.view addSubview:_pallete];
    [self.view insertSubview:_tempViewPaletteListener belowSubview:_pallete];

    [UIView animateWithDuration:0.3 
                     animations:^{
                         _pallete.top -= kPaletteHeight;
                     } 
                     completion:^(BOOL isFinished) {

                     }];

} else {

    [UIView animateWithDuration:0.3 
                     animations:^{
                         _pallete.top += kPaletteHeight;
                     } 
                     completion:^(BOOL isFinished) {
                         [_tempViewPaletteListener removeFromSuperview];
                         [_pallete removeFromSuperview];
                     }];
}

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