不可见集合视图单元格的拖放问题

时间:2018-09-20 14:00:49

标签: objective-c uicollectionview drag-and-drop scrollview

更新的说明

Please find image here.

在这里我使用了两个集合视图。一个用于问题,第二个用于选项,我想通过拖放来使用选项来填充空白。

但是当我拖动一个选项来填充并且当它超出集合视图的范围时,就遇到了关于UI的问题,就像下面的图像链接一样显示问题。

我试图删除不可见的单元格,但是它不起作用。

我知道关于scroll和clipToBound存在问题,但是当我给clipToBound = true时,超出集合视图后就看不到我的拖动选项,第二件事是当我给clipToBound = no时出现显示问题链接

下的图片

所以现在我想知道我将按照完全填充的要求执行什么操作,并且服务器提供了动态的其他选项。 请事先给我答复,谢谢

 - (void)labelDragged:(UIPanGestureRecognizer *)gesture
    {

        //for clip to bound
        collectionViewAnswers.clipsToBounds = true;



        UILabel *label = (UILabel *)gesture.view;
        CGRect labelframe = [label convertRect:label.bounds toView:self.view];


        if (labelframe.origin.y <= 218.000000) {
            collectionViewAnswers.clipsToBounds = NO;
            [self removeNaughtyLingeringCells];
        }

        if (gesture.state == UIGestureRecognizerStateEnded) {

            NSArray *ary = collectionViewQuestion.visibleCells;

            for (DragDropCollectionViewCell *cell in ary)
            {
                CGRect cellFrame = [cell convertRect:cell.bounds toView:self.view];

                if (CGRectIntersectsRect(labelframe, cellFrame))
                {

                    // NSLog(@"%ld",(long)cell.tag);

                    NSIndexPath *indexPath = [collectionViewQuestion indexPathForCell:cell];

                    NSMutableDictionary *dict = [arrQueAnsData objectAtIndex:APP_DELEGATE.nextPreviousCount];
                    NSMutableArray *arrinnner =  [[dict valueForKey:@"questionToShow"] mutableCopy];

                    NSMutableArray *arrSections = [[[dict valueForKey:@"questionToShow"]objectAtIndex:indexPath.section] mutableCopy];

                    NSMutableDictionary *dictInner = [arrSections objectAtIndex:indexPath.row];

                    if ([[dictInner valueForKey:@"questionTest"] isEqualToString:@"blank"] || [[dict valueForKey:@"questionTest"] isEqualToString:@"blankdot"])
                    {
                        NSInteger previousState = 0;
                        BOOL isvalid = FALSE;
                        if ([[dictInner valueForKey:@"selectedText"] length] !=0)
                        {
                            isvalid = TRUE;
                            previousState = [[dictInner valueForKey:@"isSelected"] integerValue];
                           // NSLog(@"%ld",(long)previousState);
                        }

                        [dictInner setObject:[NSString stringWithFormat:@"%ld",(long)label.tag] forKey:@"isSelected"];
                        [dictInner setObject:label.text forKey:@"selectedText"];

                        if ([[dict valueForKey:@"questionTest"] isEqualToString:@"blankdot"])
                        {
                            [dictInner setObject:[NSString stringWithFormat:@"%@.",label.text] forKey:@"selectedText"];
                        }

                        [arrSections replaceObjectAtIndex:indexPath.row withObject:dictInner];

                        [arrinnner replaceObjectAtIndex:indexPath.section withObject:arrSections];
                        [dict setObject:arrinnner forKey:@"questionToShow"];


                        NSMutableArray *arrinnnerAnswer =  [[dict valueForKey:@"answerToShow"] mutableCopy];

                        [arrinnnerAnswer removeObjectAtIndex:label.tag];



                        [dict setObject:arrinnnerAnswer forKey:@"answerToShow"];

                        [dict setObject:@"1" forKey:@"answer_given_or_not"];

                        [arrQueAnsData replaceObjectAtIndex:APP_DELEGATE.nextPreviousCount withObject:dict];



                        //[self setArrayObjectRandom];

                        label.hidden = YES;
                        label.userInteractionEnabled = NO;
                        break;
                    }
                }
            }

         //   NSLog(@"%@",arrQueAnsData);

            [collectionViewQuestion reloadData];
            [collectionViewAnswers reloadData];
        }
        else {

            CGPoint translation = [gesture translationInView:label];

            // move label
            label.center = CGPointMake(label.center.x + translation.x,
                                       label.center.y + translation.y);

            // reset translation
            [gesture setTranslation:CGPointZero inView:label];
        }
    }

    - (void) removeNaughtyLingeringCells {

        // 1. Find the visible cells
        NSArray *visibleCells = collectionViewAnswers.visibleCells;
        //NSLog(@"We have %i visible cells", visibleCells.count);

        // 2. Find the visible rect of the collection view on screen now
        CGRect visibleRect;
        visibleRect.origin = collectionViewAnswers.contentOffset;
        visibleRect.size = collectionViewAnswers.bounds.size;
        //NSLog(@"Rect %@", NSStringFromCGRect(visibleRect));

        int i=0;
        // 3. Find the subviews that shouldn't be there and remove them
        //NSLog(@"We have %i subviews", self.collectionView.subviews.count);
        for (UIView *aView in [collectionViewAnswers subviews]) {
            if ([aView isKindOfClass:UICollectionViewCell.class]) {
                CGPoint origin = aView.frame.origin;
                if(CGRectContainsPoint(visibleRect, origin)) {
                    if (![visibleCells containsObject:aView]) {
                        [aView removeFromSuperview];
                        aView.backgroundColor = [UIColor clearColor];

                        i++;
                        NSLog(@"-----<>%d",i);
                    }
                }
            }
        }
        //NSLog(@"%i views shouldn't be there", viewsShouldntBeThere.count);

        // 4. Refresh the collection view display
        [collectionViewAnswers setNeedsDisplay];
    }

    - (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        if (!decelerate) {
            [self removeNaughtyLingeringCells];
        }
    }

    - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        [self removeNaughtyLingeringCells];
    }

0 个答案:

没有答案