滑动手势滑动 UIView
我想做滑动手势来连续滑动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在有效性检查之前增加或减少索引值(您将其命名为增量),如果无效,则在 else 中反转您的操作。像这样:
另一个方向也是如此。
编辑:为了澄清,最初的问题是您检查以确保索引有效,但是,通过在检查后递增,您最终会使其无效。使用您的示例,当增量为 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:
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.