如何在ios中自定义网页浏览指标

时间:2015-05-21 11:32:07

标签: ios objective-c

enter image description here 我想在我的视图控制器中以下列方式显示页面指示器。对于访问页面我想显示绿色刻度标记\图像。

1 个答案:

答案 0 :(得分:0)

UIPageControl创建自定义类,我们将其命名为'customPageContoller':

CustomPageContoller.h:

@interface CustomPageContoller : UIPageControl
{
    UIImage* activeImage;
    UIImage* inactiveImage;
}

CustomPageContoller.m文件:

@implementation CustomPageContoller

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    activeImage = [UIImage imageNamed:@"active_page_image.png"] ;
    inactiveImage = [UIImage imageNamed:@"inactive_page_image.png"] ;

    return self;
}

-(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;
}

-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    [self updateDots];
}

现在您的View控制器需要自定义页面控制器:

ViewController.m文件:

#import "CustomPageContoller.h"

@interface PullRefreshViewController : UIViewController{
    IBOutlet CustomPageContoller *PageIndicator;
}

现在为UIPageControl添加一个动作:

ViewController.m文件:

-(IBAction) pageChanged:(id)sender
{
    NSInteger page = PageIndicator.currentPage;

    // update the scroll view to the appropriate page
    CGRect frame = ImagesScroller.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [ImagesScroller scrollRectToVisible:frame animated:YES];
}

在ViewController界面构建器中,将自定义类UIPageControl设置为CustomPageContoller。并使用您的自定义UIPageControl进行IBOutlet连接,例如PageIndicator

不要忘记将active_page_image.pngInactive_page_image.png添加到您的项目中。

P.S - 致JWD And iProgrammer

的积分