开发者

uipagecontrol indicator(dot)issue

开发者 https://www.devze.com 2023-02-04 10:03 出处:网络
Hai All... I want to change the uipagecontroller indicator(dot) color...For that i\'m doing the below steps.

Hai All...

I want to change the uipagecontroller indicator(dot) color...For that i'm doing the below steps.

Create new class named as PageControl with 2 methods

1.- (void) setCurrentPage:(NSInteger)page

2.- (void) setNumberOfPages:(NSInteger)pages

- (void) setCurrentPage:(NSInteger)page 
{
    NSLog(@"setCurrentPage");
    [super setCurrentPage:page];
    NSString* imgActive = [[NSBundle mainBundle] pathForResource:@"activeimage" ofType:@"png"];
    NSString* imgInactive = [[NSBundle mainBundle] pathForResource:@"inactive" ofType:@"png"];
    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++)
    {
        UIImageView* subview= [self.subviews objectAtIndex:subviewIndex];

        if (subviewIndex == page) 
            [subview setImage:[UIImage imageWithContentsOfFile:imgActive]];          
        else
            [subview setImage:[UIImage imageWithContentsOfFile:imgInactive]];           

        subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, 10,10);
    }
}


- (void) setNumberOfPages:(NSInteger)pages 
{
    NSLog(@"setNumberOfPages");
    [super setNumberOfPages:pages];
    NSString* img = [[NSBundle mainBundle] pathForResource:@"inactive" ofType:@"png"];
    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++)
    {
        UIImageView* subview= [self.subviews objectAtIndex:subviewIndex];            
        [subview setImage:[UIImage imageWithContentsOfFile:img]];        
        subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, 10,10);
    }
}

Already i'm having one view with pagecontrol and开发者_JS百科 scrollview...Here what i'm doing is select the uipagecontroller from nib and choose identity inspector and select the class name PageControl(newly created class with 2 methods)it works for changing the color while swipe the images but,it didn't works for clicking the pagecontrol for moving to next page...

I don't know what shoud i want to include or what mistake i'm doing...please help me out to do this...

Thank You...


If you want to enable page scrolling on click of dot then you need to first of all associate the "value changed" property in your xib file to the action you set in your code.

I have implemented the same functionality in one of my projects so it will definitely work.

- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
CGFloat pageWidth = scrollView.frame.size.width;

pageWidth = scrollView.frame.size.width;
pageControl.currentPage = page;

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.

}

Cheers


Your above code is crashing in iOS 7.I have found a solution for this problem. I know it is not the way but Sure that it works till iOS 8 will be launched in the market.

Reason for Crash:

in iOS 7 [self.subViews objectAtIndex: i] returns UIView Instead of UIImageView and setImage is not the property of UIView and the app crashes. I solve my problem using this following code.

Check Whether the subview is UIView(for iOS7) or UIImageView(for iOS6 or earlier). And If it is UIView I am going to add UIImageView as subview on that view and voila its working and not crash..!!

-(void) updateDots
{
    for (int i = 0; i < [self.subviews count]; i++)
    {
        UIImageView * dot = [self imageViewForSubview:  [self.subviews objectAtIndex: i]];
        if (i == self.currentPage) dot.image = activeImage;
        else dot.image = inactiveImage;
    }
}
 - (UIImageView *) imageViewForSubview: (UIView *) view
{
    UIImageView * dot = nil;
    if ([view isKindOfClass: [UIView class]])
    {
        for (UIView* subview in view.subviews)
        {
            if ([subview isKindOfClass:[UIImageView class]])
            {
                dot = (UIImageView *)subview;
                break;
            }
        }
        if (dot == nil)
        {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
            [view addSubview:dot];
        }
    }
    else
    {
        dot = (UIImageView *) view;
    }

    return dot;
}

Hope this solve ur issue too for iOS7. and If Anypone find the optimal solution for that please comment. :)

Happy coding

0

精彩评论

暂无评论...
验证码 换一张
取 消