2013-05-29 108 views
0

我正在使用由水平滾動視圖和頁面控制器組成的視圖控制器以卡魯塞爾方式顯示大量圖像。控制器傳遞一組圖像並從第一個圖像開始顯示它們。我從第三方教程中獲得了代碼片段,並花了最後一天的時間試圖理解它並改變它的功能。我一直試圖使卡魯塞爾從圖像陣列的中間開始(即,如果從圖像5開始有九個圖像並且允許兩種方式滾動),但是在修改現有代碼方面沒有運氣。我玩過loadPage和loadVisiblePages方法,但運氣不大。這裏是.m和.h文件。IOS幫助修改代碼片段

PeekPagedScrollViewController.m

#import "PeekPagedScrollViewController.h" 

@interface PeekPagedScrollViewController() 
@property (nonatomic, strong) NSMutableArray *pageViews; 

- (void)loadVisiblePages; 
- (void)loadPage:(NSInteger)page; 
- (void)purgePage:(NSInteger)page; 
@end 

@implementation PeekPagedScrollViewController 

@synthesize scrollView = _scrollView; 
@synthesize pageControl = _pageControl; 
@synthesize gallaryType = _gallaryType; 
@synthesize pageImages = _pageImages; 
@synthesize pageViews = _pageViews; 
@synthesize startPoint = _startPoint; 

#pragma mark - 

- (void)loadVisiblePages 
{ 
    // First, determine which page is currently visible 
    CGFloat pageWidth = self.scrollView.frame.size.width; 
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth)/(pageWidth * 2.0f)); 

    // Work out which pages we want to load 
    NSInteger firstPage = page - 1; 
    NSInteger lastPage = page + 1; 

    // Purge anything before the first page 
    for (NSInteger i=0; i<firstPage; i++) { 
     [self purgePage:i]; 
    } 
    for (NSInteger i=firstPage; i<=lastPage; i++) { 
     [self loadPage:i]; 
    } 
    for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) { 
     [self purgePage:i]; 
    } 
} 

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

- (void)loadPage:(NSInteger)page 
{ 
    if (page < 0 || page >= self.pageImages.count) 
    { 
     // If it's outside the range of what we have to display, then do nothing 
     return; 
    } 

    // Load an individual page, first seeing if we've already loaded it 
    UIView *pageView = [self.pageViews objectAtIndex:page]; 

    if ((NSNull*)pageView == [NSNull null]) 
    { 
     CGRect frame = self.scrollView.bounds; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0.0f; 
     frame = CGRectInset(frame, 10.0f, 0.0f); 

     UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]]; 
     newPageView.contentMode = UIViewContentModeScaleAspectFit; 
     newPageView.frame = frame; 

     [self addTapHandler:newPageView actionSelector:@selector(handleSingleTap:)]; 
     [self.scrollView addSubview:newPageView]; 
     [self.pageViews replaceObjectAtIndex:page withObject:newPageView]; 
    } 


} 

- (void)purgePage:(NSInteger)page 
{ 
    if (page < 0 || page >= self.pageImages.count) 
    { 
     // If it's outside the range of what we have to display, then do nothing 
     return; 
    } 

    // Remove a page from the scroll view and reset the container array 
    UIView *pageView = [self.pageViews objectAtIndex:page]; 
    if ((NSNull*)pageView != [NSNull null]) 
    { 
     [pageView removeFromSuperview]; 
     [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]]; 
    } 
} 

- (void)addTapHandler:(UIView *)pageView actionSelector:(SEL)actionSelector 
{ 
    [pageView setUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:actionSelector]; 
    [pageView addGestureRecognizer:tapper]; 
} 

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer 
{ 
    if (_gallaryType == 0) 
    { 
     NSString *messageString = @"Use as profile image?"; 
     UIAlertView *imageAlertView = [[UIAlertView alloc] initWithTitle:@"Select Image" message:messageString delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
     [imageAlertView show]; 

    } 
    else if (_gallaryType == 1) 
    { 
     NSString *messageString = @"Use as cover photo?"; 
     UIAlertView *imageAlertView = [[UIAlertView alloc] initWithTitle:@"Select Image" message:messageString delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
     [imageAlertView show]; 

    } 
} 

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) 
    { 
     // add yes button text here 
    } 
} 

#pragma mark - 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.view setTranslatesAutoresizingMaskIntoConstraints:YES]; 

    self.title = @"Paged"; 

    NSInteger pageCount = self.pageImages.count; 

    // Set up the array to hold the views for each page 
    self.pageViews = [[NSMutableArray alloc] init]; 

    for (NSInteger i = 0; i < pageCount; ++i) 
    { 
     [self.pageViews addObject:[NSNull null]]; 
    } 
} 

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

    // Set up the content size of the scroll view 
    CGSize pagesScrollViewSize = self.scrollView.frame.size; 
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height); 

    // Load the initial set of pages that are on screen 
    [self loadVisiblePages]; 
} 

- (IBAction)back:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    self.scrollView = nil; 
    self.pageControl = nil; 
    self.pageImages = nil; 
    self.pageViews = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 


#pragma mark - UIScrollViewDelegate 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // Load the pages which are now on screen 
    [self loadVisiblePages]; 
} 

@end 

PeekPagedScrollViewController.h

#import <UIKit/UIKit.h> 

@interface PeekPagedScrollViewController : UIViewController <UIScrollViewDelegate, UIAlertViewDelegate> 

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView; 
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl; 
@property (nonatomic, strong) NSArray *pageImages; 
@property (nonatomic) NSInteger *gallaryType; 
@property (nonatomic) NSInteger *startPoint; 

- (IBAction)back:(id)sender; 

@end 

感謝

回答

2

只是採取滾動視圖 設置其內容的大小///這是它的多少將滾動

滾動視圖.contentSize = CGSizeMake(scrollView.bounds.size.width * imageArray.count,scrollView.frame.size.height);

//然後設置起點

scrollView.contentOffset = CGPointMake(當前頁* scrollView.bounds.size.width,0);

+1

感謝man溢出總是這麼好幫助@alpha – ScottOBot

1

我想你只需要滾動到一個特定的頁面。剩下的事情會自動發生。試着這樣

CGRect frame = self.scrollView.frame; 
frame.origin.x = (frame.size.width * page);   
frame.origin.y = 0; 
[self.scrollView scrollRectToVisible:frame animated:YES]; 

這頁是數字(例如5)