我正在构建iOS应用程序的教程部分。本教程中有三页,并且我已经为每一页正确设置了UIPageIndicators
我需要将UIButton放在UIPageIndicators下面,并将图像放在背景中。我还附上了设计图片,以便您理解我的意思。ios - 在UIPageIndicator下面添加UIButton-LMLPHP

有什么想法吗?

先感谢您!

最佳答案

i have implement same thing in my project and it's working fine for me.

controller.h---->

 @interface FastTickIntroScreen : UIViewController<UIScrollViewDelegate>
    {
        IBOutlet UIButton *btnStartMessgaing;
        IBOutlet UIScrollView *scrViewForIntro;
        IBOutlet UIPageControl *pageControl;

        NSMutableArray *arrImages;

        NSInteger pos;
        NSInteger posScr;


        //    IBOutlet UIPageControl *pageControl;


    }
    - (IBAction)btnStartMessgaingTapped:(id)sender;


    controller.m---->

@implementation FastTickIntroScreen
{
    CGFloat lastContentOffset;
}

#pragma mark-
#pragma mark- UIbuttion Action Method


-(void)viewDidLoad{

    arrImages=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"1FastTick"],
               [UIImage imageNamed:@"2Chat"],
               [UIImage imageNamed:@"3Video"],
               [UIImage imageNamed:@"4Nearby"],
               [UIImage imageNamed:@"5Discover"], nil];

    NSArray *arrTitle=[NSArray arrayWithObjects:@"",@"Real time texting",@"Instant video messaging",@"Nearby",@"Discover", nil];

    pos=0;

    posScr=0;
    scrViewForIntro.pagingEnabled = YES;
    scrViewForIntro.showsHorizontalScrollIndicator = NO;
    scrViewForIntro.showsVerticalScrollIndicator = NO;
    [scrViewForIntro setDelegate:self];
    scrViewForIntro.contentSize=CGSizeMake(self.view.frame.size.width*arrImages.count, 50);

    UILabel *lbl;
    UIImageView*imgViewForIntro;

    for (int i=0; i<arrImages.count; i++) {

        imgViewForIntro=[[UIImageView alloc]initWithFrame:CGRectMake(pos, 0, self.view.frame.size.width,
                                                                     self.view.frame.size.height)];
        [imgViewForIntro setImage:[arrImages objectAtIndex:i]];
        [scrViewForIntro addSubview:imgViewForIntro];

        lbl = [[UILabel alloc]initWithFrame:CGRectMake(pos, self.view.frame.size.height-130, self.view.frame.size.width,
                                                       30)];
        [lbl setText:[arrTitle objectAtIndex:i]];
        [lbl setTextAlignment:NSTextAlignmentCenter];
        [lbl setTextColor:[UIColor blackColor]];
        [lbl setFont:KSetFont(kDefaultFontName, 20)];
        [lbl setBackgroundColor:CLEARCOLOUR];
        [scrViewForIntro addSubview:lbl];

        pos = pos+self.view.frame.size.width;
        imgViewForIntro.tag=i;
    }

    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageControl.numberOfPages = 5;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController.navigationBar setHidden:YES];
}

- (IBAction)btnStartMessgaingTapped:(id)sender {

//    Congratulations *objScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"Congratulations"];
//    [self.navigationController pushViewController:objScreen animated:YES];

    if(posScr<arrImages.count){
        posScr +=1;
        [scrViewForIntro scrollRectToVisible:CGRectMake(posScr*scrViewForIntro.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
        NSLog(@"Position: %li",(long)posScr);
        NSInteger pageNumber = roundf(scrViewForIntro.contentOffset.x / (scrViewForIntro.frame.size.width));
        pageControl.currentPage = pageNumber+=1;

        if (pageNumber == 5){
            TermsAndCondScreen *objScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"TermsAndCondScreen"];
            [self.navigationController pushViewController:objScreen animated:YES];
            posScr--;
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (lastContentOffset > scrollView.contentOffset.x)
    {
        CGFloat width = scrollView.frame.size.width;
        NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
        posScr=page;
    }
    else if (lastContentOffset < scrollView.contentOffset.x)
    {
        CGFloat width = scrollView.frame.size.width;
        NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
        posScr=page;
    }
    lastContentOffset = scrollView.contentOffset.x;

}

- (IBAction)changePage:(id)sender {
    CGFloat x = pageControl.currentPage * scrViewForIntro.frame.size.width;
    [scrViewForIntro setContentOffset:CGPointMake(x, 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  {
    NSInteger pageNumber = roundf(scrViewForIntro.contentOffset.x / (scrollView.frame.size.width));
    pageControl.currentPage = pageNumber;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}


@end

关于ios - 在UIPageIndicator下面添加UIButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33014377/

10-14 21:38