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
- +(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 the content
- - (id)initWithSuperview:(UIView*)sview andContent:(NSString*)pContent
- {
- self = [super init];
- if (self) {
- // Initialization code here.
- self.bgView = [[UIView alloc] initWithFrame: sview.bounds] ;
- [sview addSubview: self.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 UITextView
- -(void)doTransitionWithContent:(NSString*)popupContent
- {
- //faux view
- UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)] ;
- [self.bgView addSubview: fauxView];
- //the new panel
- self.bigPanelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bgView.frame.size.width, self.bgView.frame.size.height)];
- self.bigPanelView.center = CGPointMake( self.bgView.frame.size.width/2, self.bgView.frame.size.height/2);
- self.bigPanelView.backgroundColor = [UIColor lightGrayColor];
- //add the text view
- int offsetx = 20;
- int offsety = 50;
- UITextView *view = [[UITextView alloc] initWithFrame:CGRectInset(self.bigPanelView.frame, offsetx, offsety)];
- view.backgroundColor = [UIColor clearColor];
- view.text = popupContent;
- [self.bigPanelView addSubview: view];
- //add the close button
- int closeBtnOffset = 10;
- UIImage* closeBtnImg = [UIImage imageNamed:@"popupCloseBtn.png"];
- self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [self.closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
- [self.closeBtn setFrame:CGRectMake(self.bigPanelView.frame.origin.x + self.bigPanelView.frame.size.width - closeBtnImg.size.width - closeBtnOffset,
- self.bigPanelView.frame.origin.y ,
- closeBtnImg.size.width + closeBtnOffset,
- closeBtnImg.size.height + closeBtnOffset)];
- [self.closeBtn addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
- [self.bigPanelView addSubview: self.closeBtn];
- //animation options
- UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromRight |
- UIViewAnimationOptionAllowUserInteraction |
- UIViewAnimationOptionBeginFromCurrentState;
- //run the animation
- [UIView transitionFromView:fauxView toView:self.bigPanelView duration:0.5 options:options completion: ^(BOOL finished) {
- //dim the contents behind the popup window
- UIView* shadeView = [[UIView alloc] initWithFrame:self.bigPanelView.frame] ;
- shadeView.backgroundColor = [UIColor blackColor];
- shadeView.alpha = 0.3;
- shadeView.tag = kShadeViewTag;
- [self.bigPanelView addSubview: shadeView];
- [self.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];
- [[self.bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
- }
- // Animates the window and when done removes all views from the view hierarchy
- -(void)closePopupWindowAnimate
- {
- //faux view
- __block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
- [self.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:self.bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {
- //when popup is closed, remove all the views
- for (UIView* child in self.bigPanelView.subviews) {
- [child removeFromSuperview];
- }
- for (UIView* child in self.bgView.subviews) {
- [child removeFromSuperview];
- }
- [self.bgView removeFromSuperview];
- }];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement