在iPad拆分视图应用程序中实施拖放操作?

时间:2010-05-17 11:45:11

标签: iphone ipad

我在iPhone和iPad开发方面相当不错,所以任何建议都会感激不尽。

我想创建一个拆分视图iPad应用程序,用户可以将图像拖出根视图控制器并将其放在详细视图中。

一旦完成,理想情况下,用户可以通过移动图像并调整图像大小来操纵图像。

指向实现此目标的最佳方法的指针?非常感谢!

1 个答案:

答案 0 :(得分:1)

首先,如果你的新我建议买一本书。我的开始iPhone 4开发
Dave Mark,Jack Nutting和Jeff LaMarche(Apress)。它让我免于浪费大量时间并开始使用。此外,可能有更好的书适合你所需要的,但这个有一个很好的章节关于水龙头,触摸和手势...称为15:水龙头,触摸和手势。

无论如何,这是主旨:

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

您可能还想实施 touchesCanceled
我没有测试过这种方法,所以可能出现意想不到的事情 此外,UIView具有相同的触摸方法,因此如果您想创建自己的自定义视图/视图控制器,使其更加“可扩展”。