Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- // MTPopupWindow.m
- //
- //
- #import "MTPopupWindow.h"
- #define kShadeViewTag 1000
- @interface MTPopupWindow(Private)
- - (id)initWithSuperview:(UIView*)sview andContent:(NSString*)pContent;
- @end
- @implementation MTPopupWindow
- @synthesize closeBtn = _closeBtn, bigPanelView = _bigPanelView, bgView = _bgView;
- /**
- * This is the only public method, it opens a popup window and loads the given content
- * @param NSString* fileName provide a file name to load a file from the app resources, or a URL to load a web page
- * @param UIView* view provide a UIViewController's view here (or other view)
- */
- +(void)showWindowWithContent:(NSString*)popupContent insideView:(UIView*)view
- {
- (void)[[MTPopupWindow alloc] initWithSuperview:view andContent:popupContent]; // Cast to void because we don't use the result (otherwise compiler warning)
- }
- /**
- * Initializes the class instance, gets a view where the window will pop up in
- * and a file name/ URL
- */
- - (id)initWithSuperview:(UIView*)sview andContent:(NSString*)pContent
- {
- self = [super init];
- if (self) {
- // Initialization code here.
- _bgView = [[UIView alloc] initWithFrame: sview.bounds] ;
- [sview addSubview: _bgView];
- // proceed with animation after the bgView was added
- [self performSelector:@selector(doTransitionWithContent:) withObject:pContent afterDelay:0.1];
- }
- return self;
- }
- /**
- * Afrer the window background is added to the UI the window can animate in
- * and load the UIWebView
- */
- -(void)doTransitionWithContent:(NSString*)popupContent
- {
- //faux view
- UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)] ;
- [_bgView addSubview: fauxView];
- //the new panel
- _bigPanelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _bgView.frame.size.width, _bgView.frame.size.height)];
- _bigPanelView.center = CGPointMake( _bgView.frame.size.width/2, _bgView.frame.size.height/2);
- //add the window background
- UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popupWindowBack.png"]] ;
- background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);
- [_bigPanelView addSubview: background];
- //add the text view
- int offset = 10;
- UITextView *view = [[UITextView alloc] initWithFrame:CGRectInset(background.frame, offset, offset)];
- view.backgroundColor = [UIColor clearColor];
- view.text = popupContent;
- [_bigPanelView addSubview: view];
- //add the close button
- int closeBtnOffset = 10;
- UIImage* closeBtnImg = [UIImage imageNamed:@"popupCloseBtn.png"];
- _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
- [_closeBtn setFrame:CGRectMake( background.frame.origin.x + background.frame.size.width - closeBtnImg.size.width - closeBtnOffset,
- background.frame.origin.y ,
- closeBtnImg.size.width + closeBtnOffset,
- closeBtnImg.size.height + closeBtnOffset)];
- [_closeBtn addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
- [_bigPanelView addSubview: _closeBtn];
- //animation options
- UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromRight |
- UIViewAnimationOptionAllowUserInteraction |
- UIViewAnimationOptionBeginFromCurrentState;
- //run the animation
- [UIView transitionFromView:fauxView toView:_bigPanelView duration:0.5 options:options completion: ^(BOOL finished) {
- //dim the contents behind the popup window
- UIView* shadeView = [[UIView alloc] initWithFrame:_bigPanelView.frame] ;
- shadeView.backgroundColor = [UIColor blackColor];
- shadeView.alpha = 0.3;
- shadeView.tag = kShadeViewTag;
- [_bigPanelView addSubview: shadeView];
- [_bigPanelView sendSubviewToBack: shadeView];
- }];
- }
- /**
- * Removes the window background and calls the animation of the window
- */
- -(void)closePopupWindow
- {
- //remove the shade
- [self performSelector:@selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];
- [[_bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
- }
- /**
- * Animates the window and when done removes all views from the view hierarchy
- * since they are all only retained by their superview this also deallocates them
- * finally deallocate the class instance
- */
- -(void)closePopupWindowAnimate
- {
- //faux view
- __block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
- [_bgView addSubview: fauxView];
- //run the animation
- UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromLeft |
- UIViewAnimationOptionAllowUserInteraction |
- UIViewAnimationOptionBeginFromCurrentState;
- //hold to the bigPanelView, because it'll be removed during the animation
- [UIView transitionFromView:_bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {
- //when popup is closed, remove all the views
- for (UIView* child in _bigPanelView.subviews) {
- [child removeFromSuperview];
- }
- for (UIView* child in _bgView.subviews) {
- [child removeFromSuperview];
- }
- [_bgView removeFromSuperview];
- }];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement