滑动手势滑动 UIView

发布于 2024-12-25 21:07:41 字数 3586 浏览 1 评论 0原文

我想做滑动手势来连续滑动 UIView 并从中获取数据。考虑每个 UIView 中的每个单词。我将数据存储在数组中,并在转换时打印在 UIView 的标签中。但是,当我在显示所有数据后尝试滑动时,程序停止工作。我的项目没有显示任何错误。请帮我解决一下。

这是我的数组:

addArray = [[NSMutableArray alloc]initWithCapacity:4];
[addArray insertObject:@"10" atIndex:0];
[addArray insertObject:@"20" atIndex:1];
[addArray insertObject:@"30" atIndex:2];
[addArray insertObject:@"40" atIndex:3];

flippedArray = [[NSMutableArray alloc] initWithCapacity:4];
[flippedArray insertObject:@"100" atIndex:0];
[flippedArray insertObject:@"200" atIndex:1];
[flippedArray insertObject:@"300" atIndex:2];
[flippedArray insertObject:@"400" atIndex:3];

这是我的手势识别器编码:

-(void)swipegesture:(UISwipeGestureRecognizer *)recognizer{

    CGPoint location = [recognizer locationInView:additionalView];   
    if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
    {
        if (increment<[addArray count]) 
        {
            NSLog(@"%d",[addArray count]);
            increment++;
            if(increment==[addArray count])
            {
                NSLog(@"Fail");
                //[recognizer requireGestureRecognizerToFail:swipeGesture];
                [recognizer setEnabled:NO];
            }
            else
            {
                additionalLabel.text=[[NSString alloc] initWithFormat:@"%@",
                                         [addArray objectAtIndex:increment]];
                flippedLabel.text = [[NSString alloc] initWithFormat:@"%@",
                                         [flippedArray objectAtIndex:increment]];
                NSLog(@"increment %d",increment);
                [UIView animateWithDuration:0.55 animations:^{
                    [UIView setAnimationDelay:0.2];
                }];
                CATransition *animation = [CATransition animation];
                [animation setType:kCATransitionPush];
                [animation setSubtype:kCATransitionFromRight];
                [animation setTimingFunction:[CAMediaTimingFunction
                               functionWithName:kCAMediaTimingFunctionDefault]];
                [animation setSpeed:0.4];
                [[additionalView  layer] addAnimation:animation forKey:nil];
            }
        }
    }
    else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        if (increment>=0 && increment<[addArray count]) 
        {
            increment--;
            if(increment>[addArray count])
            {
                additionalLabel.text=[[NSString alloc]initWithFormat:@"%@",
                                         [addArray objectAtIndex:increment]];
                flippedLabel.text=[[NSString alloc]initWithFormat:@"%@",
                                      [flippedArray objectAtIndex:increment]];
                NSLog(@"Decrement %d",increment);
                [UIView animateWithDuration:0.55 animations:^{
                    [UIView setAnimationDelay:0.2];
                }];
                CATransition *animation = [CATransition animation];
                [animation setType:kCATransitionPush];
                [animation setSubtype:kCATransitionFromLeft];
                [animation setTimingFunction:[CAMediaTimingFunction 
                               functionWithName:kCAMediaTimingFunctionDefault]];
                [animation setSpeed:0.4];
                [[additionalView layer] addAnimation:animation forKey:nil];
            }
        }
    }
}

仅增量时存在问题。我的 NSLog 打印失败。但我不知道如果手势识别器达到 [addArray count] 的值,如何停止它。

I want to do swipe gesture to slide the UIView continuously and getting data from it. Consider each word in each UIView. I stored data in an array and printed in label of UIView while transition. But when I try to swipe after shown all data program get stopped working. My project showing no errors. Please help me with it.

This is my array:

addArray = [[NSMutableArray alloc]initWithCapacity:4];
[addArray insertObject:@"10" atIndex:0];
[addArray insertObject:@"20" atIndex:1];
[addArray insertObject:@"30" atIndex:2];
[addArray insertObject:@"40" atIndex:3];

flippedArray = [[NSMutableArray alloc] initWithCapacity:4];
[flippedArray insertObject:@"100" atIndex:0];
[flippedArray insertObject:@"200" atIndex:1];
[flippedArray insertObject:@"300" atIndex:2];
[flippedArray insertObject:@"400" atIndex:3];

This is my gesture recognizer coding:

-(void)swipegesture:(UISwipeGestureRecognizer *)recognizer{

    CGPoint location = [recognizer locationInView:additionalView];   
    if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
    {
        if (increment<[addArray count]) 
        {
            NSLog(@"%d",[addArray count]);
            increment++;
            if(increment==[addArray count])
            {
                NSLog(@"Fail");
                //[recognizer requireGestureRecognizerToFail:swipeGesture];
                [recognizer setEnabled:NO];
            }
            else
            {
                additionalLabel.text=[[NSString alloc] initWithFormat:@"%@",
                                         [addArray objectAtIndex:increment]];
                flippedLabel.text = [[NSString alloc] initWithFormat:@"%@",
                                         [flippedArray objectAtIndex:increment]];
                NSLog(@"increment %d",increment);
                [UIView animateWithDuration:0.55 animations:^{
                    [UIView setAnimationDelay:0.2];
                }];
                CATransition *animation = [CATransition animation];
                [animation setType:kCATransitionPush];
                [animation setSubtype:kCATransitionFromRight];
                [animation setTimingFunction:[CAMediaTimingFunction
                               functionWithName:kCAMediaTimingFunctionDefault]];
                [animation setSpeed:0.4];
                [[additionalView  layer] addAnimation:animation forKey:nil];
            }
        }
    }
    else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        if (increment>=0 && increment<[addArray count]) 
        {
            increment--;
            if(increment>[addArray count])
            {
                additionalLabel.text=[[NSString alloc]initWithFormat:@"%@",
                                         [addArray objectAtIndex:increment]];
                flippedLabel.text=[[NSString alloc]initWithFormat:@"%@",
                                      [flippedArray objectAtIndex:increment]];
                NSLog(@"Decrement %d",increment);
                [UIView animateWithDuration:0.55 animations:^{
                    [UIView setAnimationDelay:0.2];
                }];
                CATransition *animation = [CATransition animation];
                [animation setType:kCATransitionPush];
                [animation setSubtype:kCATransitionFromLeft];
                [animation setTimingFunction:[CAMediaTimingFunction 
                               functionWithName:kCAMediaTimingFunctionDefault]];
                [animation setSpeed:0.4];
                [[additionalView layer] addAnimation:animation forKey:nil];
            }
        }
    }
}

There is a problem while increment only. I got NSLog print as FAIL. But I dont how to stop gesture recognizer if it reaches the value as [addArray count].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2025-01-01 21:07:41

我建议在有效性检查之前增加或减少索引值(您将其命名为增量),如果无效,则在 else 中反转您的操作。像这样:

if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
{
    increment++;

    if (increment<[addArray count]) 
    {
        // Your code
    }
    else
    {
        increment--; // The increment would pass the range of the array, set it back.
    }
}

另一个方向也是如此。

编辑:为了澄清,最初的问题是您检查以确保索引有效,但是,通过在检查后递增,您最终会使其无效。使用您的示例,当增量为 3(数组的最高索引)时,它实际上小于数组的计数,即 4。然后您将索引增加到 4,这将超出范围,或者,在您的案例,落入 if 语句(使用建议将不再需要)并记录您的失败。

I would recommend incrementing or decrementing your index value (which you named increment) before the validity check, and if it is not valid reverse your action in an else. Like so:

if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
{
    increment++;

    if (increment<[addArray count]) 
    {
        // Your code
    }
    else
    {
        increment--; // The increment would pass the range of the array, set it back.
    }
}

And likewise for the other direction.

EDIT: To clarify, the original problem is that you check to make sure your index is valid, but, by incrementing after the check you end up making it invalid. Using your example, when the increment is 3 (the highest index of your array) it is in fact less than the array's count, which is 4. You then increment your index to 4, which would be out of bounds, or, in your case, fall into that if statement (which will no longer be needed using the suggestion) and log your FAIL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文