Advertisement
Guest User

MTPopupWindow.m

a guest
Mar 11th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. //
  2. //
  3. // MTPopupWindow.m
  4. //
  5. //
  6.  
  7. #import "MTPopupWindow.h"
  8.  
  9. #define kShadeViewTag 1000
  10.  
  11. @interface MTPopupWindow(Private)
  12. - (id)initWithSuperview:(UIView*)sview andContent:(NSString*)pContent;
  13. @end
  14.  
  15. @implementation MTPopupWindow
  16. @synthesize closeBtn = _closeBtn, bigPanelView = _bigPanelView, bgView = _bgView;
  17. /**
  18. * This is the only public method, it opens a popup window and loads the given content
  19. * @param NSString* fileName provide a file name to load a file from the app resources, or a URL to load a web page
  20. * @param UIView* view provide a UIViewController's view here (or other view)
  21. */
  22. +(void)showWindowWithContent:(NSString*)popupContent insideView:(UIView*)view
  23. {
  24. (void)[[MTPopupWindow alloc] initWithSuperview:view andContent:popupContent]; // Cast to void because we don't use the result (otherwise compiler warning)
  25. }
  26.  
  27. /**
  28. * Initializes the class instance, gets a view where the window will pop up in
  29. * and a file name/ URL
  30. */
  31. - (id)initWithSuperview:(UIView*)sview andContent:(NSString*)pContent
  32. {
  33. self = [super init];
  34. if (self) {
  35. // Initialization code here.
  36. _bgView = [[UIView alloc] initWithFrame: sview.bounds] ;
  37. [sview addSubview: _bgView];
  38.  
  39. // proceed with animation after the bgView was added
  40. [self performSelector:@selector(doTransitionWithContent:) withObject:pContent afterDelay:0.1];
  41. }
  42.  
  43. return self;
  44. }
  45.  
  46. /**
  47. * Afrer the window background is added to the UI the window can animate in
  48. * and load the UIWebView
  49. */
  50. -(void)doTransitionWithContent:(NSString*)popupContent
  51. {
  52. //faux view
  53. UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)] ;
  54. [_bgView addSubview: fauxView];
  55.  
  56. //the new panel
  57. _bigPanelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _bgView.frame.size.width, _bgView.frame.size.height)];
  58. _bigPanelView.center = CGPointMake( _bgView.frame.size.width/2, _bgView.frame.size.height/2);
  59.  
  60. //add the window background
  61. UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popupWindowBack.png"]] ;
  62. background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);
  63. [_bigPanelView addSubview: background];
  64.  
  65. //add the text view
  66. int offset = 10;
  67.  
  68. UITextView *view = [[UITextView alloc] initWithFrame:CGRectInset(background.frame, offset, offset)];
  69. view.backgroundColor = [UIColor clearColor];
  70.  
  71.  
  72. view.text = popupContent;
  73.  
  74.  
  75. [_bigPanelView addSubview: view];
  76.  
  77. //add the close button
  78. int closeBtnOffset = 10;
  79. UIImage* closeBtnImg = [UIImage imageNamed:@"popupCloseBtn.png"];
  80. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  81. [_closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
  82. [_closeBtn setFrame:CGRectMake( background.frame.origin.x + background.frame.size.width - closeBtnImg.size.width - closeBtnOffset,
  83. background.frame.origin.y ,
  84. closeBtnImg.size.width + closeBtnOffset,
  85. closeBtnImg.size.height + closeBtnOffset)];
  86. [_closeBtn addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
  87. [_bigPanelView addSubview: _closeBtn];
  88.  
  89. //animation options
  90. UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromRight |
  91. UIViewAnimationOptionAllowUserInteraction |
  92. UIViewAnimationOptionBeginFromCurrentState;
  93.  
  94. //run the animation
  95. [UIView transitionFromView:fauxView toView:_bigPanelView duration:0.5 options:options completion: ^(BOOL finished) {
  96.  
  97. //dim the contents behind the popup window
  98. UIView* shadeView = [[UIView alloc] initWithFrame:_bigPanelView.frame] ;
  99. shadeView.backgroundColor = [UIColor blackColor];
  100. shadeView.alpha = 0.3;
  101. shadeView.tag = kShadeViewTag;
  102. [_bigPanelView addSubview: shadeView];
  103. [_bigPanelView sendSubviewToBack: shadeView];
  104. }];
  105. }
  106.  
  107. /**
  108. * Removes the window background and calls the animation of the window
  109. */
  110. -(void)closePopupWindow
  111. {
  112. //remove the shade
  113. [self performSelector:@selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];
  114. [[_bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
  115.  
  116. }
  117.  
  118. /**
  119. * Animates the window and when done removes all views from the view hierarchy
  120. * since they are all only retained by their superview this also deallocates them
  121. * finally deallocate the class instance
  122. */
  123. -(void)closePopupWindowAnimate
  124. {
  125.  
  126. //faux view
  127. __block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
  128. [_bgView addSubview: fauxView];
  129.  
  130. //run the animation
  131. UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromLeft |
  132. UIViewAnimationOptionAllowUserInteraction |
  133. UIViewAnimationOptionBeginFromCurrentState;
  134.  
  135. //hold to the bigPanelView, because it'll be removed during the animation
  136.  
  137. [UIView transitionFromView:_bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {
  138.  
  139. //when popup is closed, remove all the views
  140. for (UIView* child in _bigPanelView.subviews) {
  141. [child removeFromSuperview];
  142. }
  143. for (UIView* child in _bgView.subviews) {
  144. [child removeFromSuperview];
  145. }
  146.  
  147. [_bgView removeFromSuperview];
  148.  
  149. }];
  150. }
  151.  
  152. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement