【问题标题】:Swipe Gesture issue滑动手势问题
【发布时间】:2018-08-03 10:46:20
【问题描述】:

我在图像视图中添加了两个滑动手势。一个用于右侧,一个用于左侧。我想要的是,当使用单击任何图像并向左或向右滑动时,图像会相应地改变,就像我们在手机图库中所做的那样。但是当我向左或向右滑动时,只有一次图像更改,之后没有任何反应。所有这些图像都来自数组,数组名称是 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"]];
    }

【问题讨论】:

    标签: ios objective-c uiswipegesturerecognizer


    【解决方案1】:

    什么是 selectimageIndex ?我从 selectimageIndex 解决问题;

    因为你刷了一次 imageview,方法 '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"]];
       }
      }
    

    【讨论】:

    • selectedimageindex 是用户点击的图像的索引号,这是显示图像的集合视图单元格 getselectedimageindexno = (int) indexPath.row;
    • int imageindex = (int) selectedimageindex;看看这条线。你的 imageindex 总是等于 selectedimageindex 。因为 selectedimageindex 不能改变,所以你滑动 imageview ,imageindex 可以改变。
    • 那么我该如何解决这个问题?
    • 我已经编辑了答案,希望能解决你的问题。
    【解决方案2】:

    问题在于,当您到达 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 运算符

    【讨论】:

    • 不工作 .. 仍然是同样的问题,只有在第一次刷卡工作之后没有任何反应
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多