如何修改此示例代码以停止堆叠图像视图?

时间:2011-08-14 18:23:08

标签: iphone ios ipad uiimage

http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html

我是IOS开发的新手,并且一直在玩IOS开发示例代码,试图了解工作原理等等...我遇到了这个示例代码(经典版本,而不是手势识别器版本) 。)除了一件事以外,它能完成我正在寻找的一切。当图像视图被拖动到另一个图像视图上时,它们会开始堆叠,您必须双击图像才能将它们分开。

我花了很多时间试图弄清楚如何让它们不叠加。我想我必须存储一块正在被移动到ivar中,并阻止它获得其他视图的位置......但我不知道如何去做。如果有人有时间修改示例代码而不是堆叠碎片,我真的很感激!在我花费的时间里,我很想找到解决方案!

我会尝试对我在这里尝试做的事情有点具体......我已经制作了26个这样的图像视图,使它们成为字母表中的一个字母。我有两行13个图像视图。 A-M& N-Z-。目前,如果我从顶行向下拖动A瓦片,将其向下移动到“拼写区域”,我必须越过N-Z行。当我这样做时,“A”图块(图像视图)会拾取它悬停在其上的任何图块。因此,如果我将A直接向下拖动,它将会拾取N瓦片,因为它将在其上方悬停,然后才能进入“拼写”区域。

预先感谢您提供任何帮助或指导!

1 个答案:

答案 0 :(得分:0)

你在示例代码中有这个功能

// Checks to see which view, or views,  the point is in and then calls a method to perform the closing animation,
// which is to return the piece to its original size, as if it is being put down by the user.
-(void)dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{   
    // Check to see which view, or views,  the point is in and then animate to that position.
    if (CGRectContainsPoint([firstPieceView frame], position)) {
        [self animateView:firstPieceView toPosition: position];
    } 
    if (CGRectContainsPoint([secondPieceView frame], position)) {
        [self animateView:secondPieceView toPosition: position];
    } 
    if (CGRectContainsPoint([thirdPieceView frame], position)) {
        [self animateView:thirdPieceView toPosition: position];
    }
    // If one piece obscures another, display a message so the user can move the pieces apart
    if (CGPointEqualToPoint(firstPieceView.center, secondPieceView.center) ||
        CGPointEqualToPoint(firstPieceView.center, thirdPieceView.center) ||
        CGPointEqualToPoint(secondPieceView.center, thirdPieceView.center)) {
        touchInstructionsText.text = @"Double tap the background to move the pieces apart.";
        piecesOnTop = YES;
    } else {
        piecesOnTop = NO;
    }
}

你可以尝试修改为

// Checks to see which view, or views,  the point is in and then calls a method to perform the closing animation,
// which is to return the piece to its original size, as if it is being put down by the user.
-(void)dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{   
    // Check to see which view, or views,  the point is in and then animate to that position.
    if (CGRectContainsPoint([firstPieceView frame], position)) {
        [self animateView:firstPieceView toPosition: position];
    } 
    if (CGRectContainsPoint([secondPieceView frame], position)) {
        [self animateView:secondPieceView toPosition: position];
    } 
    if (CGRectContainsPoint([thirdPieceView frame], position)) {
        [self animateView:thirdPieceView toPosition: position];
    }
    // If one piece obscures another, display a message so the user can move the pieces apart
    if (CGPointEqualToPoint(firstPieceView.center, secondPieceView.center) ||
        CGPointEqualToPoint(firstPieceView.center, thirdPieceView.center) ||
        CGPointEqualToPoint(secondPieceView.center, thirdPieceView.center)) {

        if (firstPieceView.center.x == secondPieceView.center.x)
            secondPieceView.center = CGPointMake(firstPieceView.center.x - 50, firstPieceView.center.y - 50);       
        if (firstPieceView.center.x == thirdPieceView.center.x)
            thirdPieceView.center  = CGPointMake(firstPieceView.center.x + 50, firstPieceView.center.y + 50);   
        if (secondPieceView.center.x == thirdPieceView.center.x)
            thirdPieceView.center  = CGPointMake(secondPieceView.center.x + 50, secondPieceView.center.y + 50);
        touchInstructionsText.text = @"";
    } else {
        piecesOnTop = NO;
    }
}

让我知道它是否符合您的要求......