Advertisement
Guest User

MTPopupWindow.m

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