Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Composite view for segregating views for states
typedef void(^WGStateTransitionPerformer)(UIView *parent, UIView *sourceView, UIView *destinationView, BOOL animated);
@interface WGStateTransitioningView : UIView
- (void)registerPerformer:(WGStateTransitionPerformer)performer toState:(NSUInteger)state;
- (void)registerView:(UIView *)view forState:(NSUInteger)state;
- (void)transitionToState:(NSUInteger)state animated:(BOOL)animated;
#import "WGStateTransitioningView.h"
@interface WGStateTransitioningView ()
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, WGStateTransitionPerformer> *performers;
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIView *> *views;
@property (nonatomic, strong) UIView *currentView;
@implementation WGStateTransitioningView
#pragma mark - lifecycle
- (void)awakeFromNib {
NSAssert(NO, @"This view is not intended to be nib-instantiated. Consider adding appropriate logic to handle initial setup / final reset to initWithFrame:");
[super awakeFromNib];
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupStorage];
return self;
- (void)setupStorage {
self.performers = [NSMutableDictionary new];
self.views = [NSMutableDictionary new];
#pragma mark - public
- (void)registerPerformer:(WGStateTransitionPerformer)performer toState:(NSUInteger)state {
self.performers[@(state)] = performer;
- (void)registerView:(UIView *)view forState:(NSUInteger)state {
self.views[@(state)] = view;
- (void)transitionToState:(NSUInteger)state animated:(BOOL)animated {
WGStateTransitionPerformer performer = self.performers[@(state)];
NSAssert(performer, @"no performer registered for transition to state: %@", @(state));
UIView *destinationView = self.views[@(state)];
NSAssert(destinationView, @"no view registered for state: %@", @(state));
performer(self, self.currentView, destinationView, animated);
self.currentView = destinationView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment