OBDragDrop 是一个紧凑的 iOS UI 库,用于拖放。它是自包含的,仅依赖于 UIKit。
特性
只需将文件拖到项目中即可!无需处理库路径、目标依赖项等。
拖动操作是通过在目标视图上附加自定义 UIGestureRecognizer 来启动的,通常是 UILongPressGestureRecognizer,但将来也可以使用其他识别器类型。此识别器应从 OBDragDropManager 中创建,该管理器充当工厂对象。
拖动的视图会向其 OBOvumSource(通常是 UIViewController)请求关于应附加到拖放的数据对象的信息。
OBOvum 对象封装了拖放手势,在拖放区域调用委托方法以处理事件,如进入、移动、放下和退出。
对于 OBOvum 可以放下其中的每个视图,只需附加一个拖放区域处理程序即可,通常是 UIViewController,并实现必要的委托方法。
初始化拖放管理器的必要设置调用是将它关联到主窗口。通常在应用程序加载时在您的应用程序委托或上下文中调用此方法。
OBDragDropManager *manager = [OBDragDropManager sharedManager];
[manager prepareOverlayWindowUsingMainWindow:self.window];
使用 OBDragDropManager 作为拖放特定手势识别器的工厂,并将其附加到您的视图上。您现在可以使用任何选择的手势识别器,但它应是一个连续跟踪的手势而不是离散操作。
OBDragDropManager *dragDropManager = [OBDragDropManager sharedManager];
// Drag and drop using long press
UILongPressGestureRecognizer *dragDropRecognizer = [dragDropManager createLongPressDragDropGestureRecognizerWithSource:self];
[view addGestureRecognizer:dragDropRecognizer];
// Drag and drop using pan
UIGestureRecognizer *panRecognizer = [dragDropManager createDragDropGestureRecognizerWithClass:[UIPanGestureRecognizer class] source:self];
[view addGestureRecognizer:panRecognizer];
-(OBOvum *) createOvumFromView:(UIView*)sourceView { OBOvum *ovum = [[[OBOvum alloc] init] autorelease]; ovum.dataObject = [sourceView model]; return ovum; } -(UIView *) createDragRepresentationOfSourceView:(UIView *)sourceView inWindow:(UIWindow*)window { // Create a view that represents this source. It will be place on // the overlay window and hence the coordinates conversion to make // sure user doesn't see a jump in object location CGRect frameInWindow = [assetView convertRect:sourceView.frame toView:sourceView.window]; frameInWindow = [window convertRect:frameInWindow fromWindow:sourceView.window]; UIImageView *dragImage = [[[UIImageView alloc] initWithFrame:frameInWindow] autorelease]; dragImage.image = [(UIImageView*) sourceView image]; dragImage.contentMode = UIViewContentModeScaleAspectFit; return dragImage; }
-(void) viewDidLoad { [super viewDidLoad]; // Register view as a drop zone that will be handled by its controller self.view.dropZoneHandler = self; } -(OBDropAction) ovumEntered:(OBOvum*)ovum inView:(UIView*)view atLocation:(CGPoint)location { self.view.backgroundColor = [UIColor redColor]; return OBDropActionCopy; // Return OBDropActionNone if view is not currently accepting this ovum } -(void) ovumExited:(OBOvum*)ovum inView:(UIView*)view atLocation:(CGPoint)location { self.view.backgroundColor = [UIColor clearColor]; } -(void) ovumDropped:(OBOvum*)ovum inView:(UIView*)view atLocation:(CGPoint)location { // Handle the drop action }
这里有一个名为OBDragDropTest的简单示例项目,它展示了库的基本功能。
此库遵循MIT许可证发布。