Last active
May 23, 2017 15:30
-
-
Save mnmaraes/9458421 to your computer and use it in GitHub Desktop.
Gist that seeks to emulate Paper's menu animation
This file contains 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
// | |
// TUIViewController.m | |
// TestUIs | |
// | |
// Created by Murillo Nicacio de Maraes on 3/9/14. | |
// Copyright (c) 2014 m-one. All rights reserved. | |
// | |
#import "TUIViewController.h" | |
@interface TUIViewController () | |
@property (nonatomic) UIView *topLineView; | |
@property (nonatomic) UIView *centerLineView; | |
@property (nonatomic) UIView *bottomLineView; | |
@property (nonatomic) UIView *containerView; | |
@property (nonatomic) BOOL displayingMenu; | |
@end | |
@implementation TUIViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self viewSetUp]; | |
} | |
-(void)viewSetUp | |
{ | |
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(morph:)]; | |
[self.containerView addGestureRecognizer:tapRecognizer]; | |
[self.containerView addSubview:self.topLineView]; | |
[self.containerView addSubview:self.centerLineView]; | |
[self.containerView addSubview:self.bottomLineView]; | |
[self.view addSubview:self.containerView]; | |
} | |
#pragma mark - Where The Magic Happens | |
-(void)morph:(UITapGestureRecognizer *)tapRecognizer | |
{ | |
if (self.displayingMenu) { | |
[self morphToLine]; | |
} else { | |
[self morphToX]; | |
} | |
self.displayingMenu = !self.displayingMenu; | |
} | |
-(void)morphToX | |
{ | |
CGFloat angle = M_PI_4; | |
CGPoint center = CGPointMake(120., 120.); | |
__weak TUIViewController *weakSelf = self; | |
[UIView animateWithDuration:0.8 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ | |
TUIViewController *strongSelf = weakSelf; | |
strongSelf.topLineView.transform = CGAffineTransformMakeRotation(-angle*5); | |
strongSelf.topLineView.center = center; | |
strongSelf.bottomLineView.transform = CGAffineTransformMakeRotation(angle*5); | |
strongSelf.bottomLineView.center = center; | |
strongSelf.centerLineView.transform = CGAffineTransformMakeScale(0., 1.0); | |
} completion:^(BOOL finished) { | |
}]; | |
} | |
-(void)morphToLine | |
{ | |
__weak TUIViewController *weakSelf = self; | |
[UIView animateWithDuration:0.8 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ | |
TUIViewController *strongSelf = weakSelf; | |
strongSelf.topLineView.transform = CGAffineTransformIdentity; | |
strongSelf.topLineView.center = CGPointMake(120., 2.); | |
strongSelf.bottomLineView.transform = CGAffineTransformIdentity; | |
strongSelf.bottomLineView.center = CGPointMake(120., 238.); | |
strongSelf.centerLineView.transform = CGAffineTransformIdentity; | |
} completion:^(BOOL finished) { | |
}]; | |
} | |
#pragma mark - Acessors | |
-(UIView *)topLineView | |
{ | |
if (!_topLineView) { | |
_topLineView = [[UIView alloc] initWithFrame:CGRectMake(0., 0., 240, 4.)]; | |
_topLineView.backgroundColor = [UIColor blackColor]; | |
} | |
return _topLineView; | |
} | |
-(UIView *)centerLineView | |
{ | |
if (!_centerLineView) { | |
_centerLineView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 120., 240, 4.)]; | |
_centerLineView.backgroundColor = [UIColor blackColor]; | |
} | |
return _centerLineView; | |
} | |
-(UIView *)bottomLineView | |
{ | |
if (!_bottomLineView) { | |
_bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 240., 240., 4.)]; | |
_bottomLineView.backgroundColor = [UIColor blackColor]; | |
} | |
return _bottomLineView; | |
} | |
-(UIView *)containerView | |
{ | |
if (!_containerView) { | |
_containerView = [UIView new]; | |
_containerView.bounds = CGRectMake(0.0, 0.0, 240., 240.); | |
_containerView.center = self.view.center; | |
} | |
return _containerView; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment