1

私のアプリには、2 つの UITableView が並んだ UIView があります。これらの上にレイヤー化されているのは UIView です (すべてストーリーボードで行われます)。

ここに画像の説明を入力

上部の UIView である DnD Overlay View にアタッチすることで、テーブル間でセルをドラッグ アンド ドロップするように設定されています (いくつかのオープン ソース コードhttps://github.com/josh2112/iPad-UITableView-DnD-Demoを使用)。テーブルにスクロールを保証するのに十分なセルがあるまで、これはすべて正常に機能します-セルに触れるたびにドラッグするためにポップアウトするため、スクロールできません。

そこで、瞬時ではなく長押しで飛び出す機能を作ってみたいと思います。ここに私が持っているものがあります:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog( @"touchesBegan" );
    UITouch* touch = [touches anyObject];
    longPress = NO;

    [self performSelector:@selector(longPress:) withObject:touch afterDelay:2.0];
}

- (void)longPress:(UITouch *)touch{
BOOL inDragSource = NO;
longPress = YES;
// Was the touch over a drag source?
for( UIView<DragSource>* dragSource in dragSources ) {
    CGPoint point = [touch locationInView:dragSource];
    if( CGRectContainsPoint( dragSource.bounds, point )) {
        NSLog( @"Detected touch inside drag source!!!" );
        inDragSource = YES;

        // Ask the drag source for a draggable
        UIView* draggable = [dragSource popItemForDragFrom:point];
        if( draggable != nil ) {
            NSLog( @"Start drag on thing: %@", draggable );
            dragOperation = [[DragOperation alloc] initWithDraggable:draggable dragSource:dragSource initialPoint:draggable.center];

            // Unattach the draggable from its parent view and attach it to the overlay view, using the touch
            // location as the center point.
            [self addSubview:draggable];
            draggable.center = [self convertPoint:draggable.center fromView:dragSource];
            draggable.layer.masksToBounds = NO;
            draggable.layer.cornerRadius = 8;
            draggable.layer.shadowOffset = CGSizeMake( 7, 7 );
            draggable.layer.shadowRadius = 5;
            draggable.layer.shadowOpacity = 0.5;

            [UIView animateWithDuration:0.1f animations:^{
                draggable.center = [touch locationInView:self];
                draggable.transform = CGAffineTransformMakeScale( 1.2f, 1.2f );
            }];

            // If the drag source is also a drop target, tell it the draggable is hovering over it.
            if( [dragSource conformsToProtocol:@protocol(DropTarget)] ) {
                [self notifyDraggableEntered:(UIView<DropTarget>*)dragSource atPoint:point];
            }

            return;
        }
    }
}

}  


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if( dragOperation == nil ) return;

UITouch* touch = [touches anyObject];

if(!longPress){
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(longPress:) object:touch];
}
else{
    [self setHighlight:NO onDropTarget:dragOperation.currentDropTarget];

    // If we have a current drop target, drop the draggable there
    if( dragOperation.currentDropTarget != nil ) {
        NSLog( @"Transferring draggable to drop target!" );
        CGPoint point = [touch locationInView:dragOperation.currentDropTarget];

        CGPoint adjustedPoint = [dragOperation.currentDropTarget actualDropPointForLocation:point];
        [self animateDropOn:dragOperation.currentDropTarget atPoint:adjustedPoint withDuration:0.1f];
    }
    else {
        // If the original drag source is also a drop target, put the draggable back in its
        // original spot.
        if( [dragOperation.dragSource conformsToProtocol:@protocol(DropTarget)] ) {
            NSLog( @"Transferring draggable back to source" );
            [self animateDropOn:(UIView<DropTarget>*)dragOperation.dragSource atPoint:dragOperation.initialPoint withDuration:0.3f];
        }
        else {
            // Otherwise, just kill it?
            NSLog( @"Killing draggable, nobody wants it :-(" );
            [dragOperation.draggable removeFromSuperview];
            dragOperation = 0;
        }
    }
}
}

これで、どこかをタッチすると touchesBegan が起動され、次に longPress が起動されます。touchesEnded は、前の実行要求をキャンセルするためにヒットしません。touchesCancelled もヒットしません。

ここで私が間違っていることは明らかですか?

4

1 に答える 1