我想在我的视图控制器中以下列方式显示页面指示器。对于访问页面我想显示绿色刻度标记\图像。
答案 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.png
和Inactive_page_image.png
添加到您的项目中。
P.S - 致JWD And iProgrammer
的积分