Lets assume we have ten pages.
假设我们有10页。
An event handler for the event is added as below:
事件的事件处理程序添加如下:
Run the app. Now in the ten pages, by default page 1 (index 0) is selected. Touch the second page or third page. The event wont be triggered. If the last page is selected, the event will be triggered. The same happens with last page. Once you have selected the last page, select the previous page. The event will not be triggered, but if you select the first page, the event will not be triggered.
运行app。现在在10个页面中,默认选择1页(索引0)。触摸第二页或第三页。事件不会被触发。如果选择了最后一页,则将触发事件。最后一页也是如此。选择最后一页后,选择前一页。事件将不会被触发,但是如果您选择第一页,事件将不会被触发。
To see an easy demonstration of this case, download the UICatalog example and open the ControlsViewController.m and change the UIControlEventTouchUpInside to UIControlEventValueChanged in line no 375.
要查看这个示例的简单演示,请下载UICatalog示例并打开ControlsViewController。m并将UIControlEventTouchUpInside改为UIControlEventValueChanged,在第375行中。
- (UIPageControl *)pageControl
{
if (pageControl == nil)
{
CGRect frame = CGRectMake(120.0, 14.0, 178.0, 20.0);
pageControl = [[UIPageControl alloc] initWithFrame:frame];
[pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
pageControl.backgroundColor = [UIColor grayColor];
pageControl.numberOfPages = 10; // must be set or control won't draw
pageControl.currentPage = 0;
pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return pageControl;
}
2 个解决方案
#1
10
You might be misunderstanding how the page control works. It is a visual indicator of the number of pages present, but tapping on a particular dot doesn't go to that page. You only ever move one page at a time, tapping on the left half moves back a page, and on the right half moves forward a page.
您可能误解了页面控件的工作方式。它是一个显示页面数量的视觉指示器,但是点击一个特定的点不会到达页面。你每次只移动一页,点击左半页移回一页,在右半页移回一页。
If you're on the first page, and you tap on the left half, you can't move back another page, so nothing happens.
如果你在第一页,你点击左半页,你不能再回到另一页,所以什么都不会发生。
I don't really like that behaviour, particularly on the iPad, so usually use a subclass or my own touch handling to work out if the touch location is to the left or right of the currently selected page, and send the event appropriately.
我不喜欢这种行为,尤其是在iPad上,所以通常使用子类或我自己的触摸处理来确定触摸位置是在当前选择的页面的左边还是右边,并适当地发送事件。
#2
0
You can use UITapGestureRecognizer instead of using addTarget:self action:@selector()
可以使用UITapGestureRecognizer而不是addTarget:self action:@selector()
//Added UITapGestureRecognizer instead of using addTarget: method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
tapGesture addTarget:self action:@selector(pageAction:) ];
[pageControl addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;
-(void)pageAction:(UITapGestureRecognizer *)tapGesture{
UIPageControl *pageControl = (UIPageControl *)tapGesture.view;
NSLog(@"page Number: %d",pageControl.currentPage);
}
#1
10
You might be misunderstanding how the page control works. It is a visual indicator of the number of pages present, but tapping on a particular dot doesn't go to that page. You only ever move one page at a time, tapping on the left half moves back a page, and on the right half moves forward a page.
您可能误解了页面控件的工作方式。它是一个显示页面数量的视觉指示器,但是点击一个特定的点不会到达页面。你每次只移动一页,点击左半页移回一页,在右半页移回一页。
If you're on the first page, and you tap on the left half, you can't move back another page, so nothing happens.
如果你在第一页,你点击左半页,你不能再回到另一页,所以什么都不会发生。
I don't really like that behaviour, particularly on the iPad, so usually use a subclass or my own touch handling to work out if the touch location is to the left or right of the currently selected page, and send the event appropriately.
我不喜欢这种行为,尤其是在iPad上,所以通常使用子类或我自己的触摸处理来确定触摸位置是在当前选择的页面的左边还是右边,并适当地发送事件。
#2
0
You can use UITapGestureRecognizer instead of using addTarget:self action:@selector()
可以使用UITapGestureRecognizer而不是addTarget:self action:@selector()
//Added UITapGestureRecognizer instead of using addTarget: method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
tapGesture addTarget:self action:@selector(pageAction:) ];
[pageControl addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;
-(void)pageAction:(UITapGestureRecognizer *)tapGesture{
UIPageControl *pageControl = (UIPageControl *)tapGesture.view;
NSLog(@"page Number: %d",pageControl.currentPage);
}