Giter VIP home page Giter VIP logo

obdragdrop's Introduction

OBDragDrop

OBDragDrop is a compact iOS UI library for drag and drop. It is self-contained and depends only on UIKit.

Features:

  • Drag and drop between any UIViews
  • Any view can be draggable, and any view can be a drop target
  • Visuals exists on a UIWindow that sits on top of the app
  • Various events for different stages of a drag and drop gesture

Installation

Simply drag the files into your project and go! No need to mess with library paths, target dependencies, etc

Implementation

Dragging is initiated when a custom UIGestureRecognizer is attached to a source view, typically this is a UILongPressGestureRecognizer but other recognizer types could be made to work in the future. This recognizer should be created from OBDragDropManager which serves as a factory object

The view that is dragged then asks its OBOvumSource, typically the UIViewController, for information on the data object that should be attached to the drag and drop.

The OBOvum object encapsulates the drag and drop gesture, calling delegate methods on the drop zone for events such as enter, move, drop, and exit.

For each view that an OBOvum can be dropped in simply attach a drop zone handler, typically also the UIViewController, and implementing the necessary delegate methods.

Code Snippets

Set Up

The one set up call necessary to initialize the drag drop manager is to associate it with the main window. Typically call this when the application loads inside your app delegate or context

	OBDragDropManager *manager = [OBDragDropManager sharedManager];
	[manager prepareOverlayWindowUsingMainWindow:self.window];

Instantiating a OBDragDropRecognizer

Use OBDragDropManager as a factory for the drag drop specific gesture recognizers and attach it to your view. You can now use any gesture recognizer of your choosing, but it should be a continuous tracking gesture rather than discrete actions.

	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];

Using OBDragDropManager

-(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;
}

Drop Zone handling

-(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
}

Example

There's a simple example project called OBDragDropTest which demonstrates the basic functionality of the library.

License

This library is released under an MIT license.

obdragdrop's People

Contributors

brynbodayle avatar cjheng avatar igorsales avatar ivanbellalopez avatar miguelsanchez avatar rayh avatar zaichang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.