實現 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM