ios - 通过长按手势交换 UICollectionView 单元格位置
<p><p>我在弄清楚我在这里做错了什么时遇到了一些麻烦。我的目标是能够拖动 CollectionViewCell 并让它用拖动的单元格替换目标单元格,同时让替换的单元格接管最初拖动的单元格上的位置。这甚至让我感到困惑!我可以拖动并替换单元格,但我的问题是,当我发生时,我会在集合中留下越来越多的空单元格..</p>
<p>我对 Objective-C相当陌生,所以很可能我在这里遗漏了一些愚蠢的东西。任何帮助将不胜感激。希望我包含了足够的相关代码来帮助并链接图像以显示我的问题...</p>
<p>干杯李</p>
<p> <a href="http://www.leighvdb.com/collectionviewissue.jpg" rel="noreferrer noopener nofollow">Collection View Issue Image</a> </p>
<pre><code>- (void)viewDidLoad
{
;
// Do any additional setup after loading the view.
;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
-(void)getPlayerPositions
{
SQL_PlayerTracker *pt = [init];
playerPositionsARR = [mutableCopy];
;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OBJ_Positions *s = ;
CVC_PlayerPositions *pcell = (CVC_PlayerPositions *);
pcell.playerName.text = s.playerName;
pcell.positionName.text = s.position;
pcell.playerNumber.text = ;
if (pcell.selected) {
pcell.backgroundColor = ;
}
return pcell;
}
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
NSLog(@"MOVED INDEX %i TO %i",indexPath.row,newIndexPath.row);
OBJ_Positions *oldPlayer = ;
OBJ_Positions *newPlayer = ;
//REMOVE TARGET POSITION FROM ARRAY
;
//INSERT NEW PLAYER POSITION IN TARGET POSITION
;
//INSERT OLD PLAYER IN MOVED PLAYERS POSITION
;
//REMOVE ADDED POSITION OBJECT FROM INSERTION
;
;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
OBJ_Positions *p = ;
NSLog(@"Selected Player IS %@",p.playerName);
}
#pragma mark - MOVE COLLECTION VIEW CELLS
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
-(void)dragPlayer:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"State Began");
CGPoint point = ;
originalCellIndex = ;
if (originalCellIndex) {
UICollectionViewCell *cell = ;
beginView =;
beginView.backgroundColor = ;
;
beginView.center = ;
originalStartPoint = beginView.center;
;
;
OBJ_Players *pd = ;
NSLog(@"SELECT PLAYER NAME IS - %@",pd.playerName);
return;
}
}
if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"State Changed");
if (!beginView)
return;
CGPoint location = ;
CGPoint translation;
translation.x = location.x - originalStartPoint.x;
translation.y = location.y - originalStartPoint.y;
CGAffineTransform theTransform = beginView.transform;
theTransform.tx = translation.x;
theTransform.ty = translation.y;
beginView.transform = theTransform;
return;
}
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"STATE ENDED");
if (beginView) {
CGPoint epoint = ;
endCellIndex = ;
if(endCellIndex)
{
OBJ_Positions *p = ;
NSLog(@"POSITION IS - %@ AND PLAYER DRAGGED IS %@",p.position,p.playerName);
;
;
return;
}
}
}
}
- (IBAction)movingPlayer:(id)sender {
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我相信这个链接就是你要找的。
<a href="https://github.com/lxcid/LXReorderableCollectionViewFlowLayout" rel="noreferrer noopener nofollow">https://github.com/lxcid/LXReorderableCollectionViewFlowLayout</a> </p>
<p>它扩展了 <code>UICollectionViewFlowLayout</code> 以支持单元格的重新排序。类似于 iBook 中的长按和平移。</p>
<p> <img src="/image/a6Zjr.png" alt="from the github readme"/> </p></p>
<p style="font-size: 20px;">关于ios - 通过长按手势交换 UICollectionView 单元格位置,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18738182/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18738182/
</a>
</p>
页:
[1]