UGCKitEffectSelectView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitEffectSelectView.h"
  3. #import "UGCKit_UIViewAdditions.h"
  4. #import "UGCKitColorMacro.h"
  5. #define EFFCT_COUNT 4
  6. #define EFFCT_IMAGE_WIDTH 50 * kScaleY
  7. #define EFFCT_IMAGE_SPACE 20
  8. @implementation UGCKitEffectSelectView
  9. {
  10. UIScrollView *_effectSelectView;
  11. NSMutableArray *_selectViewList;
  12. }
  13. - (instancetype)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. _effectSelectView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.ugckit_width,EFFCT_IMAGE_WIDTH + 20)];
  18. [self addSubview:_effectSelectView];
  19. _selectViewList = [NSMutableArray array];
  20. }
  21. return self;
  22. }
  23. - (void)setSelectedIndex:(NSInteger)selectedIndex
  24. {
  25. if (self.momentary) return;
  26. if (selectedIndex < _selectViewList.count) {
  27. _selectedIndex = selectedIndex;
  28. for (UIImageView *view in _selectViewList) {
  29. if (view.tag == selectedIndex) {
  30. view.hidden = NO;
  31. }else{
  32. view.hidden = YES;
  33. }
  34. }
  35. }
  36. }
  37. - (void)setEffectList:(NSArray<UGCKitEffectInfo *> *)effecList
  38. {
  39. [self setEffectList:effecList momentary:NO];
  40. }
  41. - (void)setEffectList:(NSArray<UGCKitEffectInfo *> *)effecList momentary:(BOOL)momentary
  42. {
  43. self.momentary = momentary;
  44. [_effectSelectView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  45. [_selectViewList removeAllObjects];
  46. CGFloat space = floorf(20 * kScaleX);
  47. CGFloat buttonSize = floorf(EFFCT_IMAGE_WIDTH);
  48. for (int i = 0 ; i < effecList.count ; i ++){
  49. UGCKitEffectInfo *info = effecList[i];
  50. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  51. [btn setFrame:CGRectMake(space + (space + buttonSize) * i, 0, buttonSize, buttonSize)];
  52. if (info.animateIcons) {
  53. UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:btn.bounds];
  54. animatedImageView.animationImages = info.animateIcons;
  55. if (info.isSlow) {
  56. animatedImageView.animationDuration = 1.0 / 15 * effecList[i].animateIcons.count;
  57. }
  58. [animatedImageView startAnimating];
  59. [btn addSubview:animatedImageView];
  60. } else {
  61. [btn setImage:effecList[i].icon forState:UIControlStateNormal];
  62. }
  63. btn.layer.cornerRadius = EFFCT_IMAGE_WIDTH / 2.0;
  64. btn.layer.masksToBounds = YES;
  65. btn.titleLabel.numberOfLines = 0;
  66. btn.tag = i;
  67. [btn addTarget:self action:@selector(beginPress:) forControlEvents:UIControlEventTouchDown];
  68. [btn addTarget:self action:@selector(endPress:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
  69. [btn addTarget:self action:@selector(onTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  70. UIImageView *selectView = [[UIImageView alloc]initWithFrame:btn.frame];
  71. [selectView setImage:effecList[i].selectIcon];
  72. selectView.hidden = YES;
  73. selectView.tag = i;
  74. [_selectViewList addObject:selectView];
  75. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(btn.ugckit_x - space/2, btn.ugckit_bottom + 8, btn.ugckit_width+space, 12)];
  76. label.text = effecList[i].name;
  77. label.adjustsFontSizeToFitWidth = YES;
  78. label.minimumScaleFactor = 0.5;
  79. label.textColor = [UIColor whiteColor];
  80. label.textAlignment = NSTextAlignmentCenter;
  81. label.font = [UIFont systemFontOfSize:10];
  82. [_effectSelectView addSubview:btn];
  83. [_effectSelectView addSubview:selectView];
  84. [_effectSelectView addSubview:label];
  85. _effectSelectView.contentSize = CGSizeMake(btn.ugckit_right, buttonSize);
  86. }
  87. if (_effectSelectView.contentSize.width > self.ugckit_width) {
  88. _effectSelectView.alwaysBounceHorizontal = YES;
  89. }else{
  90. _effectSelectView.alwaysBounceHorizontal = NO;
  91. }
  92. }
  93. //开始按压
  94. -(void) beginPress: (UIButton *) button {
  95. CGFloat offset = _effectSelectView.contentOffset.x;
  96. CGFloat diff = _effectSelectView.contentSize.width - _effectSelectView.bounds.size.width;
  97. if (offset < 0 || (diff > 0 && offset > diff)) {
  98. // 在回弹区域会触发button事件被cancel,导致收不到 TouchEnd 事件
  99. return;
  100. }
  101. [self.delegate onEffectBtnBeginSelect:button];
  102. for (UIImageView *view in _selectViewList) {
  103. if (view.tag == button.tag) {
  104. view.hidden = NO;
  105. }else{
  106. view.hidden = YES;
  107. }
  108. }
  109. }
  110. //结束按压
  111. -(void) endPress: (UIButton *) button {
  112. if (self.momentary) {
  113. [_selectViewList enumerateObjectsUsingBlock:^(UIImageView * obj, NSUInteger idx, BOOL * _Nonnull stop) {
  114. obj.hidden = YES;
  115. }];
  116. } else {
  117. _selectedIndex = button.tag;
  118. }
  119. [self.delegate onEffectBtnEndSelect:button];
  120. }
  121. //按压
  122. -(void) onTouchUpInside:(UIButton *) button {
  123. [self.delegate onEffectBtnSelected:button];
  124. }
  125. @end