使用 UIScrollViewController 进行照片查看
我正在尝试在 iPhone 中制作像 photo.app 这样的相册。我知道我们有来自 Apple 的“PhotoScroller”示例(由于保密协议,我认为我无法将代码复制并粘贴到此处,而且目前使用起来有点大)。然而,我从随机搜索中发现了另一个例子,它似乎是基于 PhotoScroller (非常简化困难)。该示例的链接是; http://ykyuen.wordpress.com/2010/ 05/22/iphone-uiscrollview-with-paging-example/
我将此代码应用到我的项目中来制作相册(至少它可以滚动),但我发现了一个问题。问题是我无法从我想要的页面开始。示例代码仅从第一页开始滚动。代码本身看起来“非常”简单,但不知何故我无法让它从我选择的页面开始。
这是我在互联网上找到的示例代码的主要部分。您当然可以通过上面的链接下载该项目。
- (void)viewDidLoad {
NSLog(@"ScrollViewWithPagingViewContrller::viewDidLoad"); [超级viewDidLoad];
// 视图控制器是延迟创建的 // 同时,加载带有占位符的数组,这些占位符将根据需要进行替换 NSMutableArray *controllers = [[NSMutableArray alloc] init]; for (无符号 i = 0; i < kNumberOfPages; i++) { [控制器addObject:[NSNull null]]; } self.viewControllers = 控制器; [控制者发布];
// 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"); 如果(页数<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];
//
[控制器释放]; NSLog
// 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];
}
(@"ScrollViewWithPagingViewContrller::scrollViewDidScroll" )
- (void)scrollViewDidScroll:(UIScrollView *)sender {
;
// 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 = 页;
// 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"); 页面控制已使用=否; }
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"ScrollViewWithPagingViewContrller::scrollViewDidEndDecelerating"); 页面控制已使用=否; NSLog
- (IBAction)changePage:(id)sender {
(@"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框架=scrollView.frame; frame.origin.x =frame.size.width * 页; 框架.origin.y = 0; [scrollViewscrollRectToVisible:帧动画:是];
// 设置当滚动源自 UIPageControl 时使用的布尔值。请参见上面的scrollViewDidScroll:。 页面控制已使用=是; }
I'm trying to make photo album like photo.app in iPhone. I know We have 'PhotoScroller' example from Apple (which I don't think I can copy and paste the code here due to the Non Disclosure Agreement, plus it's bit biggy to play with at the moment). However I found an another example from random search and it seems like based on the PhotoScroller (very simplified tough). The link for the example is ;
http://ykyuen.wordpress.com/2010/05/22/iphone-uiscrollview-with-paging-example/
I applied this code to my project to make photo album (and it scrolls, at least), but I found a problem. The problem is I cannot start from the page I want. The sample code only starts scrolling with Page One. Code itself seems 'really' simple but somehow I just cannot make it start from the page I choose.
Here is main part of the sample code that I found on the Internet. You can of course download the project the link above.
- (void)viewDidLoad {
NSLog(@"ScrollViewWithPagingViewContrller :: viewDidLoad");
[super viewDidLoad];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// 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) return;
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];
//
[controller release];
}
// 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];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经实现了相册视图,在第一个视图中我显示了所有缩略图,单击每个图像时它会提供全屏视图。您还可以浏览图像。
查看此答案。
一切顺利。
I have implemented photo album view where in first view i have shown all the thumbnail images and on clicking each image it gives its full screen view. you also scroo the images.
See this answer.
All the best.
答案是
scrollView1.contentOffset = CGPointMake( , );
很简单。花了很多时间。
The answer is
scrollView1.contentOffset = CGPointMake( , );
quite simple. spent lot of time.