Skip to content

Instantly share code, notes, and snippets.

@mnmaraes
Last active June 29, 2016 10:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnmaraes/9529586 to your computer and use it in GitHub Desktop.
Save mnmaraes/9529586 to your computer and use it in GitHub Desktop.
Gist that seeks to emulate a color mask.
//
// TUIMaskViewController.m
// TestUIs
//
// Created by Murillo Nicacio de Maraes on 3/13/14.
// Copyright (c) 2014 m-one. All rights reserved.
//
#import "TUIMaskViewController.h"
@interface TUIMaskViewController () <UIScrollViewDelegate>
#pragma mark - UIView
@property (nonatomic) UIView *bottomView;
@property (nonatomic) UIView *topView;
@property (nonatomic) UIScrollView *maskView;
#pragma mark - View Properties
@property (nonatomic) CGFloat topCenterInitialX;
@end
@implementation TUIMaskViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpView];
}
-(void)setUpView
{
UIView *clipperView = [[UIView alloc] initWithFrame:self.view.frame];
clipperView.clipsToBounds = YES;
[self.view addSubview:self.bottomView];
[clipperView addSubview:self.topView];
[self.maskView addSubview:clipperView];
[self.view addSubview:self.maskView];
}
#pragma mark - UIScroll Delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat xDelta = scrollView.contentOffset.x;
self.topView.center = CGPointMake(self.topCenterInitialX + xDelta, self.topView.center.y);
}
#pragma mark - Accessors
-(UIView *)bottomView
{
if (!_bottomView) {
_bottomView = [self createTemplateView];
_bottomView.backgroundColor = [UIColor blackColor];
((UIView *)[_bottomView.subviews firstObject]).backgroundColor = [UIColor whiteColor];
}
return _bottomView;
}
-(UIView *)topView
{
if (!_topView) {
_topView = [self createTemplateView];
_topView.backgroundColor = [UIColor whiteColor];
((UIView *)[_topView.subviews firstObject]).backgroundColor = [UIColor blackColor];
self.topCenterInitialX = _topView.center.x;
}
return _topView;
}
-(UIScrollView *)maskView
{
if (!_maskView) {
_maskView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
_maskView.contentSize = CGSizeMake(2 * CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
_maskView.pagingEnabled = YES;
_maskView.bounces = NO;
_maskView.showsHorizontalScrollIndicator = NO;
_maskView.delegate = self;
}
return _maskView;
}
#pragma mark - View Creation
-(UIView *)createTemplateView
{
UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
UIView *contentView = [UIView new];
contentView.bounds = CGRectMake(0.0, 0.0, 280., 40.);
contentView.center = containerView.center;
[containerView addSubview:contentView];
return containerView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment