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?
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: