Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIControlEventValueChanged event is not triggered for the UIPageControl consistently

Lets assume we have ten pages.

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.

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.

- (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;
}
like image 554
RK- Avatar asked Nov 22 '12 07:11

RK-


1 Answers

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.

like image 194
jrturton Avatar answered Nov 24 '22 07:11

jrturton