UzysGroupPickerView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // UzysGroupPickerView.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 "UzysGroupPickerView.h"
  12. #import "UzysGroupViewCell.h"
  13. #import "UzysAssetsPickerController_Configuration.h"
  14. #define BounceAnimationPixel 5
  15. #define NavigationHeight kNavOffSet
  16. @implementation UzysGroupPickerView
  17. - (id)initWithGroups:(NSMutableArray *)groups
  18. {
  19. self = [super initWithFrame:CGRectZero];
  20. if (self) {
  21. // Initialization code
  22. self.groups = groups;
  23. [self setupLayout];
  24. [self setupTableView];
  25. [self addObserver:self forKeyPath:@"groups" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
  26. }
  27. return self;
  28. }
  29. - (void)dealloc
  30. {
  31. [self removeObserver:self forKeyPath:@"groups"];
  32. }
  33. - (void)setupLayout
  34. {
  35. //anchorPoint 를 잡는데 화살표 지점으로 잡아야함
  36. self.frame = CGRectMake(0, - [UIScreen mainScreen].bounds.size.height, kSize.width, kSize.height);;
  37. self.layer.cornerRadius = 4;
  38. self.clipsToBounds = YES;
  39. self.backgroundColor = [UIColor whiteColor];
  40. }
  41. - (void)setupTableView
  42. {
  43. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationHeight, kSize.width, self.bounds.size.height -NavigationHeight) style:UITableViewStylePlain];
  44. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  45. self.tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
  46. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  47. self.tableView.dataSource = self;
  48. self.tableView.delegate = self;
  49. self.tableView.rowHeight = kGroupPickerViewCellLength;
  50. self.tableView.backgroundColor = [UIColor whiteColor];
  51. [self addSubview:self.tableView];
  52. }
  53. - (void)reloadData
  54. {
  55. [self.tableView reloadData];
  56. }
  57. - (void)show
  58. {
  59. [UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  60. self.frame = CGRectMake(0, BounceAnimationPixel , kSize.width, [UIScreen mainScreen].bounds.size.height);
  61. } completion:^(BOOL finished) {
  62. [UIView animateWithDuration:0.15f delay:0.f options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  63. self.frame = CGRectMake(0, 0 , kSize.width, [UIScreen mainScreen].bounds.size.height);
  64. } completion:^(BOOL finished) {
  65. }];
  66. }];
  67. self.isOpen = YES;
  68. }
  69. - (void)dismiss:(BOOL)animated
  70. {
  71. if (!animated)
  72. {
  73. self.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, kSize.width, [UIScreen mainScreen].bounds.size.height );
  74. }
  75. else
  76. {
  77. [UIView animateWithDuration:0.3f animations:^{
  78. self.frame = CGRectMake(0, - [UIScreen mainScreen].bounds.size.height, kSize.width, [UIScreen mainScreen].bounds.size.height);
  79. } completion:^(BOOL finished) {
  80. }];
  81. }
  82. self.isOpen = NO;
  83. }
  84. - (void)toggle
  85. {
  86. if(self.frame.origin.y <0)
  87. {
  88. [self show];
  89. }
  90. else
  91. {
  92. [self dismiss:YES];
  93. }
  94. }
  95. #pragma mark - Table view data source
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  97. {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. return self.groups.count;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. static NSString *CellIdentifier = @"kGroupViewCellIdentifier";
  107. UzysGroupViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  108. if (cell == nil)
  109. {
  110. cell = [[UzysGroupViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  111. }
  112. [cell applyData:[self.groups objectAtIndex:indexPath.row]];
  113. return cell;
  114. }
  115. #pragma mark - Table view delegate
  116. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. return kGroupPickerViewCellLength;
  119. }
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. if(self.blockTouchCell)
  123. self.blockTouchCell(indexPath.row);
  124. }
  125. @end