【问题标题】:Why does NSArray of UIImageViews follow a certain pattern?为什么 UIImageViews 的 NSArray 遵循一定的模式?
【发布时间】:2015-05-21 08:56:49
【问题描述】:

我有一个由 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,因此只有那一小部分会将图像更改为数字。

【问题讨论】:

    标签: ios objective-c arrays uiimageview


    【解决方案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];
            }
    }
    

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

    int intersectionCount = 0;
    

    并像上面一样更改touchesEnded

    【讨论】:

    • 这解决了奇怪的模式问题,但现在他们不像我需要的那样行事。如果我先拖动 A4,那么图像将变为 4 号而不是 1 号。有没有办法做到这一点,所以它们被拖动的顺序无关紧要?
    • @Laury,检查我编辑的答案并再次替换 touchEnded 方法。这能解决你的问题吗?
    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2014-05-24
    相关资源
    最近更新 更多