开发者

Implement drag and drop in an iPad split view app?

I'm quite a n00b at iPhone and iPad development, so any advice would be gratefully received.

I want to create a split-view iPad app, where users can drag an image out of the root view controller and place it in the detail view.

Once that's been done, 开发者_运维问答ideally the user would be able to manipulate the image by moving it about and resizing it.

Any pointers to the best way to achieve this? Many thanks!


Well first off, if your new I suggest getting a book. Mine is Beginning iPhone 4 Development
by Dave Mark, Jack Nutting, and Jeff LaMarche (Apress). It has saved me from wasting a lot of time and to get started. Also, there may be better books suited to what you need but this one has a pretty good chapter on Taps, Touches and Gestures... called 15: Taps, Touches and Gestures.

anyways heres the gist:

const UIImageView * viewBeingDragged = nil;

// in your rootController 
// (lets say a UIViewController with one UIImageView)
//
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

    //our image views are direct decendents of our main view
    //the following method returns the farthest decendent from the target 
    //in the point, so make sure you dont have any intersecting subviews!
    //pretty sure you dont need event
    //
    UIView *v = [self.view hitTest:touchPoint event:nil];
    NSAssert([v isMemberOfClass:[UIImageView class]], @"Not an image view?!?!?"]);

    viewBeingDragged = (UIImageView *)v;

    [v removeFromSuperview]; 
    // or just gray it out...
    [self.view.superview.superview addSubview:v]; 
    //should be your splitViewController, if not get from AppDelegate
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    viewBeingDragged.center = 
        [[touches anyObject] locationInView:SplitControllerView];

    if (viewBeingDragged.frame intersects with detailViewController.view.frame)
    {
        //visual cue that landing zone is set
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // last check if view still intersects
    // if so move underlieing data from left to right
    // redraw left view
    // remove viewBeingDragged from superview
    // add viewBeingDragged to 
    viewBeingDragged = nil;
}

you may also want to implement touchesCanceled
I havent tested this method so unexpected things may come up.
Also, UIView has the same touch methods, so if you want to create your own custom views / view controller to make it more... "extensible".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜