Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
added backgroundView as instance variable
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjuhasz committed Feb 16, 2013
1 parent 0ff87d3 commit a6c72e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Source/UIViewController+MJPopupViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

@class MJPopupBackgroundView;

typedef enum {
MJPopupViewAnimationFade = 0,
MJPopupViewAnimationSlideBottomTop = 1,
Expand All @@ -23,6 +25,7 @@ typedef enum {
@interface UIViewController (MJPopupViewController)

@property (nonatomic, retain) UIViewController *mj_popupViewController;
@property (nonatomic, retain) MJPopupBackgroundView *mj_popupBackgroundView;

- (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType;
- (void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType;
Expand Down
82 changes: 43 additions & 39 deletions Source/UIViewController+MJPopupViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

#define kPopupModalAnimationDuration 0.35
#define kMJPopupViewController @"kMJPopupViewController"
#define kMJPopupBackgroundView @"kMJPopupBackgroundView"
#define kMJSourceViewTag 23941
#define kMJPopupViewTag 23942
#define kMJBackgroundViewTag 23943
#define kMJOverlayViewTag 23945

@interface UIViewController (MJPopupViewControllerPrivate)
Expand All @@ -41,6 +41,15 @@ - (void)setMj_popupViewController:(UIViewController *)mj_popupViewController {

}

- (MJPopupBackgroundView*)mj_popupBackgroundView {
return objc_getAssociatedObject(self, kMJPopupBackgroundView);
}

- (void)setMj_popupBackgroundView:(MJPopupBackgroundView *)mj_popupBackgroundView {
objc_setAssociatedObject(self, kMJPopupBackgroundView, mj_popupBackgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType
{
self.mj_popupViewController = popupViewController;
Expand Down Expand Up @@ -102,12 +111,11 @@ - (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)
overlayView.backgroundColor = [UIColor clearColor];

// BackgroundView
MJPopupBackgroundView *backgroundView = [[MJPopupBackgroundView alloc] initWithFrame:sourceView.bounds];
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
backgroundView.tag = kMJBackgroundViewTag;
backgroundView.backgroundColor = [UIColor clearColor];
backgroundView.alpha = 0.0f;
[overlayView addSubview:backgroundView];
self.mj_popupBackgroundView = [[MJPopupBackgroundView alloc] initWithFrame:sourceView.bounds];
self.mj_popupBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mj_popupBackgroundView.backgroundColor = [UIColor clearColor];
self.mj_popupBackgroundView.alpha = 0.0f;
[overlayView addSubview:self.mj_popupBackgroundView];

// Make the Background Clickable
UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
Expand All @@ -119,7 +127,7 @@ - (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)
popupView.alpha = 0.0f;
[overlayView addSubview:popupView];
[sourceView addSubview:overlayView];

[dismissButton addTarget:self action:@selector(dismissPopupViewControllerWithanimation:) forControlEvents:UIControlEventTouchUpInside];
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
Expand All @@ -137,7 +145,7 @@ - (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)
dismissButton.tag = MJPopupViewAnimationFade;
[self fadeViewIn:popupView sourceView:sourceView overlayView:overlayView];
break;
}
}
}

-(UIView*)topView {
Expand Down Expand Up @@ -181,25 +189,24 @@ - (void)dismissPopupViewControllerWithanimation:(id)sender

- (void)slideViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
{
UIView *backgroundView = [overlayView viewWithTag:kMJBackgroundViewTag];
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
CGRect popupStartRect;
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideBottomBottom:
popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
sourceSize.height,
popupSize.width,
popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
sourceSize.height,
popupSize.width,
popupSize.height);

break;
case MJPopupViewAnimationSlideLeftLeft:
case MJPopupViewAnimationSlideLeftRight:
popupStartRect = CGRectMake(-sourceSize.width,
popupStartRect = CGRectMake(-sourceSize.width,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.width,
popupSize.height);
break;

Expand All @@ -212,23 +219,23 @@ - (void)slideViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayVie
break;

default:
popupStartRect = CGRectMake(sourceSize.width,
popupStartRect = CGRectMake(sourceSize.width,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.width,
popupSize.height);
break;
}
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
}
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.width,
popupSize.height);

// Set starting properties
popupView.frame = popupStartRect;
popupView.alpha = 1.0f;
[UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.mj_popupViewController viewWillAppear:NO];
backgroundView.alpha = 1.0f;
self.mj_popupBackgroundView.alpha = 1.0f;
popupView.frame = popupEndRect;
} completion:^(BOOL finished) {
[self.mj_popupViewController viewDidAppear:NO];
Expand All @@ -237,7 +244,6 @@ - (void)slideViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayVie

- (void)slideViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
{
UIView *backgroundView = [overlayView viewWithTag:kMJBackgroundViewTag];
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
Expand All @@ -246,36 +252,36 @@ - (void)slideViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayVi
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideTopTop:
popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
-popupSize.height,
popupSize.width,
-popupSize.height,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideBottomBottom:
case MJPopupViewAnimationSlideTopBottom:
popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
sourceSize.height,
popupSize.width,
sourceSize.height,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideLeftRight:
case MJPopupViewAnimationSlideRightRight:
popupEndRect = CGRectMake(sourceSize.width,
popupView.frame.origin.y,
popupSize.width,
popupView.frame.origin.y,
popupSize.width,
popupSize.height);
break;
default:
popupEndRect = CGRectMake(-popupSize.width,
popupView.frame.origin.y,
popupSize.width,
popupEndRect = CGRectMake(-popupSize.width,
popupView.frame.origin.y,
popupSize.width,
popupSize.height);
break;
}

[UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.mj_popupViewController viewWillDisappear:NO];
popupView.frame = popupEndRect;
backgroundView.alpha = 0.0f;
self.mj_popupBackgroundView.alpha = 0.0f;
} completion:^(BOOL finished) {
[popupView removeFromSuperview];
[overlayView removeFromSuperview];
Expand All @@ -288,13 +294,12 @@ - (void)slideViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayVi

- (void)fadeViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
{
UIView *backgroundView = [overlayView viewWithTag:kMJBackgroundViewTag];
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.width,
popupSize.height);

// Set starting properties
Expand All @@ -303,7 +308,7 @@ - (void)fadeViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView

[UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
[self.mj_popupViewController viewWillAppear:NO];
backgroundView.alpha = 0.5f;
self.mj_popupBackgroundView.alpha = 0.5f;
popupView.alpha = 1.0f;
} completion:^(BOOL finished) {
[self.mj_popupViewController viewDidAppear:NO];
Expand All @@ -312,10 +317,9 @@ - (void)fadeViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView

- (void)fadeViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
{
UIView *backgroundView = [overlayView viewWithTag:kMJBackgroundViewTag];
[UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
[self.mj_popupViewController viewWillDisappear:NO];
backgroundView.alpha = 0.0f;
self.mj_popupBackgroundView.alpha = 0.0f;
popupView.alpha = 0.0f;
} completion:^(BOOL finished) {
[popupView removeFromSuperview];
Expand Down

0 comments on commit a6c72e8

Please sign in to comment.