滑动手势问题
Swipe Gesture issue
我在图像视图中添加了两个滑动手势。一个用于右侧,一个用于左侧。我想要的是,当使用单击任何图像并向左或向右滑动时,图像会相应地改变,就像我们在 phone 图库中所做的那样。但是当我向左或向右滑动一次时,图像发生变化,之后什么也没有发生。所有这些图像都来自数组,数组名称是 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"]];
}
问题在于,当您到达 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"]];
}
And 你应该使用 && 运算符而不是 OR 运算符
什么是 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"]];
}
}
我在图像视图中添加了两个滑动手势。一个用于右侧,一个用于左侧。我想要的是,当使用单击任何图像并向左或向右滑动时,图像会相应地改变,就像我们在 phone 图库中所做的那样。但是当我向左或向右滑动一次时,图像发生变化,之后什么也没有发生。所有这些图像都来自数组,数组名称是 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"]];
}
问题在于,当您到达 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"]];
}
And 你应该使用 && 运算符而不是 OR 运算符
什么是 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"]];
}
}