Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lminhtm committed Nov 1, 2016
1 parent 5398e91 commit 194a9bd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 70 deletions.
5 changes: 5 additions & 0 deletions LMDropdownView/LMDropdownView.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ typedef NS_ENUM(NSUInteger, LMDropdownViewDirection) {
*/
@property (nonatomic, assign) CGFloat closedScale;

/**
A boolean indicates whether container view should be blurred. Default is YES
*/
@property (nonatomic, assign) BOOL shouldBlurContainerView;

/*!
* The blur radius of container view.
*/
Expand Down
125 changes: 59 additions & 66 deletions LMDropdownView/LMDropdownView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#import "LMDropdownView.h"
#import "UIImage+LMExtension.h"

#define frx(a) (a.frame.origin.x)
#define fry(a) (a.frame.origin.y)
#define midx(a) (CGRectGetMidX(a.frame))
#define midy(a) (CGRectGetMidY(a.frame))
#define W(a) (a.frame.size.width)
#define H(a) (a.frame.size.height)

#define kDefaultClosedScale 0.85
#define kDefaultBlurRadius 5
#define kDefaultBlackMaskAlpha 0.5
Expand Down Expand Up @@ -41,8 +48,10 @@ + (instancetype)dropdownView
- (id)init
{
self = [super init];
if (self) {
if (self)
{
_closedScale = kDefaultClosedScale;
_shouldBlurContainerView = YES;
_blurRadius = kDefaultBlurRadius;
_blackMaskAlpha = kDefaultBlackMaskAlpha;
_animationDuration = kDefaultAnimationDuration;
Expand All @@ -65,6 +74,19 @@ - (void)dealloc
}


#pragma mark - PROPERTIES

- (void)setClosedScale:(CGFloat)closedScale
{
_closedScale = MIN(MAX(closedScale, 0.1), 1);
}

- (void)setBlackMaskAlpha:(CGFloat)blackMaskAlpha
{
_blackMaskAlpha = MIN(MAX(blackMaskAlpha, 0), 0.9);
}


#pragma mark - PUBLIC METHOD

- (BOOL)isOpen
Expand Down Expand Up @@ -184,7 +206,10 @@ - (void)setupContentView:(UIView *)contentView inView:(UIView *)containerView at
CGFloat scale = (3 - 2 * self.closedScale);
CGSize capturedSize = CGSizeMake(containerSize.width * scale, containerSize.height * scale);
UIImage *capturedImage = [UIImage imageFromView:containerView withSize:capturedSize];
UIImage *blurredCapturedImage = [capturedImage blurredImageWithRadius:self.blurRadius iterations:5 tintColor:[UIColor clearColor]];
UIImage *containerImage = capturedImage;
if (self.shouldBlurContainerView) {
containerImage = [capturedImage blurredImageWithRadius:self.blurRadius iterations:5 tintColor:[UIColor clearColor]];
}

/*!
* Main View
Expand All @@ -204,11 +229,8 @@ - (void)setupContentView:(UIView *)contentView inView:(UIView *)containerView at
self.containerWrapperView.backgroundColor = [UIColor blackColor];
self.containerWrapperView.contentMode = UIViewContentModeCenter;
}
self.containerWrapperView.image = blurredCapturedImage;
self.containerWrapperView.bounds = CGRectMake(0,
0,
capturedSize.width,
capturedSize.height);
self.containerWrapperView.image = containerImage;
self.containerWrapperView.bounds = CGRectMake(0, 0, capturedSize.width, capturedSize.height);
self.containerWrapperView.center = self.mainView.center;
[self.mainView addSubview:self.containerWrapperView];

Expand All @@ -235,23 +257,17 @@ - (void)setupContentView:(UIView *)contentView inView:(UIView *)containerView at
CGFloat contentWrapperViewHeight = CGRectGetHeight(contentView.frame) + self.animationBounceHeight;
switch (self.direction) {
case LMDropdownViewDirectionTop:
contentView.frame = CGRectMake(0,
self.animationBounceHeight,
CGRectGetWidth(contentView.frame),
CGRectGetHeight(contentView.frame));
contentView.frame = CGRectMake(0, self.animationBounceHeight, W(contentView), H(contentView));
self.contentWrapperView.frame = CGRectMake(origin.x,
origin.y - contentWrapperViewHeight,
CGRectGetWidth(contentView.frame),
W(contentView),
contentWrapperViewHeight);
break;
case LMDropdownViewDirectionBottom:
contentView.frame = CGRectMake(0,
0,
CGRectGetWidth(contentView.frame),
CGRectGetHeight(contentView.frame));
contentView.frame = CGRectMake(0, 0, W(contentView), H(contentView));
self.contentWrapperView.frame = CGRectMake(origin.x,
origin.y + contentWrapperViewHeight,
CGRectGetWidth(contentView.frame),
W(contentView),
contentWrapperViewHeight);
break;
default:
Expand All @@ -263,19 +279,12 @@ - (void)setupContentView:(UIView *)contentView inView:(UIView *)containerView at
/*!
* Set up origin, destination content center
*/
originContentCenter = CGPointMake(CGRectGetMidX(self.contentWrapperView.frame),
CGRectGetMidY(self.contentWrapperView.frame));
switch (self.direction) {
case LMDropdownViewDirectionTop:
desContentCenter = CGPointMake(CGRectGetMidX(self.contentWrapperView.frame),
origin.y + contentWrapperViewHeight/2 - self.animationBounceHeight);
break;
case LMDropdownViewDirectionBottom:
desContentCenter = CGPointMake(CGRectGetMidX(self.contentWrapperView.frame),
origin.y + contentWrapperViewHeight/2);
break;
default:
break;
originContentCenter = CGPointMake(midx(self.contentWrapperView), midy(self.contentWrapperView));
if (self.direction == LMDropdownViewDirectionTop) {
desContentCenter = CGPointMake(midx(self.contentWrapperView), origin.y + contentWrapperViewHeight/2 - self.animationBounceHeight);
}
else {
desContentCenter = CGPointMake(midx(self.contentWrapperView), origin.y + contentWrapperViewHeight/2);
}
}

Expand All @@ -297,19 +306,13 @@ - (void)backgroundButtonTapped:(id)sender
[self hide];
}

- (void)setClosedScale:(CGFloat)closedScale
{
_closedScale = MIN(closedScale, 1);
}


#pragma mark - KEYFRAME ANIMATION

- (void)addContentAnimationForState:(LMDropdownViewState)state
{
CAKeyframeAnimation *contentBounceAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
contentBounceAnim.duration = self.animationDuration;
contentBounceAnim.delegate = self;
contentBounceAnim.removedOnCompletion = NO;
contentBounceAnim.fillMode = kCAFillModeForwards;
contentBounceAnim.values = [self contentPositionValuesForState:state];
Expand All @@ -324,7 +327,6 @@ - (void)addContainerAnimationForState:(LMDropdownViewState)state
{
CAKeyframeAnimation *containerScaleAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
containerScaleAnim.duration = self.animationDuration;
containerScaleAnim.delegate = self;
containerScaleAnim.removedOnCompletion = NO;
containerScaleAnim.fillMode = kCAFillModeForwards;
containerScaleAnim.values = [self containerTransformValuesForState:state];
Expand All @@ -340,47 +342,38 @@ - (void)addContainerAnimationForState:(LMDropdownViewState)state

- (NSArray *)contentPositionValuesForState:(LMDropdownViewState)state
{
CGFloat positionX = self.contentWrapperView.layer.position.x;
CGFloat positionY = self.contentWrapperView.layer.position.y;
CGPoint currentContentCenter = self.contentWrapperView.layer.position;

NSMutableArray *values = [[NSMutableArray alloc] init];
[values addObject:[NSValue valueWithCGPoint:self.contentWrapperView.layer.position]];
NSMutableArray *values = [NSMutableArray new];
[values addObject:[NSValue valueWithCGPoint:currentContentCenter]];

if (state == LMDropdownViewStateWillOpen || state == LMDropdownViewStateDidOpen)
{
switch (self.direction) {
case LMDropdownViewDirectionTop:
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, desContentCenter.y + self.animationBounceHeight)]];
break;
case LMDropdownViewDirectionBottom:
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, desContentCenter.y - self.animationBounceHeight)]];
break;
default:
break;
if (self.direction == LMDropdownViewDirectionTop) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, desContentCenter.y + self.animationBounceHeight)]];
}
else {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, desContentCenter.y - self.animationBounceHeight)]];
}
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, desContentCenter.y)]];
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, desContentCenter.y)]];
}
else
{
switch (self.direction) {
case LMDropdownViewDirectionTop:
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, positionY + self.animationBounceHeight)]];
break;
case LMDropdownViewDirectionBottom:
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, positionY - self.animationBounceHeight)]];
break;
default:
break;
if (self.direction == LMDropdownViewDirectionTop) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, currentContentCenter.y + self.animationBounceHeight)]];
}
else {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, currentContentCenter.y - self.animationBounceHeight)]];
}
[values addObject:[NSValue valueWithCGPoint:CGPointMake(positionX, originContentCenter.y)]];
[values addObject:[NSValue valueWithCGPoint:CGPointMake(currentContentCenter.x, originContentCenter.y)]];
}

return values;
}

- (NSArray *)contentKeyTimesForState:(LMDropdownViewState)state
{
NSMutableArray *keyTimes = [[NSMutableArray alloc] init];
NSMutableArray *keyTimes = [NSMutableArray new];
[keyTimes addObject:[NSNumber numberWithFloat:0]];
[keyTimes addObject:[NSNumber numberWithFloat:0.5]];
[keyTimes addObject:[NSNumber numberWithFloat:1]];
Expand All @@ -389,7 +382,7 @@ - (NSArray *)contentKeyTimesForState:(LMDropdownViewState)state

- (NSArray *)contentTimingFunctionsForState:(LMDropdownViewState)state
{
NSMutableArray *timingFunctions = [[NSMutableArray alloc] init];
NSMutableArray *timingFunctions = [NSMutableArray new];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
return timingFunctions;
Expand All @@ -399,7 +392,7 @@ - (NSArray *)containerTransformValuesForState:(LMDropdownViewState)state
{
CATransform3D transform = self.containerWrapperView.layer.transform;

NSMutableArray *values = [[NSMutableArray alloc] init];
NSMutableArray *values = [NSMutableArray new];
[values addObject:[NSValue valueWithCATransform3D:transform]];

if (state == LMDropdownViewStateWillOpen || state == LMDropdownViewStateDidOpen)
Expand All @@ -420,7 +413,7 @@ - (NSArray *)containerTransformValuesForState:(LMDropdownViewState)state

- (NSArray *)containerKeyTimesForState:(LMDropdownViewState)state
{
NSMutableArray *keyTimes = [[NSMutableArray alloc] init];
NSMutableArray *keyTimes = [NSMutableArray new];
[keyTimes addObject:[NSNumber numberWithFloat:0]];
[keyTimes addObject:[NSNumber numberWithFloat:0.5]];
[keyTimes addObject:[NSNumber numberWithFloat:1]];
Expand All @@ -429,7 +422,7 @@ - (NSArray *)containerKeyTimesForState:(LMDropdownViewState)state

- (NSArray *)containerTimingFunctionsForState:(LMDropdownViewState)state
{
NSMutableArray *timingFunctions = [[NSMutableArray alloc] init];
NSMutableArray *timingFunctions = [NSMutableArray new];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
return timingFunctions;
Expand Down
26 changes: 22 additions & 4 deletions LMDropdownViewDemo/LMDropdownViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
28575FB51C6212EE007C306A /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 28575FB41C6212EE007C306A /* Launch Screen.storyboard */; settings = {ASSET_TAGS = (); }; };
28575FB51C6212EE007C306A /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 28575FB41C6212EE007C306A /* Launch Screen.storyboard */; };
5CB1F30F1B5B6AEF001886C3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5CB1F30E1B5B6AEF001886C3 /* Main.storyboard */; };
5CB1F3151B5B70C4001886C3 /* LMNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5CB1F3141B5B70C4001886C3 /* LMNavigationController.m */; };
626CE7D81976240A0086888F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 626CE7D71976240A0086888F /* Foundation.framework */; };
Expand Down Expand Up @@ -188,8 +188,14 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = LM;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = LMinh;
TargetAttributes = {
626CE7D31976240A0086888F = {
DevelopmentTeam = 6HXC4UA9AV;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 626CE7CF1976240A0086888F /* Build configuration list for PBXProject "LMDropdownViewDemo" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -264,14 +270,19 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -303,13 +314,18 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -328,10 +344,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 6HXC4UA9AV;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "LMDropdownViewDemo/LMDropdownViewDemo-Prefix.pch";
INFOPLIST_FILE = "LMDropdownViewDemo/LMDropdownViewDemo-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = com.lminhtm.LMDropdownView;
PRODUCT_BUNDLE_IDENTIFIER = com.lminhtm.lmdropdownviewdemo;
PRODUCT_NAME = LMDropdown;
PROVISIONING_PROFILE = "";
WRAPPER_EXTENSION = app;
Expand All @@ -344,10 +361,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 6HXC4UA9AV;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "LMDropdownViewDemo/LMDropdownViewDemo-Prefix.pch";
INFOPLIST_FILE = "LMDropdownViewDemo/LMDropdownViewDemo-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = com.lminhtm.LMDropdownView;
PRODUCT_BUNDLE_IDENTIFIER = com.lminhtm.lmdropdownviewdemo;
PRODUCT_NAME = LMDropdown;
PROVISIONING_PROFILE = "";
WRAPPER_EXTENSION = app;
Expand Down
2 changes: 2 additions & 0 deletions LMDropdownViewDemo/LMDropdownViewDemo/LMMenuCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ @implementation LMMenuCell

- (void)awakeFromNib
{
[super awakeFromNib];

self.selectionStyle = UITableViewCellSelectionStyleNone;
}

Expand Down

0 comments on commit 194a9bd

Please sign in to comment.