0

我正在使用由水平滚动视图和页面控制器组成的视图控制器以轮播方式显示许多图像。控制器被传递一个图像数组并从第一张图像开始显示它们。我从第三方教程中获得了代码片段,并在最后一天试图理解它并更改其功能。我一直试图让旋转木马从图像数组的中间开始(即,如果有九张图像从图像五开始并允许双向滚动),但在修改现有代码方面没有运气。我玩过 loadPage 和 loadVisiblePages 方法,但运气不佳。这是 .m 和 .h 文件。

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

谢谢

4

2 回答 2

2

只需滚动视图设置它的内容大小/// 这是它要滚动多少

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

// 然后设置起点

scrollView.contentOffset = CGPointMake(currentpage * scrollView.bounds.size.width, 0);

于 2013-05-29T04:27:29.020 回答
1

I think you only need to scroll to a particular page. The rest of the thing will automatically happens. Try like this

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

Here page is the page number(eg 5)

于 2013-05-29T04:20:35.170 回答