質問

2014+のiPhoneまたはiPadでは、[ホーム]ボタンをダブルクリックして[App Manager]

を表示します。

画像の入力ここで

これは左右のUICollectionViewですが、「スワイプアウェイ」ジェスチャーがあります。どうやってやっていますか?UICollectionViewからセルを "削除するのが簡単ではありません。


グーグラーの脚注..「剥がれ」、「引き裂き」の一般的な問題は、コレクションビューからの1つのセルです、ここに完全なきちんと説明があります。/ 24339705/294884"> https://stackoverflow.com/a/24339705/294884 誰かに役立つことを願っています。

役に立ちましたか?

解決

あなたの質問に対するコメントよりはるかに簡単なことができます。

あなたのセルにはビュー(ドラッグしようとするもの)を含めるべきであり、そのビューにUipangesturecognizerを追加します。

ジェスチャーのアクション方式では、ビューを上下に移動し、それを削除したいとはるかに遠く離れたときに、それをアニメートするだけです。ここではこの部分を扱ってたくさんの質問があります。

これはあなたのコレクションにギャップを残し、そして今あなたは物事を動かす必要があります。これは非常に簡単です:

[_collectionView performBatchUpdates:^{
   [_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
     // you might want to remove the data from the data source here so the view doesn't come back to life when the collection view is reloaded.
}];
.

削除されたセルの右側のものはスライドしていて、私たちはみんな良いです。

乗り越えた問題:あなたのジェスチャー認識士とコレクションビューが一緒に素敵なプレーをしていることを確認してください。ありがたいことに、それはあまりにもトリッキーではありません。

[_collectionView.panGestureRecognizer requireGestureRecognizerToFail:pgr]; //where pgr is the recognizer you made for dragging the view off
.

これは、コレクションビューのパンジェスチャーがそのことをするために、あなたのものは失敗する必要があります。そのため、上下にパンしているときにのみ機能するようにして、コレクションビューに右のパンを右にしてください。あなたのジェスチャー認識者の代表者では、X軸またはy軸に移動しているかどうかを簡単にチェックする以下の方法を実装してください。

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translation =[gestureRecognizer translationInView:self.view];

    return(translation.x * translation.x > translation.y * translation.y);
}
.

他のヒント

私はこの機能を探していて、@mbehanの提案を使ってUicollectionViewを使ってこの機能を偽造しました。

私がコレクションセル(透明な背景)上で小さいサイズのビューを追加し、CollectionViewに1つのパンジェスチャー(各セルではなく)を追加してからビューを移動し、それはセルのように見えます動いている。ビューがある点に達すると、最初にそれを非表示にしてから、コレクションビューセルを削除します。

セル階層:CollectionViewCell - > View(タグ値== 2) - > Uilabel(タグ値== 1) ラベルはプレースホルダー目的のために使用されます。

私は以下のコードを投稿しています:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cards" forIndexPath:indexPath];
    UILabel *lblNumber = (UILabel*)[cell.contentView viewWithTag:1];
    UIView *viewTouch = (UIView*)[cell.contentView viewWithTag:2];
    [viewTouch setHidden:NO];
    [lblNumber setText:arrCards[indexPath.row]];

    return cell;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 50, 0, 30);
  }

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{

    if([gestureRecognizer isEqual:panGesture]) {
    CGPoint point = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:collectionView_];
        if(point.x != 0) { //adjust this condition if you want some leniency on the X axis
        //The translation was on the X axis, i.e. right/left,
        //so this gesture recognizer shouldn't do anything about it
        return NO;
        }
   }
   return YES;
}

- (IBAction)panGestureCalled:(UIPanGestureRecognizer *)sender {
    yFromCenter = [sender translationInView:collectionView_].y; //%%% positive for up, negative for down

    UIView *view = sender.view;
    CGPoint location = [view.superview convertPoint:view.center toView:collectionView_];
    NSIndexPath *indexPath = [collectionView_ indexPathForItemAtPoint:location];
    UICollectionViewCell *cell = [collectionView_ cellForItemAtIndexPath:indexPath];
    UIView *touchView = (UIView*)[cell.contentView viewWithTag:2];


    switch (sender.state) {
      case UIGestureRecognizerStateBegan:{
        originalPoint = touchView.center;
        break;
    };
      case UIGestureRecognizerStateChanged:{
        touchView.center = CGPointMake(originalPoint.x , originalPoint.y + yFromCenter);

        break;
    };
        //%%% let go of the card
      case UIGestureRecognizerStateEnded: {
        CGFloat velocityY = (0.2*[(UIPanGestureRecognizer*)sender velocityInView:collectionView_].y);

        if (velocityY < -30 && yFromCenter<0) {
            [self hideView:touchView withDuration:0.2 andIndexPath:indexPath];

        }else if ((yFromCenter< 0 && yFromCenter > -200) || yFromCenter > 0){

            CGFloat animationDuration = (ABS(velocityY)*.0002)+.2;
            [self resettleViewToOriginalPosition:touchView andDuration:animationDuration];

        }else
            [self hideView:touchView withDuration:0.2 andIndexPath:indexPath];

    };
        break;
      case UIGestureRecognizerStatePossible:break;
      case UIGestureRecognizerStateCancelled:break;
      case UIGestureRecognizerStateFailed:break;
  }
}


-(void)resettleViewToOriginalPosition:(UIView*)view andDuration:(float)duration{
[UIView animateWithDuration:duration
                      delay:0.0f
                    options: UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     [view setCenter:originalPoint];
 }
                 completion:^(BOOL finished)
 {

 }];
}
- (void)hideView:(UIView*)view withDuration:(float)duration andIndexPath:(NSIndexPath*)indexPath
{

[UIView animateWithDuration:duration
                      delay:0.0f
                    options: UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     CGRect frame = view.frame;
     frame.origin.y = -300;
     view.frame = frame;
 }
                 completion:^(BOOL finished)
 {
     [view setHidden:YES];
     CGRect frame = view.frame;
     frame.origin.y = 39;
     view.frame = frame;
     NSLog(@"View is hidden.");

     [arrCards removeObjectAtIndex:indexPath.row];
     [collectionView_ performBatchUpdates:^{
         [collectionView_ deleteItemsAtIndexPaths:@[indexPath]];
     } completion:^(BOOL finished) {
         // you might want to remove the data from the data source here so the view doesn't come back to life when the collection view is reloaded.
     }];
 }];
}
.

とCollectionViewのPagingEnabledをNOに保存してから、行ってもいいはずです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top