0

I need to access a random element from IBOutletCollection array. I have 4 letter imageViews that needs to be dragged on to another imageView and change the image depending on how many were dragged. Im testing with 2 at the moment. I have this but it doesn't seem to take random but a particular one of the 4.

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

    for (UIImageView *letter in letterA)
    {
        for (UIImageView *letter1 in letterA)
        {
            if (CGRectIntersectsRect(letter.frame ,answerA.frame))
            {
                UIImage *Pic1 = [UIImage imageNamed:@"number1"];
                [correctCounterA setImage:Pic1];
                letter.userInteractionEnabled=NO;
                letter.hidden=YES;

            }
            else if (CGRectIntersectsRect(letter1.frame, answerA.frame)) //CGRectIntersectsRect(letter.frame, answerA.frame))
            {
                UIImage *Pic2 = [UIImage imageNamed:@"number2"];
                [correctCounterA setImage:Pic2];
            }
        }

    }
}
Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
Laury
  • 53
  • 11
  • I see no randomness in that code?!? – trojanfoe May 14 '15 at 09:47
  • I know thats why im asking how? I tried arc4Random but not sure how to use it correctly. 3 out of 4 letters i have changes the image to number2 and the only one goes changes to number1, I need it so if i drag any of them first it will change to number1 and that letter disappears, afterwards if i drag another then it will change to number2 and so on. – Laury May 14 '15 at 09:50
  • It's not really clear what you want to do; however the result from the first `CGRectIntersectsRect()` call won't change in every iteration of the inner loop, so it looks like it needs to be in the outer loop. I don't think there is much more I can offer. – trojanfoe May 14 '15 at 09:56
  • I moved the first CGRctIntersectsRect() to outer loop and the second to inner loop but now all goes straight to number2, seems like it ignores the first loop and goes straight to second loop – Laury May 14 '15 at 10:07
  • Sorry I don't understand what you are trying to do. – trojanfoe May 14 '15 at 10:09
  • I have 4 imageViews to be dragged and 1 to be dragged on. I placed the 4 in IBOutletCollection. I need to drag and drop the 4 on the other imageView. Every time i drag one of the 4 it changes the image of the last ones to a number. If i drag one then it changes to an image of 1. Basically the last imageView is like a counter that counts how many i dragged on it. Does this explain it better? – Laury May 14 '15 at 10:13
  • Yes I think I get it. OK so you are testing if the frames of the image views intersect. How does that work? What is moving the image views? – trojanfoe May 14 '15 at 10:17
  • the 4 draggable imageViews are moved by mouse. The counter imageView is stationary and when i drag one of the 4 and they intersect it will disappear and the counters image changes to number 1. If i would to drag another one then it would change to number 2 and so on. But i want it so that it would not matter which of the 4 imageView are dragged first or second – Laury May 14 '15 at 10:21
  • Mouse? I'm guessing you are talking about the mouse being used in the iOS Simulator to simulate a touch? Regardless, it would be useful to see how that works, and it's not clear to me. It seems like you are using a form of *drag and drop* that we are used to under OSX, so there might already be a solution you could use already out there. – trojanfoe May 14 '15 at 10:23
  • Yes in simulator. All i know is im using xcode 6. My first IOS app that im trying to develop, started about a month ago. I have searched a lot for this solution and couldn't find it. – Laury May 14 '15 at 10:29

0 Answers0