0

been working on an app and have been studying the questions available here but no luck in finding something similar to resolve my problem.

I have an array setup with 4 images and I'm trying to change to a different image every time the image is touched in sequence and then back to first after last image displayed.

For example: image1 "touch" => image2 "touch" => image3 "touch" image4 "touch" => image1.....

The image won't change unless "touched".

My array is setup as:

    -(void) viewDidLoad {
        imageArray = [[NSArray alloc] initWithObjects:
                      [UIImage imageNamed:@"image1.png"],
                      [UIImage imageNamed:@"image2.png"],
                      [UIImage imageNamed:@"image3.png"],
                      [UIImage imageNamed:@"image4.png"],
                      nil];

I then display the first image with the following:

    imageView.image = [imageArray objectAtIndex:0];

Then, I'm able to successfully change between two images with the following code:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       UITouch *touch = [[event allTouches] anyObject];
       NSLog(@"'Touches Began");

       if ([touch view] == imageView) {
           imageView.image = [imageArray objectAtIndex:1];
           NSLog(@"Image touched and changed");
       }

Now, because I want imageView display more than two changes, I tried to use if statement inside a for loop with no luck:

    int touchCount = 0;
    for (touchCount = 1; touchCount < 4; touchCount++) {
        if ((touchCount < 4) && ([touch view] == imageView)) {
            imageView.image = [imageArray objectAtIndex:touchCount];
            NSLog(@"Touch Count = %d", touchCount);
        }
    }

When ran, the debugger shows "Touch Count = 0" and then just crash and exits back into the home screen. Can anybody please help on this problem? Is my concept wrong?

2
  • What is the log saying when it crashes? Commented Dec 11, 2011 at 7:18
  • Your for loop, if it were not crashing, will simply show each of the images in rapid succession. That you will rotate through a small number of images isn't a call for a looping construct. This is simply maintaining what image is going to come up next. You could have a random number generator picking it, or some other algorithm. What you code needs to do is decide what image ought to be shown next, and hand that image over. JRTurton gives a good answer for the rotating algorithm. Commented Dec 11, 2011 at 9:23

1 Answer 1

1

Have an integer as an instance variable which stores which image is being displayed. Set this to 0 on viewDidLoad.

When a touch is registered on the image view, increment the integer. If the value equals the count of your image array, set it back to 0.

Set the image to the element of your image array indicated by the integer value:

// touch registered on image
currentImage++;
if (currentImage == [imageArray count])
    currentImage = 0;

imageView.image = [imageArray objectAtIndex:currentImage];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks everybody for your help! It works great now. I was in the impression I needed some kind of loop in order to facilitate a continuation, forgetting that xcode itself is already running in a loop fashion until a trigger. Also, I want to point out, at first I initialize touchCount by using "NSInteger touchCount" and it keep on crashing. I've treated as a "pointer" instead of a regular integer. Taking out the "" solved everything as it is now a regular integer. What a noob I am!! Still got a lot of work ahead of me.
I meant to put "NSInteger <star>touchCount" in the comment above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.