实现 scrollview 默认显示指定的页码

思路:用scrollview的偏移量来实现

以下代码运行成功

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController<UIScrollViewDelegate>
{
    //上面的scrollview
    UIScrollView *scrollView0;
    UIPageControl *pageControl0;         //页面控制控件    tag 已在xib文件中设置为0
    NSMutableArray *arrImageViews;       //相当于datasource
    BOOL isLoadScrollView0;              //是否加载
    

    BOOL pageControlUsed;
    
}
@property (nonatomic,retain) IBOutlet UIScrollView *scrollView0;
@property (nonatomic,retain) IBOutlet UIPageControl *pageControl0;
@property BOOL isLoadScrollView0; 

- (IBAction)changePage:(id)sender;

@end
#import "MainViewController.h"

static NSUInteger fNumberOfPages = 4;

@interface MainViewController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end

@implementation MainViewController
@synthesize scrollView0, pageControl0;
@synthesize isLoadScrollView0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    /* 
     初始化scrollView0相关内容
     */
    //获取scrollView0要显示的相关内容
    
    arrImageViews = [[NSMutableArray alloc]init];
    for (int i = 0; i < fNumberOfPages; i++) {
        UIImage *tempImage = [[UIImage alloc]init];
        NSString *imageName = [NSString stringWithFormat:@"pic0%d.png", i + 1];
        tempImage = [UIImage imageNamed:imageName];
        UIImageView *view = [[UIImageView alloc] initWithImage:tempImage];
        [arrImageViews addObject:view];
    }
    // scrollView0 初始化
    scrollView0.pagingEnabled = YES;
    scrollView0.contentSize = CGSizeMake(scrollView0.frame.size.width * fNumberOfPages, scrollView0.frame.size.height);
    scrollView0.showsHorizontalScrollIndicator = NO;
    scrollView0.showsVerticalScrollIndicator = NO;
    scrollView0.scrollsToTop = NO;
    scrollView0.delegate = self;
    scrollView0.tag = 1000;
    
    pageControl0.numberOfPages = fNumberOfPages;
    pageControl0.currentPage = 0;     //这个只改变了pagecontrol 被选中的位置
    
    isLoadScrollView0 = YES;
    
    //使用如下3句代码 实现默认显示 scrollview 指定的页
    CGPoint pt = CGPointMake(640, 0);      
    [scrollView0 setContentOffset:pt];        //设置scrollview 的偏移量
    [self scrollViewDidScroll:scrollView0];   //模拟scrollview 被滑动
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (IBAction)changePage:(id)sender
{
    if ([sender tag] == 0) {
        int page = pageControl0.currentPage;
        
        // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
        [self loadScrollViewWithPage:page - 1];
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page + 1];
        
        // update the scroll view to the appropriate page
        CGRect frame = scrollView0.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        [scrollView0 scrollRectToVisible:frame animated:YES];
        
        // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
        pageControlUsed = YES;
    }
}

- (void)loadScrollViewWithPage:(int)page
{
    
    if (isLoadScrollView0 == YES) {
        if (page < 0)return;
        if (page >= fNumberOfPages)return;
        // 获取数据
        UIImageView *view = [arrImageViews objectAtIndex:page];
        
        CGRect frame = scrollView0.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        view.frame = frame;
        [scrollView0 addSubview:view];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if ([sender tag] == 1000) {
       
        //设置加载的对象
        isLoadScrollView0 = YES;
        
        if (pageControlUsed) {
            return;
        }
        
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = scrollView0.frame.size.width;
        NSLog(@"scrollView0.contentOffset.x  === %f",scrollView0.contentOffset.x );
        int page = floor((scrollView0.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        pageControl0.currentPage = page;
        
        [self loadScrollViewWithPage:page - 1];
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page + 1];
    }
}

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

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


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}
@end
原文地址:https://www.cnblogs.com/ygm900/p/3092309.html