UzysGroupPickerViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // UzysGroupPickerViewController.m
  3. // UzysAssetsPickerController
  4. //
  5. // Created by Uzysjung on 2014. 2. 13..
  6. // Copyright (c) 2014년 Uzys. All rights reserved.
  7. //
  8. // 版权属于原作者
  9. // http://code4app.com(cn) http://code4app.net(en)
  10. // 来源于最专业的源码分享网站: Code4App
  11. #import "UzysGroupViewCell.h"
  12. #import "UzysGroupPickerViewController.h"
  13. #import "UzysAssetsPickerController_Configuration.h"
  14. @interface UzysGroupPickerViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
  15. @property (nonatomic,strong) UIView *containerView;
  16. @end
  17. @implementation UzysGroupPickerViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26. - (id)initWithGroups:(NSMutableArray *)groups
  27. {
  28. self = [super init];
  29. if(self) {
  30. self.groups = groups;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. [self setupLayout];
  39. [self setupTableView];
  40. [self setupGestureRecognizer];
  41. }
  42. - (void)didReceiveMemoryWarning
  43. {
  44. [super didReceiveMemoryWarning];
  45. // Dispose of any resources that can be recreated.
  46. }
  47. - (void)setupLayout
  48. {
  49. self.view.frame = [UIScreen mainScreen].bounds;
  50. //anchorPoint 를 잡는데 화살표 지점으로 잡아야함
  51. self.containerView = [[UIView alloc] init];
  52. self.containerView.layer.anchorPoint = CGPointMake(0.5, 0);
  53. self.containerView.frame = CGRectMake(1, 55, 318, 250);
  54. self.containerView.layer.cornerRadius = 4;
  55. self.containerView.alpha = 0;
  56. self.containerView.clipsToBounds = YES;
  57. [self.view addSubview:self.containerView];
  58. self.view.alpha = 0;
  59. }
  60. - (void)setupFrame
  61. {
  62. CGFloat height = [self.groups count] * kGroupPickerViewCellLength;
  63. if(height > kGroupPickerViewCellLength * 5)
  64. {
  65. height = kGroupPickerViewCellLength *5;
  66. }
  67. self.containerView.layer.anchorPoint = CGPointMake(0.5, 0);
  68. self.containerView.frame = CGRectMake(1, 55, 318, height);
  69. }
  70. - (void)setupTableView
  71. {
  72. self.tableView = [[UITableView alloc] initWithFrame:self.containerView.bounds style:UITableViewStylePlain];
  73. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  74. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  75. self.tableView.dataSource = self;
  76. self.tableView.delegate = self;
  77. self.tableView.rowHeight = kGroupPickerViewCellLength;
  78. self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
  79. [self.containerView addSubview:self.tableView];
  80. // [self.tableView reloadData];
  81. }
  82. - (void)setupGestureRecognizer
  83. {
  84. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
  85. tap.cancelsTouchesInView = NO; // Allow touches through to a UITableView or other touchable view, as suggested by Dimajp.
  86. [self.view addGestureRecognizer:tap];
  87. self.tapGestureRecognizer = tap;
  88. }
  89. - (void)reloadData
  90. {
  91. [self setupFrame];
  92. [self.tableView reloadData];
  93. }
  94. - (void)show
  95. {
  96. [UIView animateWithDuration:0.05f delay:0.f options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  97. self.view.alpha = 1;
  98. self.containerView.alpha = 0.3f;
  99. self.containerView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
  100. } completion:^(BOOL finished) {
  101. [UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  102. self.containerView.alpha = 1.0f;
  103. self.containerView.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
  104. } completion:^(BOOL finished) {
  105. [UIView animateWithDuration:0.15f delay:0.f options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  106. self.containerView.transform = CGAffineTransformIdentity;
  107. } completion:^(BOOL finished) {
  108. // NSLog(@"groups %@ frame %@ tableView frame %@ alpha %f",self.groups,NSStringFromCGRect(self.containerView.frame),NSStringFromCGRect(self.tableView.frame),self.containerView.alpha);
  109. }];
  110. }];
  111. }];
  112. }
  113. - (void)dismiss:(BOOL)animated
  114. {
  115. if (!animated)
  116. {
  117. self.containerView.alpha = 0.0f;
  118. self.view.alpha = 0;
  119. }
  120. else
  121. {
  122. [UIView animateWithDuration:0.3f animations:^{
  123. self.containerView.alpha = 0.1f;
  124. self.containerView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
  125. } completion:^(BOOL finished) {
  126. self.containerView.alpha = 0.3f;
  127. self.view.alpha = 0;
  128. }];
  129. }
  130. }
  131. - (void)toggle
  132. {
  133. if(self.containerView.alpha <0.5)
  134. {
  135. [self show];
  136. }
  137. else
  138. {
  139. [self dismiss:YES];
  140. }
  141. }
  142. #pragma mark - Table view data source
  143. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  144. {
  145. return 1;
  146. }
  147. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  148. {
  149. return self.groups.count;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. static NSString *CellIdentifier = @"kGroupViewCellIdentifier";
  154. UzysGroupViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  155. if (cell == nil)
  156. {
  157. cell = [[UzysGroupViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  158. }
  159. [cell applyData:[self.groups objectAtIndex:indexPath.row]];
  160. return cell;
  161. }
  162. #pragma mark - Table view delegate
  163. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. return kGroupPickerViewCellLength;
  166. }
  167. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  168. {
  169. }
  170. #pragma mark - UITapGestureRecognizer
  171. - (void)tapped:(UITapGestureRecognizer *)tap
  172. {
  173. CGPoint point = [tap locationInView:self.view];
  174. if (!CGRectContainsPoint(self.containerView.frame, point))
  175. {
  176. [self dismiss:YES];
  177. }
  178. }
  179. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  180. {
  181. if([keyPath isEqualToString:@"groups"])
  182. {
  183. dispatch_async(dispatch_get_main_queue(), ^{
  184. [self setupFrame];
  185. });
  186. }
  187. }
  188. @end