我想做滑动手势来连续滑动 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)swipegestureUISwipeGestureRecognizer *)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] 的值,我不知道如何停止它。
Best Answer-推荐答案 strong>
我建议在有效性检查之前增加或减少您的索引值(您将其命名为增量),如果它无效,则在 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 语句(使用该建议将不再需要该语句)并记录您的 FAIL。
关于ios - 滑动手势滑动 UIViews,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/8808323/
|