Search code examples
iosobjective-cuiswipegesturerecognizer

Swipe Gesture issue


i have added two swipegesture on my image view .one for right and one for left. all i want is that when use click on any image and swipe it in left or right direction the image change accordingly as we do in our phone gallery. but when i swipe to left or right only one time image change after that nothing happens. all these images are coming from array and array name is getdataarray. here is my code

.h file

@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *rightgesture;

@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *leftgesture;
- (IBAction)handleswipe:(UISwipeGestureRecognizer *)sender;

.m file

-(void)viewdidload 


_rightgesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleswipe:)];

    _leftgesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleswipe:)];


- (IBAction)handleswipe:(UISwipeGestureRecognizer *)sender {

    int imageindex = (int) selectedimageindex;

    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];

    switch (direction) {

        case UISwipeGestureRecognizerDirectionLeft:
            imageindex++;
            break;
        case UISwipeGestureRecognizerDirectionRight:
            imageindex--;
            break;

        default:
            break;
    }

    if (imageindex > -1 || imageindex < getdataarray.count) {
        [fullimageview sd_setImageWithURL:[getdataarray objectAtIndex: imageindex] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    }

enter image description here

enter image description here


Solution

  • what is selectimageIndex ? I grass the issue from selectimageIndex ;

    Because you swipe the imageview once , the method 'handleswipe:' will run a once . so imageindex allways equal selectimageIndex . Please break a point to look the imageindex value .

    --

    - edit again

     -(void)viewdidload 
    {
    
    _rightgesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleswipe:)];
    
        _leftgesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleswipe:)];
    
    // this code changed. _imageindex is global property .
        _imageindex = (int) selectedimageindex;
    }
    
    - (IBAction)handleswipe:(UISwipeGestureRecognizer *)sender {
    
        UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
    
        switch (direction) {
    
            case UISwipeGestureRecognizerDirectionLeft:
                 if (_imageindex<getdataarray.count) _imageindex++; break;
            case UISwipeGestureRecognizerDirectionRight:
                 if (_imageindex>0)_imageindex--;   break;
           default: break;
       }
    
       if (_imageindex > -1 && _imageindex < getdataarray.count) {
           [fullimageview sd_setImageWithURL:[getdataarray objectAtIndex:_imageindex] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
       }
      }