亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS開發UICollectionView實現拖拽效果

2020-07-26 02:21:41
字體:
來源:轉載
供稿:網友

一.介紹

iOS9提供API實現單元格排序功能,使用UICollectionView及其代理方法。iOS9之后有自帶方法可以實現該效果,只需添加長按手勢,實現手勢方法和調用iOS9的API交換數據,iOS9之前需要自己寫方法實現這效果,除了要添加長按手勢,這里還需要利用截圖替換原理,手動計算移動位置來處理視圖交換和數據交換。

二.方法和步驟

1.創建工程項目和視圖控制器,如下圖

2.聲明對象和設置代理和數據源代理

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) NSMutableArray *dataArr;@property (nonatomic, strong) UICollectionView *collectionView;/**之前選中cell的NSIndexPath*/@property (nonatomic, strong) NSIndexPath *oldIndexPath;/**單元格的截圖*/@property (nonatomic, strong) UIView *snapshotView;/**之前選中cell的NSIndexPath*/@property (nonatomic, strong) NSIndexPath *moveIndexPath; @end

3.初始化UICollectionView,并添加長按手勢,在viewDidLoad中初始化

CGFloat SCREEN_WIDTH = self.view.frame.size.width;  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];  flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-40.0)/3, (SCREEN_WIDTH-40.0)/3);  UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50.0, SCREEN_WIDTH, (SCREEN_WIDTH-40.0)/3+20.0) collectionViewLayout:flowLayout];  collectionView.dataSource = self;  collectionView.delegate = self;  collectionView.backgroundColor = [UIColor whiteColor];  [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"uicollectionviewcell"];  [self.view addSubview:self.collectionView = collectionView];    // 添加長按手勢  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];  [collectionView addGestureRecognizer:longPress];

4.實例化數據源,(50個隨機顏色,透明度0.8),在viewDidLoad中初始化

self.dataArr = [[NSMutableArray alloc] init];for (NSInteger index = 0; index < 50; index ++) {    CGFloat hue = (arc4random()%256/256.0); //0.0 到 1.0    CGFloat saturation = (arc4random()%128/256.0)+0.5; //0.5 到 1.0    CGFloat brightness = (arc4random()%128/256.0)+0.5; //0.5 到 1.0    UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:0.5];    [self.dataArr addObject:color];  }

5.實現UICollectionView的UICollectionViewDataSource的兩個必須實現的方法

#pragma mark - UICollectionViewDataSource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{  return self.dataArr.count;} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{  UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"uicollectionviewcell" forIndexPath:indexPath];  cell.backgroundColor = self.dataArr[indexPath.row];  return cell;}

6.重點來了,實現長按手勢方法

#pragma mark - 長按手勢- (void)handlelongGesture:(UILongPressGestureRecognizer *)longPress{  if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {    [self action:longPress];  } else {    [self iOS9_Action:longPress];  }}

7.iOS9之后的實現

#pragma mark - iOS9 之后的方法- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{  // 返回YES允許row移動  return YES;} - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  //取出移動row數據  id color = self.dataArr[sourceIndexPath.row];  //從數據源中移除該數據  [self.dataArr removeObject:color];  //將數據插入到數據源中的目標位置  [self.dataArr insertObject:color atIndex:destinationIndexPath.row];} - (void)iOS9_Action:(UILongPressGestureRecognizer *)longPress{  switch (longPress.state) {    case UIGestureRecognizerStateBegan:    { //手勢開始      //判斷手勢落點位置是否在row上      NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];      if (indexPath == nil) {        break;      }      UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];      [self.view bringSubviewToFront:cell];      //iOS9方法 移動cell      [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];    }      break;    case UIGestureRecognizerStateChanged:    { // 手勢改變      // iOS9方法 移動過程中隨時更新cell位置      [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:self.collectionView]];    }      break;    case UIGestureRecognizerStateEnded:    { // 手勢結束      // iOS9方法 移動結束后關閉cell移動      [self.collectionView endInteractiveMovement];    }      break;    default: //手勢其他狀態      [self.collectionView cancelInteractiveMovement];      break;  }}

8.iOS9之前的實現

#pragma mark - iOS9 之前的方法- (void)action:(UILongPressGestureRecognizer *)longPress{  switch (longPress.state) {    case UIGestureRecognizerStateBegan:    { // 手勢開始      //判斷手勢落點位置是否在row上      NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];      self.oldIndexPath = indexPath;      if (indexPath == nil) {        break;      }      UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];      // 使用系統的截圖功能,得到cell的截圖視圖      UIView *snapshotView = [cell snapshotViewAfterScreenUpdates:NO];      snapshotView.frame = cell.frame;      [self.view addSubview:self.snapshotView = snapshotView];      // 截圖后隱藏當前cell      cell.hidden = YES;            CGPoint currentPoint = [longPress locationInView:self.collectionView];      [UIView animateWithDuration:0.25 animations:^{        snapshotView.transform = CGAffineTransformMakeScale(1.05, 1.05);        snapshotView.center = currentPoint;      }];    }      break;    case UIGestureRecognizerStateChanged:    { // 手勢改變      //當前手指位置 截圖視圖位置隨著手指移動而移動      CGPoint currentPoint = [longPress locationInView:self.collectionView];      self.snapshotView.center = currentPoint;      // 計算截圖視圖和哪個可見cell相交      for (UICollectionViewCell *cell in self.collectionView.visibleCells) {        // 當前隱藏的cell就不需要交換了,直接continue        if ([self.collectionView indexPathForCell:cell] == self.oldIndexPath) {          continue;        }        // 計算中心距        CGFloat space = sqrtf(pow(self.snapshotView.center.x - cell.center.x, 2) + powf(self.snapshotView.center.y - cell.center.y, 2));        // 如果相交一半就移動        if (space <= self.snapshotView.bounds.size.width / 2) {          self.moveIndexPath = [self.collectionView indexPathForCell:cell];          //移動 會調用willMoveToIndexPath方法更新數據源          [self.collectionView moveItemAtIndexPath:self.oldIndexPath toIndexPath:self.moveIndexPath];          //設置移動后的起始indexPath          self.oldIndexPath = self.moveIndexPath;          break;        }      }    }      break;    default:    { // 手勢結束和其他狀態      UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.oldIndexPath];      // 結束動畫過程中停止交互,防止出問題      self.collectionView.userInteractionEnabled = NO;      // 給截圖視圖一個動畫移動到隱藏cell的新位置      [UIView animateWithDuration:0.25 animations:^{        self.snapshotView.center = cell.center;        self.snapshotView.transform = CGAffineTransformMakeScale(1.0, 1.0);      } completion:^(BOOL finished) {        // 移除截圖視圖,顯示隱藏的cell并開始交互        [self.snapshotView removeFromSuperview];        cell.hidden = NO;        self.collectionView.userInteractionEnabled = YES;      }];    }      break;  }}

三.iOS9之后添加的API如下

// Support for reordering- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); // returns NO if reordering was prevented from beginning - otherwise YES- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美大全免费观看电视剧大泉洋| 成人免费看片视频| 成人欧美一区二区三区在线湿哒哒| 午夜美女久久久久爽久久| 国产成人拍精品视频午夜网站| 日韩av三级在线观看| 丁香五六月婷婷久久激情| 亚洲成人久久久久| 亚洲男人的天堂在线| 欧美黄色片免费观看| 国内精品模特av私拍在线观看| 97久久超碰福利国产精品…| 91色p视频在线| 国产精品自在线| 美女啪啪无遮挡免费久久网站| 国产精品99久久久久久人| 亚洲欧美激情一区| 欧美一区二区色| 久久韩国免费视频| 欧美一级视频在线观看| 日韩av影视综合网| 亚洲精品久久久久久久久久久久久| 欧美日本黄视频| 欧美丝袜一区二区| 国产精品无码专区在线观看| 欧美限制级电影在线观看| 88国产精品欧美一区二区三区| 欧美黑人xxxⅹ高潮交| 中文字幕一区二区精品| 亚洲国产成人在线视频| 欧美日韩另类字幕中文| 亚洲free性xxxx护士白浆| 97超碰蝌蚪网人人做人人爽| 亚洲第一区第一页| 亚洲欧美日韩在线高清直播| 中文字幕亚洲专区| 69av成年福利视频| 亚洲天堂第二页| 色偷偷噜噜噜亚洲男人| 亚洲国产精品推荐| 亚洲欧美激情四射在线日| 青青草成人在线| 精品久久香蕉国产线看观看gif| 欧美综合第一页| 亚洲欧洲xxxx| 国产婷婷成人久久av免费高清| 欧美黑人性猛交| 国产亚洲精品久久久久动| 亚洲日韩中文字幕在线播放| 亚洲人成在线一二| 亚洲一区国产精品| 91久久精品美女| 亚洲视频999| 91国内揄拍国内精品对白| 91精品免费久久久久久久久| 精品一区二区三区三区| 91社影院在线观看| 日韩成人在线视频网站| 清纯唯美日韩制服另类| 亚洲午夜国产成人av电影男同| 在线成人激情视频| 亚洲精品久久视频| 国产精品都在这里| 国产69久久精品成人| 亚洲午夜未删减在线观看| 欧美极品少妇xxxxⅹ免费视频| 91国产美女在线观看| 久久69精品久久久久久久电影好| 91精品国产自产在线观看永久| 国产精品福利在线| 日韩在线观看免费| 欧美成人免费一级人片100| 亚洲国产精品人久久电影| 国产成人综合亚洲| 91精品国产免费久久久久久| 国模极品一区二区三区| 欧美激情精品久久久久久变态| 久久久久久一区二区三区| 国模私拍一区二区三区| 国产亚洲精品久久久| 欧美另类69精品久久久久9999| 国产亚洲精品久久久久久777| 亚洲视频在线观看视频| 欧美激情视频在线免费观看 欧美视频免费一| 国产日韩在线看片| www.午夜精品| 精品亚洲精品福利线在观看| 热久久免费国产视频| 国产精品久久久久久久av大片| 国产亚洲福利一区| 欧美成年人视频| 久久久久女教师免费一区| 国产精品露脸自拍| 国产精品久久视频| 久久久久久久久久婷婷| 亚洲国产精品一区二区久| 琪琪第一精品导航| 日韩亚洲第一页| 95av在线视频| 成人97在线观看视频| 欧美日韩国产一区二区三区| 日本精品在线视频| 国产成人一区二区三区电影| 色婷婷av一区二区三区在线观看| 久久久久久国产精品久久| 精品久久久久久中文字幕一区奶水| 欧美又大又粗又长| 伦伦影院午夜日韩欧美限制| 国产九九精品视频| 青青草原成人在线视频| 欧美日韩一区二区免费在线观看| 色妞色视频一区二区三区四区| 91成人在线播放| 亚洲人成77777在线观看网| 日韩av高清不卡| 亚洲片av在线| 91久久久久久久| 国产精品美女久久久免费| 亚洲精品在线91| 美女av一区二区三区| 国产精品va在线播放我和闺蜜| 国产精品三级在线| 国产精品视频专区| 欧美性感美女h网站在线观看免费| 欧美高清性猛交| 久久久www成人免费精品张筱雨| 国产精品扒开腿做| 国产激情久久久久| 欧美性生交xxxxx久久久| 亚洲天堂男人天堂| 亚洲人成电影网站| 色综合91久久精品中文字幕| 欧美精品在线免费播放| 国产亚洲一区精品| 久久伊人色综合| 亚洲精品狠狠操| 亚洲高清在线观看| 国产精品日韩在线| 欧美性做爰毛片| 国产精品嫩草影院一区二区| 国产婷婷97碰碰久久人人蜜臀| 欧美午夜激情视频| 日本成人激情视频| 久久久免费电影| 国产啪精品视频网站| 中文字幕在线看视频国产欧美在线看完整| 久久这里只有精品99| 中文字幕亚洲一区二区三区| 高跟丝袜一区二区三区| 欧美午夜性色大片在线观看| 51久久精品夜色国产麻豆| 国产精品网红直播| 精品国偷自产在线| 欧美有码在线观看视频| 在线电影中文日韩| 欧美日韩国产va另类| 91亚洲一区精品| 色偷偷av亚洲男人的天堂| 日韩在线视频一区| 隔壁老王国产在线精品| 日韩av手机在线观看| 日韩欧美在线视频观看| 亚洲成人精品av| 97免费中文视频在线观看|