为什么UIImageView的NSArray遵循某种模式?

时间:2015-05-21 08:56:49

标签: ios objective-c arrays uiimageview

我有一个4 UIImageViews A,A2,A3和A4的数组。我希望将它们拖到另一个名为answerA的imageView上。每次我将其中一个拖到答案上时,它应该将answerA上的图像更改为一个数字,该数字表示拖动了多少。例如。如果我在answerA上拖动A然后answerA图像将更改为数字1.如果我从数组中再拖一个A然后它将更改为数字2.由于某种原因,我的数组遵循一个奇怪的模式。第一个我拖动更改为数字1,第二个没有做任何事情,第三个将更改为数字2,第四个将更改为数字1.无论我拖动它们的顺序,它将始终是相同的模式。

这是我的4 imageViews

数组
@property (strong, nonatomic) NSArray *letterA; //initialized in .h

self.letterA =  @[A, A2, A3, A4];//initialized in .m

检查碰撞的方法:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    int intersectionCount = 0;
    for (UIImageView *letter in letterA) {
        if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
            letter.userInteractionEnabled = NO;
            letter.hidden = YES;
            intersectionCount++;
        }
    }

    if (intersectionCount == 1) {
        UIImage *Pic1 = [UIImage imageNamed:@"number1.png"];
        [correctCounterA setImage:Pic1];
    }
    else if (intersectionCount == 2) {
        UIImage *Pic2 = [UIImage imageNamed:@"number2.png"];
        [correctCounterA setImage:Pic2];
    }
    else if (intersectionCount == 3) {
        UIImage *Pic3 = [UIImage imageNamed:@"number3.png"];
        [correctCounterA setImage:Pic3];
    }
    else if (intersectionCount == 4) {
        UIImage *Pic4 = [UIImage imageNamed:@"number4.png"];
        [correctCounterA setImage:Pic4];
    }

注意:correctCounterA在answerA之上是一个小imageView,因此只有那个小部分会将图像更改为数字。

1 个答案:

答案 0 :(得分:1)

您需要修改touchesEnded,如下所示。在这里你以错误的方式找到拖动图像。这可能对你有所帮助。

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

        for (UIImageView *letter in letterA) {

            if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
                letter.userInteractionEnabled = NO;
                letter.hidden = YES;
                intersectionCount++;
                break;
            }

        }

        if (intersectionCount == 1) {
            UIImage *Pic1 = [UIImage imageNamed:@"number1.png"];
            [correctCounterA setImage:Pic1];
        }
        else if (intersectionCount == 2) {
            UIImage *Pic2 = [UIImage imageNamed:@"number2.png"];
            [correctCounterA setImage:Pic2];
        }
        else if (intersectionCount == 3) {
            UIImage *Pic3 = [UIImage imageNamed:@"number3.png"];
            [correctCounterA setImage:Pic3];
        }
        else if (intersectionCount == 4) {
            UIImage *Pic4 = [UIImage imageNamed:@"number4.png"];
            [correctCounterA setImage:Pic4];
        }
}

修改:根据您的要求,您需要在intersectionCount文件和.h中声明viewDidLoad,您需要分配如下。

int intersectionCount = 0;

并像上面一样更改touchesEnded