iOS 拖拽工具包,支持
当前库的限制是拖拽操作发生在与常见根视图关联的对象上。在实际上,这仅仅限制了跨越 UIWindows 的拖拽。此库目前没有使用 ARC。我知道。我知道。但我的动机是一个更大的项目,该项目尚未转换。
我写了这个库,因为我需要拖拽支持,但没有找到满足所有需求的东西。请试用并给我反馈。我对使其更健壮很感兴趣,并将接受合理的拉取请求。如果您想进行重大更改,我可以开放,但让我们谈谈。
源代码库提供了一些使用大多数功能的示例。以下是一个使用大多数默认设置的简单示例。
这里有一个 UIView 拖拽源。
@interface AtkSampleOneDragSourceView<AtkDragSourceProtocol>
@end
@implementation AtkSampleOneDragSourceView
- (BOOL)shouldDragStart:(AtkDragAndDropManager *)manager
{
return YES;
}
- (void)dragWillStart:(AtkDragAndDropManager *)manager
{
// This is called before any call to AtkDropZoneProtocol shouldDragStart.
// It's a good place to setup data for that method to examine.
manager.pasteboard.string = [NSString stringWithFormat:@"val-%ld", (long)self.tag];
}
@end
这里有一个 UIView 拖拽区域。
@interface AtkSampleOneDropZoneView<AtkDropZoneProtocol>
@end
@implementation AtkSampleOneDropZoneView
- (BOOL)shouldDragStart:(AtkDragAndDropManager *)manager
{
// Yes, consider me for drags. Returning true here only
// ensures that isInterested, dragStarted, and dragEnded will
// be called.
return YES;
}
- (BOOL)isInterested:(AtkDragAndDropManager *)manager
{
// If we return true here then dragEntered, dragExited, dragMoved and
// dragDropped can be called.
// So, let's see if we are interested in what's on the pasteboard.
// For the example this is if the pastbaord string matches
// a string made up from the views tag property.
BOOL ret = NO;
UIPasteboard *pastebaord = manager.pasteboard;
NSString *interestedInString =
[NSString stringWithFormat:@"val-%ld", (long)self.tag];
if([interestedInString isEqualToString:pastebaord.string])
ret = YES;
return ret;
}
@end
最后,我们有我们的 UIViewController。这假设拖拽源和拖拽区域已在 Interface Builder 或其他方式中布置在 MySampleOneViewController 上。
@interface AtkSampleOneViewController : UIViewController<AtkDragAndDropManagerDelegate>
@property (nonatomic, retain) AtkDragAndDropManager *dragAndDropManager;
@end
@implementation AtkSampleOneViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
self.navigationItem.title = @"Sample One";
//
// By default the AtkDragAndDropManager uses the UIApplication key windows as
// the rootView and a UIPanGestureRecognizer. However, these are configurable.
// Notice there is no need to register the drag sources or drop zones. The
// AtkDragAndDropManager will by default traverse the view hierarch and find them.
// This behavior is also configurable through the AtkDragAndDropManager delegate.
//
self.dragAndDropManager = [[[AtkDragAndDropManager alloc] init] autorelease];
// For the AtkDragAndDropManagerDelegate methods findDragSource: finrDropZones: and
// isDropZoneActive:recognizer:, if we do not implement them here, the
// relevant methods in AtkDefaultDragAndDropManagerDelegate will be called.
// This gives us our reasonable defaults even if we want to capture drag and drop
// events here.
self.dragAndDropManager.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.dragAndDropManager start];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.dragAndDropManager stop];
[super viewWillDisappear:animated];
}
/**
* Called when a drag is dropped onto a drop zone.
*/
- (void)dragDropped:(AtkDragAndDropManager *)manager
dropZone:(id<AtkDropZoneProtocol>)dropZone
point:(CGPoint)point
{
// The drag was dropped onto an interested AtkDropZoneProtocol. Do something with it.
}
@end
Rick Boykin, [email protected]
AtkDragAndDrop 在MIT许可下可用。更多详细信息请参阅LICENSE文件。