使用UIScrollViewController的photoview

时间:2010-12-15 11:33:01

标签: iphone uiscrollview photo

我正在尝试在iPhone中制作像photo.app这样的相册。我知道我们有来自Apple的'PhotoScroller'示例(由于保密协议的原因,我认为我不能在此处复制和粘贴代码,而且目前还有点玩耍)。然而,我发现了随机搜索的另一个例子,它似乎基于PhotoScroller(非常简化的强硬)。该示例的链接是; http://ykyuen.wordpress.com/2010/05/22/iphone-uiscrollview-with-paging-example/

我将此代码应用于我的项目以制作相册(至少滚动),但我发现了一个问题。问题是我无法从我想要的页面开始。示例代码仅开始使用Page One滚动。代码本身似乎“真的”很简单,但不知何故,我无法从我选择的页面开始。

以下是我在Internet上找到的示例代码的主要部分。您当然可以通过上面的链接下载该项目。

- (void)viewDidLoad {

NSLog(@“ScrollViewWithPagingViewContrller :: viewDidLoad”);  [super viewDidLoad];

//视图控制器是懒惰创建的     //在此期间,使用占位符加载数组,这些占位符将根据需要进行替换     NSMutableArray * controllers = [[NSMutableArray alloc] init];     for(unsigned i = 0; i< kNumberOfPages; i ++){         [controllers addObject:[NSNull null]];     }     self.viewControllers = controllers;     [控制器发布];

// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;

// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];}

- (void)loadScrollViewWithPage:(int)page {

NSLog(@“ScrollViewWithPagingViewContrller :: loadScrollViewWithPage”);     if(page< 0)返回;     if(page> = kNumberOfPages)return;

// replace the placeholder if necessary


MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
    controller = [[MyViewController alloc] initWithPageNumber:page];
    [viewControllers replaceObjectAtIndex:page withObject:controller];
  //

[控制器发布];     }

// add the controller's view to the scroll view
if (nil == controller.view.superview) {
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    controller.view.frame = frame;
    [scrollView addSubview:controller.view];
}

}

- (void)scrollViewDidScroll:(UIScrollView *)sender {

NSLog(@“ScrollViewWithPagingViewContrller :: scrollViewDidScroll”);

// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
    // do nothing - the scroll was initiated from the page control, not the user dragging
    return;
}

// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;

int page = floor(scrollView.contentOffset.x - pageWidth / 2)/ pageWidth;     pageControl.currentPage = page;

// 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];

// A possible optimization would be to unload the views+controllers which are no longer visible

}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {  NSLog(@“ScrollViewWithPagingViewContrller :: scrollViewWillBeginDragging”);     pageControlUsed = NO; }

    // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     NSLog(@“ScrollViewWithPagingViewContrller :: scrollViewDidEndDecelerating”);  pageControlUsed = NO; }

- (IBAction)changePage:(id)sender {

NSLog(@“ScrollViewWithPagingViewContrller :: changePage”);     int page = pageControl.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];

//将滚动视图更新为相应的页面     CGRect frame = scrollView.frame;     frame.origin.x = frame.size.width * page;     frame.origin.y = 0;     [scrollView scrollRectToVisible:frame animated:YES];

//设置滚动源自UIPageControl时使用的布尔值。请参阅上面的scrollViewDidScroll :.     pageControlUsed = YES; }

2 个答案:

答案 0 :(得分:0)

我已经实现了相册视图,在第一个视图中我显示了所有缩略图图像,在点击每个图像时,它显示了全屏视图。你也scroo图像。 See this answer

一切顺利。

答案 1 :(得分:0)

答案是

scrollView1.contentOffset = CGPointMake(,);

很简单。花了很多时间。