Search code examples
iosiphonearraysuiswipegesturerecognizer

How can I use a button/gesture to change a value? - iOS


This may be a simple question, but I have searched for hours trying to figure it out.

Initially I was trying to create a view that allowed me to change the background image by swiping left and right. After not being able to find any simple way to do that, I figured I could make an array with a group of images and set the background image to a specific object in that array. I've successfully set that up but I'm having trouble figuring out how to use the gestures to change the value of the object at index. I'm imagining something along the lines of currentImage ++ or currentImage -- to change the value but I have no idea how to implement that.

If anyone knows how to do this please help. Or if anyone has a more efficient way to do this I'd also like to hear. I'm new to iOS programming.

This is what is in the viewDidLoad.

UIImage *beachImage = [UIImage imageNamed:@"Malibu_Sunset_1.jpg"];
UIImage *cityImage = [UIImage imageNamed:@"chicago_marina_city_parking_garages.jpg"];
UIImage *lodgeImage = [UIImage imageNamed:@"ski_lodge.jpg"];
UIImage *airplaneImage = [UIImage imageNamed:@"airplanes-work-1.jpg"];
UIImage *farmImage = [UIImage imageNamed:@"ideal-farm-house-photo.jpg"];

NSMutableArray *vacationImages = [NSMutableArray arrayWithObjects: airplaneImage, beachImage, cityImage, lodgeImage, farmImage, nil];

int numberOfImages = [vacationImages count];
int currentValue = 0;
UIImage *currentImage = [vacationImages objectAtIndex:currentValue];

self.mainViewImage.image = currentImage;



for (UIImage *currentImage in vacationImages) {
    UIImageView *line = [[UIImageView alloc] initWithImage:currentImage];
    line.hidden = YES;
    line.tag = currentValue;
    [self.view addSubview:line];
    numberOfImages++;
}

UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];


UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];

Solution

  • Using your own code:

    in .m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIImageView *mainViewImage;
    @property (nonatomic) int currentValue;
    @property (nonatomic, strong) NSArray *vacationImages;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // change the size of the frame of the image here
        self.mainViewImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 320, 200)];
        [self.view addSubview:self.mainViewImage];
    
        UIImage *beachImage = [UIImage imageNamed:@"Malibu_Sunset_1.jpg"];
        UIImage *cityImage = [UIImage imageNamed:@"chicago_marina_city_parking_garages.jpg"];
        UIImage *lodgeImage = [UIImage imageNamed:@"ski_lodge.jpg"];
        UIImage *airplaneImage = [UIImage imageNamed:@"airplanes-work-1.jpg"];
        UIImage *farmImage = [UIImage imageNamed:@"ideal-farm-house-photo.jpg"];
    
        self.vacationImages = [NSArray arrayWithObjects: airplaneImage, beachImage, cityImage, lodgeImage, farmImage, nil];
    
        self.currentValue = 0;
    
        UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
        self.mainViewImage.image = currentImage;
    
        UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft: )];
        [leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
        [[self view] addGestureRecognizer:leftRecognizer];
    
        UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight:)];
        [rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
        [[self view] addGestureRecognizer:rightRecognizer];
    }
    
    - (void) handleSwipeFromLeft: (id) sender
    {
        if (self.currentValue == 0)
        {
            self.currentValue = 4;
        }
        else
        {
            self.currentValue--;
        }
    
        UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
        self.mainViewImage.image = currentImage;
    }
    
    - (void) handleSwipeFromRight: (id) sender
    {
        if (self.currentValue == 4)
        {
            self.currentValue = 0;
        }
        else
        {
            self.currentValue++;
        }
    
        UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
        self.mainViewImage.image = currentImage;
    }
    
    @end