iOS 8 visual effects crash course

Add comment TC Tammy Coron Oct 14, 2014

iOS 8 Visual Effects Example App This tutorial will give you a crash course on giving your iOS 8 apps some added polish with UIBlurEffect, UIVibrancyEffect and UIVisualEffectView. The app we'll be building to walk through these visual effects pays homage to one of my favorite creative professionals, Tim Burton. Take a look above! Visit this Binpress Github repo for the full source code.

Adding visual effects may seem like a lot work, but thanks to iOS 8… it’s a lot easier. Using UIVisualEffectView, along with UIBlurEffect and UIVibrancyEffect, your apps can now include dynamic blurring effects with a very small amount of code.

Blur Effect

There are three types of blur effect styles available: 1) Light (UIBlurEffectStyleLight) 2) Extra Light (UIBlurEffectStyleExtraLight) 3) Dark (UIBlurEffectStyleDark)

Creating a UIBlurEffect and a UIVisualEffectView only requires a few lines of code:

// Blur Effect
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[bluredEffectView setFrame:self.selectedDetailsView.bounds];

[self.selectedDetailsView addSubview:bluredEffectView];

The first step is to create the blur effect using the desired style. Then, that effect is used to create the visual effect view. Finally, the effect view is added to the view hierarchy. Then it dynamically blurs the content beneath it.

Vibrancy Effect

Using a vibrancy effect, you can control how legible content appears over the blurred effect. Just like the blur effect, it only requires a few lines of code to implement:

// Vibrancy Effect
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
[vibrancyEffectView setFrame:self.selectedDetailsView.bounds];

// Add Label to Vibrancy View
[vibrancyEffectView.contentView addSubview:self.movieTitle];
[vibrancyEffectView.contentView addSubview:self.movieReleaseDate];

// Add Vibrancy View to Blur View
[bluredEffectView.contentView addSubview:vibrancyEffectView];

Keep in mind that views added to the contentView of the UIVisualEffectView will have this effect applied to them. Also, when creating a vibrancy effect, be sure to use the same blur effect that was used during the creation of the visual effect view. Failure to do so could result in unwanted visual effects.

Sample App

Taking a look at the sample app, you can see that MainViewController.m, has a method named btnChangeBlurEffect_tap:. This method gets called anytime the user changes the currently selected blur effect (which can be done using the wrench icon on the top-right of the header bar).

Likewise, the btnChangeVibrancyEffect_tap: method changes the vibrancy setting. This action is also available from the wrench icon on the top-right.

Another important method is setupEffectsForSelectionViewAndShowSelectionViewController. In this method, the currently selected blur and vibrancy is applied to the visual effect view.

- (void)setupEffectsForSelectionViewAndShowSelectionViewController
{
    // This sets up and displays the side panel selection view.

    [_bluredEffectView removeFromSuperview];
    [_label removeFromSuperview];

    if (_blurEffect)
    {
        self.selectionViewPrimary.backgroundColor = [UIColor clearColor];

        // Blur Effect
        _bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:_blurEffect];
        _bluredEffectView.tag = 111;
        [_bluredEffectView setFrame:self.selectionViewPrimary.bounds];
        [self.selectionViewPrimary addSubview:_bluredEffectView];

        if (_showWithVibrancy)
        {
            // Vibrancy Effect
            UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:_blurEffect];
            UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
            [vibrancyEffectView setFrame:self.selectionViewPrimary.bounds];

            // Add Label to Vibrancy View
            [vibrancyEffectView.contentView addSubview:_label];

            // Add Vibrancy View to Blur View
            [_bluredEffectView.contentView addSubview:vibrancyEffectView];
        }
        else
        {
            [self.selectionViewSecondary addSubview:_label];
        }

    }
    else
    {
        self.selectionViewPrimary.backgroundColor = [UIColor darkGrayColor];
        [self.selectionViewSecondary addSubview:_label];
    }

    [self.selectionViewPrimary addSubview:self.selectionViewSecondary];

    [self showSelectionView];
}

The last important piece of code in MainViewController.m is the bit in the prepareForSegue: sender: method. In this method, you are creating a screen shot and using that screen shot as your background image for the details view you are about to present.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];

    MoreDetailsViewController *detailViewController = segue.destinationViewController;

    detailViewController.transitioningDelegate = self;
    detailViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    detailViewController.view.backgroundColor = [UIColor clearColor];

    // There is a bug! this is a work around
    self.selectionViewPrimary.frame = CGRectMake(-113, 0, self.selectionViewPrimary.frame.size.width, self.selectionViewPrimary.frame.size.height);

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *copied = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    detailViewController.imageFromPreviousScreen = copied;
}

Finally, in MoreDetailsViewController.m, there is a setupView method. In this method you are creating another set of effects.

- (void)setupView
{
    self.selectedDetailsView.backgroundColor = [UIColor clearColor];

    self.movieTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, self.selectedDetailsView.frame.size.width, 19)];
    self.movieTitle.textColor = [UIColor lightTextColor];
    self.movieTitle.textAlignment = NSTextAlignmentCenter;

    self.movieReleaseDate = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.selectedDetailsView.frame.size.width, 19)];
    self.movieReleaseDate.textColor = [UIColor lightTextColor];
    self.movieReleaseDate.textAlignment = NSTextAlignmentCenter;

    self.movieDetails = [[UITextView alloc] initWithFrame:CGRectMake(0, 165, self.selectedDetailsView.frame.size.width, 345)];
    self.movieDetails.textColor = [UIColor lightTextColor];
    self.movieDetails.backgroundColor = [UIColor clearColor];
    self.movieDetails.font = [UIFont fontWithName:@"Helvetica" size:12];
    self.movieDetails.textContainerInset = UIEdgeInsetsMake(0, 12, 0, 12);
    self.movieDetails.editable = NO;

    // Blur Effect
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [bluredEffectView setFrame:self.selectedDetailsView.bounds];

    [self.selectedDetailsView addSubview:bluredEffectView];

    // Vibrancy Effect
    UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
    UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
    [vibrancyEffectView setFrame:self.selectedDetailsView.bounds];

    // Add Label to Vibrancy View
    [vibrancyEffectView.contentView addSubview:self.movieTitle];
    [vibrancyEffectView.contentView addSubview:self.movieReleaseDate];
    [vibrancyEffectView.contentView addSubview:self.movieDetails];

    // Add Vibrancy View to Blur View
    [bluredEffectView.contentView addSubview:vibrancyEffectView];

    long selectedItem = [[NSUserDefaults standardUserDefaults] integerForKey:@"_selectedItem"];
    [self setupSelectedDetails:selectedItem];
}

That’s it! That’s all there is to getting your app to look fantastic. For a more in-depth look at UIVisualEffectView, be sure to review the Apple Developer Documentation.

The full source code for this tutorial is available here.

0 comments


Or enter your name and Email
No comments have been posted yet.