IOS帮助修改代码段

时间:2013-05-29 03:38:01

标签: iphone ios uiscrollview xcode4.5

我正在使用一个视图控制器,它包含一个水平滚动视图和页面控制器,以转盘方式显示多个图像。控制器传递一组图像并从第一个图像开始显示它们。我从第三方教程中获取了代码片段,并在最后一天尝试了解它并更改其功能。我一直试图让转盘从图像阵列的中间开始,(即如果有九个图像从图像5开始并允许双向滚动),但是在修改现有代码方面没有运气。我玩过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

由于

2 个答案:

答案 0 :(得分:2)

只需滚动视图 设置其内容大小///即滚动的内容

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

//然后设置起点

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

答案 1 :(得分:1)

我认为您只需要滚动到特定页面。事情的其余部分将自动发生。试试这个

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

此页面是页码(例如5)