TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
It's currently not possible to use Cocoa's drag-and-drop classes and protocols in iPhone/iPad apps. This really sucks. Implementing a UIPanGestureRecognizer is really fucking boring.
SEDraggable is a subclass of UIView that fills this void, making it easy to add drag-and-drop functionality to any UIView in your application.
Add the source files to your Xcode project. Import the SEDraggable.h
header.
#import "SEDraggable.h"
To initialize an instance of SEDraggable, you'll probably want to use one of these:
- (id) initWithFrame:(CGRect)frame;
- (id) initWithImage:(UIImage *)image andSize:(CGSize)size;
- (id) initWithImageView:(UIImageView *)imageView;
// set up the UIImageView that will be used as the visible component of our draggable element
UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"someFile.png"];
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
// initialize the draggable element itself
SEDraggable *draggableView = [SEDraggable initWithImageView:imageView];
Of course, as mentioned, there are simpler init methods to suit different needs.
CGRect frame = CGRectMake(10, 10, 100, 100);
SEDraggable *draggableView = [SEDraggable initWithFrame:frame];
// ... or ...
SEDraggable *draggableView = [SEDraggable init];
SEDraggableLocation allows you to easily switch between certain automatic behaviors -- for example, visually arranging a set of SEDraggable objects so that they don't appear to be haphazardly strewn about, or yanking an SEDraggable back to its original location when a drag-and-drop operation is unsuccessful for some reason.
Try something like the following. Run the code and then play around with the draggable objects to see how they behave.
SEDraggable *draggableView = ...
// create the SEDraggableLocation to represent the draggable element's starting point
CGRect homeLocationBounds = CGRectMake(10, 10, 100, 100);
CGRect otherLocationBounds = CGRectMake(200, 200, 100, 100);
SEDraggableLocation *homeLocation = [SEDraggableLocation initWithBounds:homeLocationBounds];
SEDraggableLocation *otherLocation = [SEDraggableLocation initWithBounds:otherLocationBounds];
// configure a whole litany of options
otherLocation.objectWidth = draggableView.frame.size.width;
otherLocation.objectHeight = draggableView.frame.size.height;
otherLocation.marginLeft = 3;
otherLocation.marginRight = 3;
otherLocation.marginTop = 3;
otherLocation.marginBottom = 3;
otherLocation.marginBetweenX = 3;
otherLocation.marginBetweenY = 3;
otherLocation.shouldAcceptDroppedObjects = YES;
otherLocation.shouldAutomaticallyRecalculateObjectPositions = YES;
otherLocation.shouldAnimateObjectAdjustments = YES;
otherLocation.animationDuration = 0.5f;
otherLocation.animationDelay = 0.0f;
otherLocation.animationOptions = UIViewAnimationOptionBeginFromCurrentState;
otherLocation.fillHorizontallyFirst = YES; // NO makes it fill rows first
otherLocation.allowRows = YES;
otherLocation.allowColumns = YES;
// add the draggable object, or maybe even several
draggableView.homeLocation = homeLocation;
[draggableView addAllowedDropLocation:homeLocation];
[draggableView addAllowedDropLocation:otherLocation];
[homeLocation addDraggableObject:draggableView];
You can even specify different boundaries for where an SEDraggableLocation will accept dropped objects and where it will place them. And these two regions don't even have to be contiguous in any way! It's kind of cool to watch what happens if they're not -- when you drop your draggable objects, they fly from one area of the screen to another automatically.
These are the two properties you'll want to look at if you want to configure this kind of behavior:
@property (nonatomic, readwrite) CGRect responsiveBounds;
@property (nonatomic, readwrite) CGRect objectGutterBounds;
You can specify a delegate that will be notified of pertinent drag-and-drop events. Delegates of SEDraggable and SEDraggableLocation objects must conform either to the SEDraggableEventResponder protocol or the SEDraggableLocationEventResponder protocol, respectively. The two protocols define the following messages, all of which are optional:
- (void) draggableObjectDidMove:(SEDraggable *)object;
- (void) draggableObjectDidStopMoving:(SEDraggable *)object;
- (void) draggableObject:(SEDraggable *)object didMoveWithinLocation:(SEDraggableLocation *)location;
- (void) draggableObject:(SEDraggable *)object didStopMovingWithinLocation:(SEDraggableLocation *)location;
- (void) draggableObjectWillSnapBackToHomeFrame:(SEDraggable *)object;
- (void) draggableObjectDidEndSnappingBackToHomeFrame:(SEDraggable *)object;
- (void) draggableObject:(SEDraggable *)object didBeginSnapAnimationWithID:(NSString *)animationID andContext:(void *)context;
- (void) draggableObject:(SEDraggable *)object didEndSnapAnimationWithID:(NSString *)animationID andContext:(void *)context;
- (void) draggableLocation:(SEDraggableLocation *)location didAcceptDroppedObject:(SEDraggable *)object;
- (void) draggableLocation:(SEDraggableLocation *)location didRefuseDroppedObject:(SEDraggable *)object;
- (void) draggableObject:(SEDraggable *)object wasRemovedFromLocation:(SEDraggableLocation *)location;
bryn austin bellomy <[email protected]>
Copyright (c) 2012 bryn austin bellomy, http://signals.io/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.