Swipe Gesture issue
Clash Royale CLAN TAG#URR8PPP
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"]];
2 Answers
2
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 .
--
-(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"]];
int imageindex = (int) selectedimageindex; Look at this line . You imageindex is always equal selectedimageindex . Because selectedimageindex can’t change , So you swipe imageview , the imageindex can change .
– stack Stevn
Aug 4 at 15:07
so how can I fix this ?
– User
Aug 5 at 15:55
I have edit the answer , Hope to solve your problem.
– stack Stevn
Aug 6 at 10:35
its working now .. thanks :)
– User
Aug 6 at 11:36
Issue is with when you reach to imageindex ==0 you should stop decrement and when you are index == getdataarray.count you should stop increment
- (IBAction)handleswipe:(UISwipeGestureRecognizer *)sender {
int imageindex = (int) selectedimageindex;
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction)
case UISwipeGestureRecognizerDirectionLeft:
if imageindex<0
imageindex++;
imageindex++;
break;
case UISwipeGestureRecognizerDirectionRight:
if imageindex>getdataarray.count
imageindex--;
break;
default:
break;
if (imageindex > -1 && imageindex < getdataarray.count)
[fullimageview sd_setImageWithURL:[getdataarray objectAtIndex: imageindex] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
And you should Use && operator instead of OR operator
not working .. still the same issue only on first time swipe is working after that nothing happens
– User
Aug 3 at 12:55
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
selectedimageindex is the index no of the image in which user clicks , this is collection view cell in which images is displaying getselectedimageindexno = (int) indexPath.row;
– User
Aug 4 at 13:29