在touchesBegan中使用bringSubviewToFront,在图像帧中使用touchesEnded

时间:2015-03-23 18:52:59

标签: ios objective-c

我很难尝试从imageView创建一个框架,然后将它带到前面。它适用于我在viewDidLoad中使用它的最后一个图像,但我需要将它带入我的touchesBegan方法。

当我在touchesEnded方法中使用它时,它可以正常工作,但只是第二次按下按钮,因为这只是在释放按钮时执行。所以我不明白为什么它不应该只在touchesBegan方法中工作。它不仅仅是工作,而是让图像消失!

继承我的代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == _imageOne){
        CGRect newFrame = _imageOne.frame;
        newFrame.size.width += 280;
        newFrame.size.height += 280;
        newFrame.origin.x -= 3.2;
        newFrame.origin.y += 100;
        _imageOne.frame = newFrame;
        [_imageOne.superview bringSubviewToFront:_imageOne];
    }
    else if ([touch view] == _imageTwo)
    {
        CGRect newFrame = _imageTwo.frame;
        newFrame.size.width += 280;
        newFrame.size.height += 280;
        newFrame.origin.x -= 95;
        newFrame.origin.y += 100;
        _imageTwo.frame = newFrame;
    }
    else if ([touch view] == _imageThree)
    {
        CGRect newFrame = _imageThree.frame;
        newFrame.size.width += 280;
        newFrame.size.height += 280;
        newFrame.origin.x -= 185;
        newFrame.origin.y += 100;
        _imageThree.frame = newFrame;
    }
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == _imageOne)
    {
        CGRect newFrame = _imageOne.frame;
        newFrame.size.width -= 280;
        newFrame.size.height -= 280;
        newFrame.origin.x += 3.2;
        newFrame.origin.y -= 100;
        _imageOne.frame = newFrame;
    }
    else if ([touch view] == _imageTwo)
    {
        CGRect newFrame = _imageTwo.frame;
        newFrame.size.width -= 280;
        newFrame.size.height -= 280;
        newFrame.origin.x += 95;
        newFrame.origin.y -= 100;
        _imageTwo.frame = newFrame;
    }
    else if ([touch view] == _imageThree)
    {
        CGRect newFrame = _imageThree.frame;
        newFrame.size.width -= 280;
        newFrame.size.height -= 280;
        newFrame.origin.x += 185;
        newFrame.origin.y -= 100;
        _imageThree.frame = newFrame;
    }
}

任何帮助都非常感谢。

谢谢!

1 个答案:

答案 0 :(得分:0)

我通常会略微改变触摸代码,你可以尝试一下。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in [event allTouches]) {

        for (UIImageView *image in _images) {
            point = [touch locationInView:image];
            inside = [image pointInside:point withEvent:event];

            if (inside) {
                // your activation code
            }
        }
    }
}