FriendsListView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // FriendsListView.m
  3. // Unity-iPhone
  4. //
  5. // Created by duowan123 on 2022/9/2.
  6. //
  7. #import "FriendsListView.h"
  8. #import "FriendsListCell.h"
  9. #import "FriendsListViewLayout.h"
  10. #import "UserFriendsModel.h"
  11. #import <CoreMotion/CoreMotion.h>
  12. #define itemHight 1
  13. //当前 carousel 状态
  14. @interface FriendsListCarouselCurrentState : NSObject
  15. @property (nonatomic, assign) BOOL isOverturnState; //是否进入翻转状态
  16. @property (nonatomic, assign) BOOL isDragState; //是否处于拖动状态
  17. @property (nonatomic, assign) BOOL isDidScroll; //是否处于滑动状态
  18. @property (nonatomic, assign) BOOL isCenter; //是否处于回到正中状态
  19. @property (nonatomic, assign) BOOL isRestrict; //是否处于限制重力感应
  20. @property (nonatomic, assign) CGFloat lastCarouselPoint;//上一个lastPoint
  21. @end
  22. @implementation FriendsListCarouselCurrentState
  23. @end
  24. typedef void (^OpenAccelerometerUpdatesBlock)(CGFloat value);
  25. typedef void (^OpenGyroUpdatesBlock)(CGFloat value);
  26. @interface FriendsListView ()<
  27. UICollectionViewDelegateFlowLayout,
  28. UICollectionViewDataSource,
  29. UICollectionViewDelegate,
  30. UIScrollViewDelegate
  31. >
  32. @property (nonatomic, strong) FriendsListViewLayout *carouselViewLayout;
  33. @property (nonatomic, strong) FriendsListCarouselCurrentState *currentState;//当前 carousel 状态
  34. @property (nonatomic, strong) UIScrollView *superScrollView;
  35. @property (nonatomic, strong) UICollectionView *collectionView;
  36. @property (nonatomic, assign) CGFloat lasttimePoint;
  37. @property (nonatomic, assign) CGFloat gyrValue;//加速计值
  38. @property (nonatomic, assign) CGSize carouselSize;
  39. @property (nonatomic, assign)CGFloat lastContentOffsetY;
  40. @property (nonatomic, assign) CGFloat cIndex;
  41. @property (nonatomic, assign) BOOL turnLeft;//滑动方向
  42. @property (nonatomic, strong) NSMutableArray *modelArray;
  43. @end
  44. @implementation FriendsListView
  45. #pragma mark - 生命周期
  46. - (instancetype)initWithCoder:(NSCoder *)coder{
  47. self = [super initWithCoder:coder];
  48. if (self){
  49. self.currentState = [[FriendsListCarouselCurrentState alloc]init];
  50. self.currentState.isOverturnState = NO;
  51. self.currentState.isDragState = NO;
  52. self.currentState.isDidScroll = NO;
  53. self.currentState.isCenter = NO;
  54. self.currentState.isRestrict = NO;
  55. self.userInteractionEnabled = NO;
  56. self.collectionView.scrollEnabled = NO;
  57. }
  58. return self;
  59. }
  60. //
  61. -(void)layoutSubviews{
  62. NSLog(@"layoutSubviewslayoutSubviewslayoutSubviewslayoutSubviews");
  63. self.carouselSize = self.frame.size;
  64. [self createCollectionView];
  65. [self.collectionView reloadData];
  66. [self.collectionView layoutIfNeeded];
  67. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  68. [self setpoint];
  69. });
  70. NSLog(@"这里运行一次");
  71. //监听滑动结束的状态
  72. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificationAction:) name:@"ThisIsANoticafication" object:nil];
  73. }
  74. //
  75. -(void)setpoint{
  76. NSLog(@"setpoint");
  77. [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentOffset.y) animated:NO];
  78. [self.collectionView setContentOffset:CGPointMake(0+((self.modelArray.count/2)*(self.carouselSize.height*itemHight)), self.collectionView.contentOffset.y) animated:NO];
  79. self.currentState.lastCarouselPoint = 0;
  80. [self setSlideEnd];
  81. }
  82. #pragma mark - loadData
  83. -(void)setArrayData:(NSMutableArray<UserFriendsModel *> *)array{
  84. if (array.count<=0){
  85. return;
  86. }
  87. if (array.count == self.modelArray.count) {//没有新的数据源不刷新
  88. return;
  89. }
  90. [self.modelArray removeAllObjects];
  91. [self.modelArray addObjectsFromArray:array];
  92. [self.collectionView reloadData];
  93. [self.collectionView layoutIfNeeded];
  94. // 当只有一个数据源的时候 让cell滑到正中间
  95. if (array.count==1){
  96. // NSLog(@"设备列表只有一个数据源");
  97. _cIndex = 0;
  98. [self testAction];
  99. }else{
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. if (array.count>=5){
  102. }else{
  103. [self setpoint];
  104. }
  105. });
  106. }
  107. // 当只有两个数据源的时候 添加手势 防止collectionView数据少 没有占满屏幕 时候滑不动的BUG
  108. if (array.count>=2){
  109. UISwipeGestureRecognizer * left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  110. [left setDirection:(UISwipeGestureRecognizerDirectionLeft)];
  111. [self.collectionView addGestureRecognizer:left];
  112. UISwipeGestureRecognizer * right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  113. [right setDirection:(UISwipeGestureRecognizerDirectionRight)];
  114. [self.collectionView addGestureRecognizer:right];
  115. }
  116. self.collectionView.scrollEnabled = NO;
  117. }
  118. #pragma mark -- UISwipeGestureRecognizer
  119. - (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
  120. if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft){
  121. [self yw_gotoNext:1];
  122. }
  123. if(recognizer.direction == UISwipeGestureRecognizerDirectionRight){
  124. [self yw_gotoNext:-1];
  125. }
  126. }
  127. #pragma mark -- scrollView delegate
  128. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  129. NSLog(@"scrollViewWillBeginDragging");
  130. self.lastContentOffsetY = scrollView.contentOffset.x;
  131. }
  132. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  133. // NSLog(@"scrollViewDidScroll");
  134. if (scrollView.contentOffset.x < self.lastContentOffsetY){
  135. // NSLog(@"左左");
  136. self.turnLeft = YES;
  137. }else if (scrollView.contentOffset.x > self.lastContentOffsetY){
  138. // NSLog(@"右右");
  139. self.turnLeft = NO;
  140. }
  141. }
  142. #pragma mark -- collectionView delegate
  143. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  144. NSLog(@"self.modelArray.count - %ld",self.modelArray.count);
  145. return self.modelArray.count;
  146. }
  147. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  148. FriendsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsListCell" forIndexPath:indexPath];
  149. UserFriendsModel * model = self.modelArray[indexPath.row];
  150. [cell setCarouselCellModel:model];
  151. __weak typeof(self) weakSelf = self;
  152. //单击cell 滚到cell位置
  153. cell.tapGestureBlock = ^(NSInteger i){
  154. if (i==_cIndex) {
  155. NSLog(@"didSelectItemAtIndexPath 点击的 是 当前");
  156. _cIndex = i;
  157. if ([self.delegate respondsToSelector:@selector(connectFriend:)]){
  158. [self.delegate connectFriend:model];
  159. }
  160. }else{
  161. NSLog(@"didSelectItemAtIndexPath 点击的 不是 当前");
  162. _cIndex = i;
  163. [weakSelf testAction];
  164. }
  165. };
  166. //默认选中
  167. [self jugeSelected];
  168. return cell;
  169. }
  170. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  171. // NSLog(@"选中了 ==== %ld",(long)indexPath.row);
  172. FriendsListCell * cell = (FriendsListCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
  173. cell.cusSelected = YES;
  174. //将当前滚动到的cell->蓝牙 回调给SearchDeviceViewController
  175. UserFriendsModel * model = self.modelArray[indexPath.row];
  176. if ([self.delegate respondsToSelector:@selector(setCurrentFriend:)]){
  177. [self.delegate setCurrentFriend:model];
  178. }
  179. }
  180. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
  181. // NSLog(@"未选中 ==== %ld",(long)indexPath.row);
  182. FriendsListCell * cell = (FriendsListCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
  183. cell.cusSelected = NO;
  184. }
  185. #pragma mark - privately method
  186. /**
  187. 回到屏幕正中间
  188. */
  189. -(void)setSlideEnd{
  190. NSLog(@"setSlideEnd");
  191. CGPoint collectionViewPoint = self.collectionView.contentOffset;
  192. if ( self.currentState.lastCarouselPoint == collectionViewPoint.x||self.currentState.isDragState){
  193. return;
  194. }
  195. CGFloat viewHeight = CGRectGetWidth(self.collectionView.frame);
  196. CGFloat itemHeight = self.carouselSize.height*itemHight;
  197. CGFloat index = roundf((self.collectionView.contentOffset.x+ viewHeight / 2 - itemHeight / 2) /itemHeight)-1;
  198. // NSLog(@"setSlideEnd:x[%f]----y[%f]---index[%f]",collectionViewPoint.x,collectionViewPoint.y,index);
  199. collectionViewPoint.x = itemHeight * index + itemHeight / 2 - viewHeight / 2;
  200. [self.collectionView setContentOffset:CGPointMake(collectionViewPoint.x, collectionViewPoint.y) animated:YES];
  201. self.currentState.lastCarouselPoint = self.collectionView.contentOffset.x;
  202. self.currentState.isDidScroll = YES;
  203. _cIndex = index;
  204. // NSLog(@"setSlideEnd:x[%f]----y[%f]---index[%f]",collectionViewPoint.x,collectionViewPoint.y,index);
  205. [self jugeSelected];
  206. }
  207. #pragma mark yw add 限制点击事件
  208. - (void)yw_gotoNext:(NSInteger)index{
  209. NSLog(@"yw_gotoNext:(NSInteger)index = %d %ld",(int)_cIndex,(long)index);
  210. if ((int)_cIndex<1 && index == -1){//滑到最左边
  211. return;
  212. }else if ((int)_cIndex>self.modelArray.count-2 && index == 1){//滑到最右边
  213. return;
  214. }else{
  215. _cIndex = _cIndex + index;
  216. [self testAction];
  217. }
  218. }
  219. //setContentOffset到对应位置
  220. - (void)testAction{
  221. NSLog(@"testAction");
  222. CGFloat viewHeight = CGRectGetWidth(self.collectionView.frame);
  223. CGFloat itemHeight = self.carouselSize.height*itemHight;
  224. CGFloat offsetX = (_cIndex + (itemHeight/2)/itemHeight - (viewHeight/2)/itemHeight)*itemHeight;
  225. // NSLog(@"testAction:%f----_cIndex:%f",offsetX,_cIndex);
  226. [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
  227. [self jugeSelected];
  228. }
  229. #pragma mark leon add
  230. - (void)getNotificationAction:(NSNotification *)notification{
  231. NSDictionary * infoDic = [notification object];
  232. // 这样就得到了我们在发送通知时候传入的字典了
  233. NSNumber * number = infoDic[@"parameter"];
  234. NSInteger i = [number integerValue];
  235. NSLog(@"通知 number ==== %@ _cIndex = %f self.modelArray.count = %lu",number,_cIndex,(unsigned long)self.modelArray.count);
  236. _cIndex = i;
  237. [self endScroll];
  238. if (i==1&&self.turnLeft==YES){
  239. NSLog(@"滑到最左边");
  240. _cIndex = 0;
  241. [self endScroll];
  242. }
  243. if (i==self.modelArray.count-2&&self.turnLeft==NO){
  244. NSLog(@"滑到最右边");
  245. _cIndex = self.modelArray.count-1;
  246. [self endScroll];
  247. }
  248. }
  249. //
  250. -(void)endScroll{
  251. NSLog(@"endScroll");
  252. __weak typeof(self)WeakSelf = self;
  253. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  254. CGFloat viewHeight = CGRectGetWidth(self.collectionView.frame);
  255. CGFloat itemHeight = self.carouselSize.height*itemHight;
  256. CGFloat offsetX = (_cIndex + (itemHeight/2)/itemHeight - (viewHeight/2)/itemHeight)*itemHeight;
  257. //滚动到对应位置
  258. [WeakSelf.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
  259. //当前选中的是哪个
  260. [WeakSelf jugeSelected];
  261. });
  262. }
  263. //判断哪个cell是选中的
  264. -(void)jugeSelected{
  265. NSLog(@"jugeSelected (NSInteger)_cIndex = %ld",(NSInteger)_cIndex);
  266. //判断哪个是选中的
  267. for (NSInteger i =0; i<self.modelArray.count; i++){
  268. if (i == (NSInteger)_cIndex){//选中的cell
  269. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(NSInteger)_cIndex inSection:0];
  270. if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]){
  271. [self.collectionView.delegate collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
  272. }
  273. }else{
  274. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  275. if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]){
  276. [self.collectionView.delegate collectionView:self.collectionView didDeselectItemAtIndexPath:indexPath];
  277. }
  278. }
  279. }
  280. }
  281. #pragma mark - Lazy
  282. -(FriendsListViewLayout *)carouselViewLayout{
  283. if (!_carouselViewLayout){
  284. CGFloat itemsHeight = _carouselSize.height*itemHight;
  285. _carouselViewLayout = [[FriendsListViewLayout alloc] init];
  286. _carouselViewLayout.itemSize = CGSizeMake(itemsHeight, itemsHeight);
  287. _carouselViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  288. }
  289. return _carouselViewLayout;
  290. }
  291. -(void)createCollectionView{
  292. if (!self.collectionView){
  293. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height ) collectionViewLayout:self.carouselViewLayout];
  294. self.collectionView.dataSource = self;
  295. self.collectionView.delegate = self;
  296. self.collectionView.pagingEnabled = NO;
  297. self.collectionView.showsVerticalScrollIndicator = NO;
  298. self.collectionView.showsHorizontalScrollIndicator = NO;
  299. [self.collectionView registerNib:[UINib nibWithNibName:@"FriendsListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FriendsListCell"];
  300. self.collectionView.backgroundColor = [UIColor whiteColor];
  301. [self addSubview:self.collectionView];
  302. self.collectionView.backgroundColor = [UIColor clearColor];
  303. }
  304. }
  305. -(NSMutableArray *)modelArray{
  306. if (!_modelArray){
  307. _modelArray = [[NSMutableArray alloc]init];
  308. }
  309. return _modelArray;
  310. }
  311. @end