UUPageControl不会始终触发UIControlEventValueChanged事件

时间:2012-11-22 07:46:11

标签: ios uipagecontrol uicontrolevents

假设我们有十页。

该事件的事件处理程序添加如下:

运行该应用。 现在在十页中,默认选择页面1(索引0)。触摸第二页或第三页。该事件不会被触发。如果选择了最后一页,则将触发该事件。最后一页也是如此。选择最后一页后,选择上一页。该事件不会被触发,但如果您选择第一页,则不会触发该事件。

要查看此案例的简单演示,请下载UICatalog示例并打开ControlsViewController.m并将第375行中的UIControlEventTouchUpInside更改为UIControlEventValueChanged。

- (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 个答案:

答案 0 :(得分:10)

您可能误解了页面控件的工作原理。它是存在的页数的可视指示器,但点击特定点不会转到该页面。您一次只能移动一个页面,点击左半部分向后移动页面,而在右半部分向前移动页面。

如果您在第一页上,并点击左半边,则无法移回另一页,因此没有任何反应。

我真的不喜欢这种行为,特别是在iPad上,所以通常使用子类或我自己的触摸处理来确定触摸位置是否位于当前所选页面的左侧或右侧,并发送事件适当。

答案 1 :(得分:0)

您可以使用 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);


}
相关问题