我在图像视图上添加了两个swipegesture,一个用于右,一个用于左。我想要的是,使用时单击任何图像并向左或向右滑动,图像就会像我们在手机图库中那样相应地更改。但是当我向左或向右滑动时,一次图像更改之后什么也没有发生。所有这些图像都来自数组,数组名称为getdataarray。这是我的代码
.h文件
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *rightgesture;
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *leftgesture;
- (IBAction)handleswipe:(UISwipeGestureRecognizer *)sender;
.m文件
-(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"]];
}
答案 0 :(得分:0)
问题在于当imageindex == 0时应停止递减,而当index == getdataarray.count时应停止递增
- (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"]];
}
然后,您应该使用&&运算符而不是OR运算符
答案 1 :(得分:0)
selectimageIndex是什么?我从selectimageIndex放问题;
由于您一次滑动了图像视图,因此方法“ handleswipe:”将运行一次。所以imageindex始终等于selectimageIndex。请中断一点以查看imageindex值。
-
-(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"]];
}
}