Skip to content

Instantly share code, notes, and snippets.

@dimitribouniol
Last active December 14, 2015 15:08
Show Gist options
  • Save dimitribouniol/5105081 to your computer and use it in GitHub Desktop.
Save dimitribouniol/5105081 to your computer and use it in GitHub Desktop.
Easily create animatable properties without needing to do the work twice when you don't want them animated!
//
// UIView+MDConditionalAnimations.h
// MDConditionalAnimations
//
// Created by Dimitri Bouniol on 3/6/13.
// Copyright (c) 2013 Dimitri Bouniol. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (MDConditionalAnimations)
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
@end
//
// UIView+MDConditionalAnimations.m
// MDConditionalAnimations
//
// Created by Dimitri Bouniol on 3/6/13.
// Copyright (c) 2013 Dimitri Bouniol. All rights reserved.
//
#import "UIView+MDConditionalAnimations.h"
@implementation UIView (MDConditionalAnimations)
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
{
if (animate) {
[self animateWithDuration:duration delay:delay options:options animations:animations completion:completion];
} else {
animations();
if (completion)
completion(YES);
}
}
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
{
if (animate) {
[self animateWithDuration:duration animations:animations completion:completion];
} else {
animations();
if (completion)
completion(YES);
}
}
+ (void)conditionallyAnimate:(BOOL)animate withDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
{
if (animate) {
[self animateWithDuration:duration animations:animations];
} else {
animations();
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment