Няма описание

SubmitSelectTransportView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // SubmitChangeTransportView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/6/20.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. // 选择运输方式
  8. #import "SubmitSelectTransportView.h"
  9. typedef enum {
  10. kTransportBtnTagCarriageIn = 0,
  11. kTransportBtnTagTakeSelf,
  12. kTransportBtnTagCancel,
  13. kTransportBtnTagConfirm
  14. } kTransportBtnTag;
  15. #define kTransportContentHeight 190
  16. @interface SubmitSelectTransportView ()
  17. @property (nonatomic, strong) UIView *contentView;
  18. @property (nonatomic, strong) UIButton *countryInBtn;
  19. @property (nonatomic, strong) UIButton *takeSelfBtn;
  20. @end
  21. @implementation SubmitSelectTransportView
  22. @synthesize carriageInLabel = _carriageInLabel;
  23. @synthesize takeSelfLabel = _takeSelfLabel;
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. {
  26. if (self = [super initWithFrame:frame]) {
  27. [self initializeLayout];
  28. [self addTapGesture];
  29. self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  30. }
  31. return self;
  32. }
  33. - (void)addTapGesture
  34. {
  35. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureEvent:)];
  36. [self addGestureRecognizer:singleTap];
  37. }
  38. - (void)initializeLayout
  39. {
  40. UIView *bannerView = [[UIView alloc]init];
  41. UIView *countryInView = [[UIView alloc]init];
  42. UIView *takeSelfView = [[UIView alloc]init];
  43. UIView *bottomView = [[UIView alloc]init];
  44. bannerView.backgroundColor = [UIColor colorWithRed:234.0 / 255 green:234.0 / 255 blue:234.0 / 255 alpha:1.0f];
  45. UILabel *titleLabel = ({
  46. UILabel *label = [[UILabel alloc]init];
  47. label.backgroundColor = [UIColor clearColor];
  48. label.font = [UIFont systemFontOfSize:15];
  49. label.textColor = UIColorFromRGB(0x999999);
  50. label.text = @"运送方式";
  51. label;
  52. });
  53. [self addSubview:self.contentView];
  54. [self.contentView addSubview:bannerView];
  55. [self.contentView addSubview:countryInView];
  56. [self.contentView addSubview:takeSelfView];
  57. [self.contentView addSubview:bottomView];
  58. [bannerView addSubview:titleLabel];
  59. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.height.equalTo(@kTransportContentHeight);
  61. make.left.right.bottom.equalTo(self);
  62. }];
  63. [bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.right.top.equalTo(self.contentView);
  65. make.height.equalTo(@40);
  66. }];
  67. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.center.equalTo(bannerView);
  69. }];
  70. [countryInView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.top.equalTo(bannerView.mas_bottom);
  72. make.left.right.equalTo(self.contentView);
  73. make.height.equalTo(@50);
  74. }];
  75. [takeSelfView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.equalTo(countryInView.mas_bottom);
  77. make.left.right.equalTo(self.contentView);
  78. make.height.equalTo(@50);
  79. }];
  80. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.top.equalTo(takeSelfView.mas_bottom);
  82. make.left.right.equalTo(self.contentView);
  83. make.bottom.equalTo(self.contentView);
  84. }];
  85. [self layoutContentView:countryInView withLabel:self.carriageInLabel actionBtn:self.countryInBtn];
  86. [self layoutContentView:takeSelfView withLabel:self.takeSelfLabel actionBtn:self.takeSelfBtn];
  87. [self layoutBottomView:bottomView];
  88. }
  89. - (void)layoutBottomView:(UIView *)bottomView
  90. {
  91. UIView *middleLine = [[UIView alloc]init];
  92. middleLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
  93. UIButton *cancelBtn = ({
  94. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  95. [button setTitle:@"取消" forState:UIControlStateNormal];
  96. [button setTitleColor:UIColorFromRGB(0x2b6ef7) forState:UIControlStateNormal];
  97. button.titleLabel.font = [UIFont systemFontOfSize:18];
  98. [button addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  99. button.tag = kTransportBtnTagCancel;
  100. button;
  101. });
  102. UIButton *confirmBtn = ({
  103. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  104. [button setTitle:@"确认" forState:UIControlStateNormal];
  105. [button setTitleColor:UIColorFromRGB(0x2b6ef7) forState:UIControlStateNormal];
  106. button.titleLabel.font = [UIFont systemFontOfSize:18];
  107. [button addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  108. button.tag = kTransportBtnTagConfirm;
  109. button;
  110. });
  111. [bottomView addSubview:middleLine];
  112. [bottomView addSubview:cancelBtn];
  113. [bottomView addSubview:confirmBtn];
  114. [middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
  115. make.top.bottom.centerX.equalTo(bottomView);
  116. make.width.equalTo(@0.5);
  117. }];
  118. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.right.equalTo(middleLine.mas_left);
  120. make.top.left.bottom.equalTo(bottomView);
  121. }];
  122. [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.left.equalTo(middleLine.mas_right);
  124. make.top.right.bottom.equalTo(bottomView);
  125. }];
  126. }
  127. - (void)layoutContentView:(UIView *)contentView withLabel:(UILabel *)titleLabel actionBtn:(UIButton *)actionBtn
  128. {
  129. UIView *bottomLine = [[UIView alloc]init];
  130. bottomLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
  131. [contentView addSubview:titleLabel];
  132. [contentView addSubview:bottomLine];
  133. [contentView addSubview:actionBtn];
  134. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.left.equalTo(contentView).offset(30);
  136. make.centerY.equalTo(contentView);
  137. }];
  138. [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.right.equalTo(contentView).offset(- 30);
  140. make.centerY.equalTo(contentView);
  141. make.width.height.equalTo(contentView.mas_height);
  142. }];
  143. [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.left.right.bottom.equalTo(contentView);
  145. make.height.equalTo(@0.5);
  146. }];
  147. }
  148. - (void)showInView:(UIView *)view animated:(BOOL)animated
  149. {
  150. if (!view) return;
  151. [view addSubview:self];
  152. self.frame = view.bounds;
  153. if (animated) {
  154. self.backgroundColor = [UIColor clearColor];
  155. CGAffineTransform transForm = CGAffineTransformMakeTranslation(0, kTransportContentHeight);
  156. self.contentView.transform = transForm;
  157. [UIView animateWithDuration:0.3f animations:^{
  158. self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  159. self.contentView.transform = CGAffineTransformIdentity;
  160. }];
  161. }
  162. }
  163. - (void)dismissAnimation
  164. {
  165. CGRect frame = self.contentView.frame;
  166. CGRect downFrame = CGRectOffset(frame, 0, CGRectGetHeight(frame));
  167. self.contentView.transform = CGAffineTransformIdentity;
  168. [UIView animateWithDuration:0.3f animations:^{
  169. self.backgroundColor = [UIColor clearColor];
  170. self.contentView.frame = downFrame;
  171. } completion:^(BOOL finished) {
  172. [self removeFromSuperview];
  173. self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  174. self.contentView.frame = frame;
  175. }];
  176. }
  177. #pragma mark - event response
  178. - (void)singleBtnClick:(UIButton *)sender
  179. {
  180. switch (sender.tag) {
  181. case kTransportBtnTagCarriageIn:
  182. case kTransportBtnTagTakeSelf:{
  183. self.countryInBtn.selected = NO;
  184. self.takeSelfBtn.selected = NO;
  185. sender.selected = YES;
  186. }
  187. break;
  188. case kTransportBtnTagCancel:{
  189. [self dismissAnimation];
  190. }
  191. break;
  192. case kTransportBtnTagConfirm:{
  193. if (_confirmAction) _confirmAction(self);
  194. [self dismissAnimation];
  195. }
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. - (void)singleTapGestureEvent:(UIGestureRecognizer *)gesture
  202. {
  203. CGPoint location = [gesture locationInView:self];
  204. if (!CGRectContainsPoint(self.contentView.frame, location)) {
  205. [self dismissAnimation];
  206. }
  207. }
  208. #pragma mark - getter && setter
  209. - (UIView *)contentView
  210. {
  211. if (_contentView == nil) {
  212. _contentView = [[UIView alloc]init];
  213. _contentView.backgroundColor = [UIColor whiteColor];
  214. }
  215. return _contentView;
  216. }
  217. - (UILabel *)carriageInLabel
  218. {
  219. if (_carriageInLabel == nil) {
  220. _carriageInLabel = [[UILabel alloc]init];
  221. _carriageInLabel.textColor = UIColorFromRGB(0x333333);
  222. _carriageInLabel.font = [UIFont systemFontOfSize:15];
  223. _carriageInLabel.text = @"国内运费¥10.00";
  224. }
  225. return _carriageInLabel;
  226. }
  227. - (UILabel *)takeSelfLabel
  228. {
  229. if (_takeSelfLabel == nil) {
  230. _takeSelfLabel = [[UILabel alloc]init];
  231. _takeSelfLabel.textColor = UIColorFromRGB(0x333333);
  232. _takeSelfLabel.font = [UIFont systemFontOfSize:15];
  233. _takeSelfLabel.text = @"自取运费¥0.00";
  234. }
  235. return _takeSelfLabel;
  236. }
  237. - (UIButton *)countryInBtn
  238. {
  239. if (_countryInBtn == nil) {
  240. _countryInBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  241. [_countryInBtn setImage:[UIImage imageNamed:@"common_body_unselected_n"] forState:UIControlStateNormal];
  242. [_countryInBtn setImage:[UIImage imageNamed:@"common_body_choose_h"] forState:UIControlStateSelected];
  243. [_countryInBtn addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  244. _countryInBtn.tag = kTransportBtnTagCarriageIn;
  245. _countryInBtn.selected = YES; // 默认选中
  246. }
  247. return _countryInBtn;
  248. }
  249. - (UIButton *)takeSelfBtn
  250. {
  251. if (_takeSelfBtn == nil) {
  252. _takeSelfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  253. [_takeSelfBtn setImage:[UIImage imageNamed:@"common_body_unselected_n"] forState:UIControlStateNormal];
  254. [_takeSelfBtn setImage:[UIImage imageNamed:@"common_body_choose_h"] forState:UIControlStateSelected];
  255. [_takeSelfBtn addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  256. _takeSelfBtn.tag = kTransportBtnTagTakeSelf;
  257. }
  258. return _takeSelfBtn;
  259. }
  260. - (void)setSelectedIndex:(NSInteger)selectedIndex
  261. {
  262. self.takeSelfBtn.selected = self.countryInBtn.selected = NO;
  263. if (selectedIndex == 1) {
  264. self.takeSelfBtn.selected = YES;
  265. }else if (selectedIndex == 0){
  266. self.countryInBtn.selected = YES;
  267. }
  268. }
  269. - (NSInteger)selectedIndex
  270. {
  271. if (self.takeSelfBtn.selected) return 1;
  272. return 0;
  273. }
  274. - (NSInteger)defaultIndex
  275. {
  276. NSInteger index = 0;
  277. if (self.takeSelfBtn.selected) index = 1;
  278. return index;
  279. }
  280. @end