如何激活页面控制?

时间:2014-03-05 22:21:34

标签: ios objective-c uiscrollview

页面控件无效,滚动浏览控制器时点不会改变。

继承人.m:

中的代码
@synthesize scrollView;
@synthesize pageControl;
@synthesize pageControlUsed = _pageControlUsed;
@synthesize page = _page;
@synthesize rotating = _rotating;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setDelegate:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
        [self loadScrollViewWithPage:i];
    }

    self.pageControl.currentPage = 0;
    _page = 0;
    [self.pageControl setNumberOfPages:[self.childViewControllers count]];

    UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
    if (viewController.view.superview != nil) {
        [viewController viewWillAppear:animated];
    }

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
}


- (void)loadScrollViewWithPage:(int)page {
    if (page < 0)
        return;
    if (page >= [self.childViewControllers count])
        return;

    // replace the placeholder if necessary
    UIViewController *controller = [self.childViewControllers objectAtIndex:page];
    if (controller == nil) {
        return;
    }

    // add the controller's view to the scroll view
    if (controller.view.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [self.scrollView addSubview:controller.view];
    }
}


// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _pageControlUsed = NO;
}

继承人.h:

 // Scroll View 
 @interface LevelOneAViewController : UIViewController <UIScrollViewDelegate>

 @property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
 @property (nonatomic, strong) IBOutlet UIPageControl *pageControl;

- (IBAction)changePage:(id)sender;

我正在使用故事板,我已连接

 @property (nonatomic, strong) IBOutlet UIPageControl *pageControl 

- 滚动视图上的页面控件。

2个视图控制器.h:

@interface ViewControllerTwo : LevelOneAViewController {

}

@property (strong, nonatomic) IBOutlet UIView *View1;
@property (strong, nonatomic) IBOutlet UIView *View2;

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;

和.m:

@implementation ViewControllerTwo

@synthesize View1;
@synthesize View2;

- (void)viewDidLoad
{
    // Do any additional setup after loading the view, typically from a nib.
    [super viewDidLoad];

    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View1"]];
    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View2"]];

}

当页面滚动到当前页面时,如何解决此问题以使点移动?

请帮忙

2 个答案:

答案 0 :(得分:0)

您需要计算您所在的页面,然后将pageControl设置为该页面。试试这个:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int newPage = lroundf(scrollView.contentOffset.x / scrollView.frame.size.width);
    _pageControl.currentPage = newPage;
}

答案 1 :(得分:0)

您将需要实现从基础scrollview

触发的委托调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

并将currentPage设置如下,

pageControl.currentPage = round(scrollView.contentOffset.x/scrollView.frame.size.width);

或者你可以遍历pageControl的子视图并尝试设置图像,首先是子类UIPageControl并在实现文件中添加这个

-(id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    self.activeImage = [[UIImage imageNamed:@"pagination_dot_active.png"] retain];
    self.inactiveImage = [[UIImage imageNamed:@"pagination_dot_up.png"] retain];

    return self;
}

-(void) updateDots
{
    for (int i = 0; i < [self.subviews count]; i++)
    {
        UIView* dotView = [self.subviews objectAtIndex:i];
        UIImageView* dot = nil;

        for (UIView* subview in dotView.subviews)
        {
            if ([subview isKindOfClass:[UIImageView class]])
            {
                dot = (UIImageView*)subview;
                break;
            }
        }

        if (dot == nil)
        {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, dotView.frame.size.width, dotView.frame.size.height)];
            [dotView addSubview:dot];
        }

        if (i == self.currentPage)
        {
            if(self.activeImage)
                dot.image = activeImage;
        }
        else
        {
            if (self.inactiveImage)
                dot.image = inactiveImage;
        }
    }
}

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

在头文件中或作为类扩展

声明变量
@property (nonatomic,retain) UIImage *activeImage;
@property (nonatomic,retain) UIImage *inactiveImage;

让我知道这是否有效。