Skip to content

Commit

Permalink
Added Delegate implementation
Browse files Browse the repository at this point in the history
Added delegarte implementation to be compatible with Swift 2.0
  • Loading branch information
balram3429 committed Nov 13, 2015
1 parent ad62b8d commit b06280d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 45 deletions.
Binary file not shown.
Expand Up @@ -10,11 +10,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "btSimplePopUp/btSimplePopUP.m"
timestampString = "423589645.072561"
timestampString = "469098204.425585"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "167"
endingLineNumber = "167"
startingLineNumber = "171"
endingLineNumber = "171"
landmarkName = "-initWithImage:andFrame:onCompletion:"
landmarkType = "5">
</BreakpointContent>
Expand Down
63 changes: 55 additions & 8 deletions btCustomPopUP/btHomeViewController.m
Expand Up @@ -12,12 +12,12 @@
#import "btHomeViewController.h"
#import "btSimplePopUP.h"

@interface btHomeViewController ()
@property(nonatomic, retain) btSimplePopUP *popUp;
@interface btHomeViewController ()<btSimplePopUpDelegate>
@property(nonatomic, retain) btSimplePopUP *popUp, *popUpWithDelegate;
@end

@implementation btHomeViewController
@synthesize popUp;
@synthesize popUp, popUpWithDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
Expand Down Expand Up @@ -57,15 +57,50 @@ - (void)viewDidLoad
author.layer.cornerRadius = author.frame.size.width/2;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 70, 200, 40);
button.center = self.view.center;
button.frame = CGRectMake(self.view.bounds.size.width/2-100, 130, 200, 40);
[button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"DIN Condensed" size:65]];
[button setTitle:@"PopUp" forState:UIControlStateNormal];

[button.titleLabel setFont:[UIFont fontWithName:@"DIN Condensed" size:28]];
[button setTitle:@"PopUp Block Based" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showPopUP:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button];

UIButton *buttonDelegate = [UIButton buttonWithType:UIButtonTypeCustom];
buttonDelegate.frame = CGRectMake(self.view.bounds.size.width/2-100, 250, 200, 40);
[buttonDelegate setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[buttonDelegate.titleLabel setFont:[UIFont fontWithName:@"DIN Condensed" size:36]];
[buttonDelegate setTitle:@"PopUp Delegate" forState:UIControlStateNormal];
[buttonDelegate addTarget:self action:@selector(showPopUpWithDelegate:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:buttonDelegate];

popUpWithDelegate = [[btSimplePopUP alloc]initWithItemImage:@[
[UIImage imageNamed:@"alert.png"],
[UIImage imageNamed:@"attach.png"],
[UIImage imageNamed:@"cloudUp.png"],
[UIImage imageNamed:@"facebook.png"],
[UIImage imageNamed:@"camera.png"],
[UIImage imageNamed:@"dropBox.png"],
[UIImage imageNamed:@"mic.png"],
[UIImage imageNamed:@"wi-Fi.png"],
[UIImage imageNamed:@"curved.png"],
[UIImage imageNamed:@"stacks.png"],
[UIImage imageNamed:@"share.png"],
[UIImage imageNamed:@"twitter.png"],
[UIImage imageNamed:@"search.png"],
[UIImage imageNamed:@"whatsApp.png"],
[UIImage imageNamed:@"settings.png"],
]
andTitles: @[
@"Alert", @"Attach",@"Upload", @"Facebook", @"Camera", @"Dropbox",
@"Recording", @"Wi-Fi", @"Icon", @"Stacks", @"Share", @"Twitter", @"Search", @"WhatsApp", @"Settings"
]

andActionArray:nil addToViewController:self];
popUpWithDelegate.delegate = self;

[self.view addSubview:popUpWithDelegate];
[popUpWithDelegate setPopUpStyle:BTPopUpStyleDefault];
[popUpWithDelegate setPopUpBorderStyle:BTPopUpBorderStyleDefaultNone];

popUp = [[btSimplePopUP alloc]initWithItemImage:@[
[UIImage imageNamed:@"alert.png"],
[UIImage imageNamed:@"attach.png"],
Expand Down Expand Up @@ -159,6 +194,10 @@ -(void)showPopUP:(id)sender {
[popUp show:BTPopUPAnimateNone];
}

-(void)showPopUpWithDelegate:(id)sender{
[popUpWithDelegate show:BTPopUPAnimateNone];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
Expand All @@ -176,4 +215,12 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
}
*/


#pragma -mark delegate btSimplePopUp

-(void)btSimplePopUP:(btSimplePopUP *)popUp didSelectItemAtIndex:(NSInteger)index{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"PopItem" message:[NSString stringWithFormat:@"iAM from Delegate. My Index is %ld", (long)index] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}

@end
14 changes: 13 additions & 1 deletion btSimplePopUp/btSimplePopUP.h
Expand Up @@ -26,6 +26,16 @@ typedef NS_ENUM(NSInteger, BTPopUpBorderStyle) {
BTPopUpBorderStyleDarkContent
};


@class btSimplePopUP;

@protocol btSimplePopUpDelegate <NSObject>

@optional
-(void)btSimplePopUP:(btSimplePopUP *)popUp didSelectItemAtIndex:(NSInteger)index;

@end

@interface btSimplePopUP : UIView <UIScrollViewDelegate>{
UIImageView *backGroundBlurr;
UIView *contentView;
Expand All @@ -40,12 +50,14 @@ typedef NS_ENUM(NSInteger, BTPopUpBorderStyle) {
@property (nonatomic, assign) BTPopUpBorderStyle popUpBorderStyle;
@property (nonatomic, assign) UIColor *popUpBackgroundColor;
@property (nonatomic, assign) BTPopUpAnimation animationStyle;

@property(nonatomic, weak) id <btSimplePopUpDelegate> delegate;
@property (nonatomic) BOOL setShowRipples;

-(instancetype)initWithItemImage:(NSArray *)items andActionArray:(NSArray *)actionArray addToViewController:(UIViewController*)sender;
-(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArray andActionArray:(NSArray *)actionArray addToViewController:(UIViewController*)sender;

-(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArray addToViewController:(UIViewController*)sender;

-(void)setPopUpBackgroundColor:(UIColor *)popUpBackgroundColor;
-(void)show:(BTPopUpAnimation)animation;
-(void)dismiss;
Expand Down
120 changes: 87 additions & 33 deletions btSimplePopUp/btSimplePopUP.m
Expand Up @@ -33,7 +33,6 @@ - (instancetype)initWithImage:(UIImage *)image;
#pragma mark - @implementation
@implementation BTPopUpItemView


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
Expand All @@ -48,7 +47,10 @@ - (instancetype)initWithImage:(UIImage *)image title:(NSString *)title action:(d
if ((self = [super init])) {
_title = [title copy];
_imageView = [[UIImageView alloc]initWithImage:image];
_action = [action copy];
if(action)
_action = [action copy];
else
_action = nil;
}

return self;
Expand Down Expand Up @@ -99,7 +101,9 @@ @interface btRippleButtton : UIView{
}

@property (nonatomic, copy) completion block;
@property (nonatomic, assign) NSInteger index;

-(void)setRippleButtonIndex:(NSInteger )index;
-(instancetype)initWithImage:(UIImage *)image andFrame:(CGRect)frame andTarget:(SEL)action andID:(id)sender;

-(instancetype)initWithImage:(UIImage *)image andFrame:(CGRect)frame onCompletion:(completion)completionBlock;
Expand Down Expand Up @@ -187,6 +191,9 @@ -(instancetype)initWithImage:(UIImage *)image andTitle:(NSString *)aTitle andFra
return self;
}

-(void)setRippleButtonIndex:(NSInteger )index {
_index = index;
}

-(void)setRippleEffectWithColor:(UIColor *)color {
rippleColor = color;
Expand Down Expand Up @@ -248,6 +255,8 @@ -(void)handleTap:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"BT_POP_UP_ITEM_PRESSED" object:nil];
BOOL success= YES;
_block(success);
}else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"BT_POP_UP_ITEM_PRESSED" object:self];
}
}];

Expand Down Expand Up @@ -277,6 +286,11 @@ - (id)initWithFrame:(CGRect)frame
return self;
}

-(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArray addToViewController:(UIViewController*)sender {

return [self initWithItemImage:items andTitles:titleArray andActionArray:nil addToViewController:sender];
}

-(instancetype)initWithItemImage:(NSArray *)items andActionArray:(NSArray *)actionArray addToViewController:(UIViewController *)sender {
return [self initWithItemImage:items andTitles:nil andActionArray:actionArray addToViewController:sender];
}
Expand All @@ -285,7 +299,7 @@ -(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArra
self = [super init];
if (self) {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:@"BT_POP_UP_ITEM_PRESSED" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss:) name:@"BT_POP_UP_ITEM_PRESSED" object:nil];
// initialize with the blur effect as background
CGRect screenSize = [UIScreen mainScreen].bounds;
self.frame = CGRectMake(0, 0, screenSize.size.width, screenSize.size.height);
Expand All @@ -296,7 +310,7 @@ -(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArra
backGroundBlurr.alpha = 0;
[self addSubview:backGroundBlurr];

UITapGestureRecognizer *tapOnBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
UITapGestureRecognizer *tapOnBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissOnTap)];
tapOnBackground.numberOfTapsRequired = 1;
[backGroundBlurr addGestureRecognizer:tapOnBackground];
[backGroundBlurr setUserInteractionEnabled:YES];
Expand Down Expand Up @@ -334,10 +348,10 @@ -(instancetype)initWithItemImage:(NSArray *)items andTitles:(NSArray *)titleArra
[itemArray addObject:item];
index++;
}
}else if([items count] == [actionArray count]){
}else if(![actionArray count]){
int index = 0;
for (UIImage *image in items) {
BTPopUpItemView *item = [[BTPopUpItemView alloc] initWithImage:image title:nil action:[actionArray objectAtIndex:index]];
BTPopUpItemView *item = [[BTPopUpItemView alloc] initWithImage:image title:[titleArray objectAtIndex:index] action:nil];
[itemArray addObject:item];
index++;
}
Expand Down Expand Up @@ -384,7 +398,7 @@ -(void)setUpPopItems {
BTPopUpItemView *item = [popItems objectAtIndex:counter];
if(count < 9){
if(count < 3){
[self addButton:item xAxis:xAxis yAxis:yAxis];
[self addButton:item xAxis:xAxis yAxis:yAxis index:counter];
xAxis += itemSize.width + xFactor;

if(count == 2){
Expand All @@ -394,7 +408,7 @@ -(void)setUpPopItems {
}

if(count > 2 && count < 6){
[self addButton:item xAxis:xAxis yAxis:yAxis];
[self addButton:item xAxis:xAxis yAxis:yAxis index:counter];
xAxis += itemSize.width+ xFactor;

if(count == 5){
Expand All @@ -404,7 +418,7 @@ -(void)setUpPopItems {
}

if(count > 5 && count < 9){
[self addButton:item xAxis:xAxis yAxis:yAxis];
[self addButton:item xAxis:xAxis yAxis:yAxis index:counter];
xAxis += itemSize.width+ xFactor;

if(count == 8){
Expand All @@ -424,9 +438,10 @@ -(void)setUpPopItems {
}


-(void)addButton:(BTPopUpItemView*) item xAxis:(CGFloat)x yAxis:(CGFloat)y {
-(void)addButton:(BTPopUpItemView*) item xAxis:(CGFloat)x yAxis:(CGFloat)y index:(NSInteger)index {
btRippleButtton *button = [[btRippleButtton alloc]initWithImage:item.imageView.image andTitle:item.title andFrame:CGRectMake(x, y, itemSize.width, itemSize.height) onCompletion:[item.action copy]];
[button setRippeEffect:YES];
[button setRippleButtonIndex:index];
[scrollView addSubview:button];
}

Expand Down Expand Up @@ -502,32 +517,71 @@ -(void)show:(BTPopUpAnimation)animation {

}

-(void)dismiss{

if(_animationStyle == BTPopUPAnimateWithFade) {
// fade out animation
[UIView animateWithDuration:0.6
animations:^{
self.alpha = 0;
backGroundBlurr.alpha = 0;
} completion:^(BOOL finished) {
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
}];
-(void)dismissOnTap{
[self dismiss:nil];
}
-(void)dismiss:(NSNotification *)message{
if([message object]){
btRippleButtton *rippleButton = (btRippleButtton *)[message object];
if(_animationStyle == BTPopUPAnimateWithFade) {
// fade out animation
[UIView animateWithDuration:0.6
animations:^{
self.alpha = 0;
backGroundBlurr.alpha = 0;
} completion:^(BOOL finished) {

if([self.delegate respondsToSelector:@selector(btSimplePopUP:didSelectItemAtIndex:)]){
[self.delegate btSimplePopUP:self didSelectItemAtIndex:rippleButton.index];
}
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
}];
}else {

// pop out animation
[UIView animateWithDuration:0.2
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
backGroundBlurr.alpha = 0;
self.alpha = 0;
} completion:^(BOOL finished){
if([self.delegate respondsToSelector:@selector(btSimplePopUP:didSelectItemAtIndex:)]){
[self.delegate btSimplePopUP:self didSelectItemAtIndex:rippleButton.index];
}

}];
}

}else {
if(_animationStyle == BTPopUPAnimateWithFade) {
// fade out animation
[UIView animateWithDuration:0.6
animations:^{
self.alpha = 0;
backGroundBlurr.alpha = 0;
} completion:^(BOOL finished) {
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
}];
}else {

// pop out animation
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
backGroundBlurr.alpha = 0;
self.alpha = 0;
} completion:nil];
}

// pop out animation
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
contentView.frame = CGRectMake(self.frame.size.width/2-150, SCREEN_SIZE.size.height, POPUP_WIDTH, POP_HEIGHT);
backGroundBlurr.alpha = 0;
self.alpha = 0;
} completion:nil];
}

}
/*
// Only override drawRect: if you perform custom drawing.
Expand Down

0 comments on commit b06280d

Please sign in to comment.