0

today I decided to make my own animations instead of using UINavigationController's ones; so I've designed an animation (afterwards pseudocode), however I stumbled when I started to write the code. I've thought it will be good if I can make a class hence I can share it.

Let me explain it with words at first, there are two view controllers, when pushing the latter view controller former view controller's view should be blurred, then latter's blurred image should come into. Former's blurred image should go hidden and finally the blur of the latter image should resolve to show the latter view controller properly.

I've got the former VC's image properly with [UIImage convertViewToImage], however I couldn't get the latter VC's image.

I have to push new VC if I want to get snapshot of it with same logic, but you may understand it doesn't seem optimized and practical.

Let me show the code:

- (void)makeLastBackgroundBlur
{
    self.lastBackgroundView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    UIImage *image = [[UIImage convertViewToImage] applyBlurWithRadius:5.0
                                                  tintColor:[UIColor colorWithWhite:0.2
                                                                              alpha:0.7]
                                      saturationDeltaFactor:1.8
                                                  maskImage:nil];

    self.lastBackgroundView.image = image;
    self.lastBackgroundView.alpha = 0;

    [self addSubview:self.lastBackgroundView];
}

- (void)makeNextBackgroundBlur
{
    self.nextBackgroundView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //    The part I couldn't do
    UIImage *image = [theImageOfTheLatterVC applyBlurWithRadius:5.0
                                                      tintColor:[UIColor colorWithWhite:0.2
                                                                                  alpha:0.7]
                                          saturationDeltaFactor:1.8
                                                      maskImage:nil];

    self.nextBackgroundView.image = image;
    self.nextBackgroundView.alpha = 1;
    self.nextBackgroundView.hidden = YES;

    [self addSubview:self.nextBackgroundView];
}    

- (void)invokeAnimation
{
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    //  Invoke the blurred background with animation
    [UIView animateWithDuration:(self.duration / 2.00)
                     animations:^{
                         self.lastBackgroundView.alpha = 1.0f;
                     }
                     completion:^(BOOL finished){
                         self.nextBackgroundView.hidden = NO;
                         [UIView animateWithDuration:(self.duration / 2.00)
                                      animations:^{
                                              self.nextBackgroundView.alpha = 0;
                                          } completion:^(BOOL finished){
                                              self.lastBackgroundView = nil;
                                          }];
                     }];
}
Buğra Ekuklu
  • 3,049
  • 2
  • 17
  • 28
  • Can you show the `convertViewToImage` method? Can you alter it to take an view as argument and pass it the latter VC's view? – robert Jul 29 '14 at 16:57
  • @robert I may but from the optimization aspect it doesn't seem eligible to me. I'm going to try taking a snapshot of latter VC manually and adding it as file, because latter VC will consist static cells only. I don't know what can be done else, it seems best solution to me. – Buğra Ekuklu Jul 29 '14 at 20:32
  • 1
    Ok, if it is possible to take the snapshot manually, that would definitly be the fastest solution performance wise. Keep in mind that you'll need prerendered snapshots for every device screen type you are supporting - that adds up ;) If it should be more dynamic, there is no way around creating the latter VC's view first and then render it to an image. If possible you want to do that in some place before the navigation is triggered to avoid delays... – robert Jul 29 '14 at 20:47

0 Answers0