我通过一些相应的图像创建了一个教程,并在其上添加了一个页面控件。另外,我添加了一个滑动手势,通过更改图片来响应。在下面的代码中,定义了一个开关盒,以便根据滑动方向(左/右)决定哪个图像和点应该是下一个。
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender
{
// NSLog(@"swiped");
NSArray *images;
if (self.selectedRow == 0) {
images = [[NSArray alloc] initWithObjects:@"w8.JPG",@"w7.JPG",@"w6.JPG",@"w5.JPG",@"w4.JPG",@"w3.JPG", @"w2.JPG", @"w1.JPG" ,@"w9.JPG", nil];
self.pageControl.hidden = NO;
[self.pageControl setNumberOfPages:9];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
if (pageController == 0 || imageIndex == 8)
{
self.Oops = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"You cannot swipe to the left, there are not any previous steps." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[self.Oops show];
}
else {
[self.pageControl setCurrentPage:pageController - 1];
pageController = pageController - 1;
}
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionLeft:
if (pageController < 8 && pageController >= 0) {
[self.pageControl setCurrentPage:pageController];
pageController = pageController + 1;
}
else {
[self.pageControl setCurrentPage: pageController ];
pageController = 0;
}
imageIndex--;
break;
default:
break;
}
NSLog(@"pagecontroller is %li and imageIndex is %li " , pageController, imageIndex);
imageIndex = (imageIndex < 0) ? ([images count] - 1): imageIndex % [images count];
self.imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
正如您在代码中看到的那样,有一个NSLog;当我运行应用程序时,一切正常,但是当我向右滑动以查看上一张图像时,尽管NSLog显示所有点和图像索引都正确变化,我们看到上一张图像,但这些点不会改变。再次向右滑动,点会改变,但由于必须分类的图像和点的顺序变得无序,例如最后一个图像用第一个点显示,所以它会变得迟。 我确定我的算法是正确的,因为我用NSLog检查了它,并且在向右滑动之前一切正常。 有什么想法吗?
我稍微更改了代码:
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
if (pageController != 0 && imageIndex != 9) {
[self.pageControl setCurrentPage:(pageController - 1)];
pageController --;
imageIndex++;
}
break;
case UISwipeGestureRecognizerDirectionLeft:
if (pageController <= 8 && pageController >= 0) {
[self.pageControl setCurrentPage:pageController];
pageController = pageController + 1;
imageIndex--;
}
break;
default:
break;
}
现在唯一的问题是,当我向不同的方向滑动时,页面控件会对当前点进行两次计数。
答案 0 :(得分:0)
您的imageIndex++
和imageIndex--
行位置错误。 (例如,如果pageController已经为0,则可能不想更改图像)。将它们移动到:
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender
{
// NSLog(@"swiped");
NSArray *images;
if (self.selectedRow == 0) {
images = [[NSArray alloc] initWithObjects:@"w8.JPG",@"w7.JPG",@"w6.JPG",@"w5.JPG",@"w4.JPG",@"w3.JPG", @"w2.JPG", @"w1.JPG" ,@"w9.JPG", nil];
self.pageControl.hidden = NO;
[self.pageControl setNumberOfPages:9];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
if (pageController == 0 || imageIndex == 8)
{
self.Oops = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"You should turn over to the left." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[self.Oops show];
}
else {
[self.pageControl setCurrentPage:pageController - 1];
pageController = pageController - 1;
imageIndex++;
}
break;
case UISwipeGestureRecognizerDirectionLeft:
if (pageController < 8 && pageController >= 0) {
[self.pageControl setCurrentPage:pageController];
pageController = pageController + 1;
imageIndex--;
}
else {
[self.pageControl setCurrentPage: pageController ];
pageController = 0;
}
break;
default:
break;
}
NSLog(@"pagecontroller is %li and imageIndex is %li " , pageController, imageIndex);
imageIndex = (imageIndex < 0) ? ([images count] - 1): imageIndex % [images count];
self.imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
在修改后的代码中,我认为您需要以不同的方式设置页面控件(到pageController + 1)以向左滑动:
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
if (pageController != 0 && imageIndex != 9) {
[self.pageControl setCurrentPage:(pageController - 1)];
pageController --;
imageIndex++;
}
break;
case UISwipeGestureRecognizerDirectionLeft:
if (pageController <= 8 && pageController >= 0) {
[self.pageControl setCurrentPage:pageController+1];
pageController = pageController + 1;
imageIndex--;
}
break;
default:
break;
}