Created
July 29, 2014 02:01
-
-
Save kwylez/05b735162ac588b980f7 to your computer and use it in GitHub Desktop.
Blur animation for Stasis' Cover Image View. This works for iOS8 only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// STSCoverImageViewController.m | |
// Stasis | |
// | |
// Created by Cory D. Wiles on 6/5/14. | |
// Copyright (c) 2014 Macspots. All rights reserved. | |
// | |
#import "STSCoverImageViewController.h" | |
#import "Profile.h" | |
#import "Profile+STSAdditions.h" | |
#import "STSCoverPhotoImageView.h" | |
#import "STSNavigationBar.h" | |
static CGFloat const STSSnapshotAnimationBlockDuration = 0.3f; | |
@interface STSCoverImageViewController () | |
@property (nonatomic, strong) Profile *profile; | |
@property (nonatomic, strong, readwrite) STSCoverPhotoImageView *coverImageView; | |
@property (nonatomic, assign, getter=isViewBlurred) BOOL viewBlurred; | |
@property (nonatomic, strong) MSBlurredEffectView *blurredView; | |
@end | |
@implementation STSCoverImageViewController | |
@synthesize viewBlurred = _viewBlurred; | |
- (instancetype)initWithProfile:(Profile *)profile { | |
// NSParameterAssert(profile != nil); | |
self = [super init]; | |
if (self) { | |
_profile = profile; | |
_viewBlurred = YES; | |
if (_profile.coverPhoto) { | |
UIImage *coverImage = [[UIImage alloc] initWithData:_profile.coverPhoto | |
scale:[UIScreen mainScreen].scale]; | |
_coverImageView = [[STSCoverPhotoImageView alloc] initWithImage:coverImage]; | |
UIColor *defaultNavigationBarColor = [_profile coverPhotoIsLight] ? [UIColor whiteColor] : [UIColor blackColor]; | |
NSDictionary *titleAttributes = @{NSForegroundColorAttributeName : defaultNavigationBarColor}; | |
[[STSNavigationBar appearance] setTitleTextAttributes:titleAttributes]; | |
[UIApplication reloadUIAppearance]; | |
} else { | |
_coverImageView = [[STSCoverPhotoImageView alloc] init]; | |
} | |
_blurredView = [[MSBlurredEffectView alloc] initWithFrame:_coverImageView.frame | |
blurEffectStyle:UIBlurEffectStyleLight]; | |
[_coverImageView addSubview:_blurredView.visualEffectView]; | |
} | |
return self; | |
} | |
- (void)updateCoverPhotoWithImage:(UIImage *)image | |
successBlock:(void (^)(BOOL success, NSError *error))block { | |
/** | |
* @note | |
* I might have to resave the profile | |
*/ | |
NSData *imageData = UIImagePNGRepresentation(image); | |
CGFloat screenScale = [UIScreen mainScreen].scale; | |
self.coverImageView.image = [[UIImage alloc] initWithData:imageData scale:screenScale]; | |
[self.profile updateCoverPhoto:imageData | |
withSuccessBlock:^(NSData *imageData){ | |
if (block) { | |
block(YES, nil); | |
} | |
} | |
errorBlock:^(NSError *error){ | |
if (block) { | |
block(NO, error); | |
} | |
}]; | |
} | |
- (void)toggleBlurForCoverView { | |
/** | |
* Create snapshot then animate the alpha | |
*/ | |
UIView *snapshotView = [self.coverImageView snapshotViewAfterScreenUpdates:YES]; | |
/** | |
* Block for removing snapshot view | |
*/ | |
dispatch_block_t snapshotToggleBlock = ^{ | |
[UIView animateWithDuration:STSSnapshotAnimationBlockDuration animations:^{ | |
snapshotView.alpha = 0.0f; | |
} completion:^(BOOL finished){ | |
[snapshotView removeFromSuperview]; | |
}]; | |
}; | |
[self.coverImageView addSubview:snapshotView]; | |
if (self.isViewBlurred) { | |
[self.blurredView.visualEffectView removeFromSuperview]; | |
snapshotToggleBlock(); | |
} else { | |
[self.coverImageView insertSubview:self.blurredView.visualEffectView | |
belowSubview:snapshotView]; | |
snapshotToggleBlock(); | |
} | |
self.viewBlurred = !self.viewBlurred; | |
} | |
#pragma mark - Accessors | |
- (BOOL)isViewBlurred { | |
return _viewBlurred; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment