实现 scrollview 默认显示指定的页码
生活随笔
收集整理的這篇文章主要介紹了
实现 scrollview 默认显示指定的页码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路:用scrollview的偏移量來實現
以下代碼運行成功
#import <UIKit/UIKit.h>@interface MainViewController : UIViewController<UIScrollViewDelegate> {//上面的scrollviewUIScrollView *scrollView0;UIPageControl *pageControl0; //頁面控制控件 tag 已在xib文件中設置為0NSMutableArray *arrImageViews; //相當于datasourceBOOL isLoadScrollView0; //是否加載 BOOL pageControlUsed;} @property (nonatomic,retain) IBOutlet UIScrollView *scrollView0; @property (nonatomic,retain) IBOutlet UIPageControl *pageControl0; @property BOOL isLoadScrollView0; - (IBAction)changePage:(id)sender;@end #import "MainViewController.h"static NSUInteger fNumberOfPages = 4;@interface MainViewController (PrivateMethods) - (void)loadScrollViewWithPage:(int)page; - (void)scrollViewDidScroll:(UIScrollView *)sender; @end@implementation MainViewController @synthesize scrollView0, pageControl0; @synthesize isLoadScrollView0;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization }return self; }- (void)viewDidLoad {[super viewDidLoad];/* 初始化scrollView0相關內容*///獲取scrollView0要顯示的相關內容 arrImageViews = [[NSMutableArray alloc]init];for (int i = 0; i < fNumberOfPages; i++) {UIImage *tempImage = [[UIImage alloc]init];NSString *imageName = [NSString stringWithFormat:@"pic0%d.png", i + 1];tempImage = [UIImage imageNamed:imageName];UIImageView *view = [[UIImageView alloc] initWithImage:tempImage];[arrImageViews addObject:view];}// scrollView0 初始化scrollView0.pagingEnabled = YES;scrollView0.contentSize = CGSizeMake(scrollView0.frame.size.width * fNumberOfPages, scrollView0.frame.size.height);scrollView0.showsHorizontalScrollIndicator = NO;scrollView0.showsVerticalScrollIndicator = NO;scrollView0.scrollsToTop = NO;scrollView0.delegate = self;scrollView0.tag = 1000;pageControl0.numberOfPages = fNumberOfPages;pageControl0.currentPage = 0; //這個只改變了pagecontrol 被選中的位置 isLoadScrollView0 = YES;//使用如下3句代碼 實現默認顯示 scrollview 指定的頁CGPoint pt = CGPointMake(640, 0); [scrollView0 setContentOffset:pt]; //設置scrollview 的偏移量[self scrollViewDidScroll:scrollView0]; //模擬scrollview 被滑動 }- (void)viewDidUnload {[super viewDidUnload];// Release any retained subviews of the main view.// e.g. self.myOutlet = nil; }- (IBAction)changePage:(id)sender {if ([sender tag] == 0) {int page = pageControl0.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 pageCGRect frame = scrollView0.frame;frame.origin.x = frame.size.width * page;frame.origin.y = 0;[scrollView0 scrollRectToVisible:frame animated:YES];// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.pageControlUsed = YES;} }- (void)loadScrollViewWithPage:(int)page {if (isLoadScrollView0 == YES) {if (page < 0)return;if (page >= fNumberOfPages)return;// 獲取數據UIImageView *view = [arrImageViews objectAtIndex:page];CGRect frame = scrollView0.frame;frame.origin.x = frame.size.width * page;frame.origin.y = 0;view.frame = frame;[scrollView0 addSubview:view];} }- (void)scrollViewDidScroll:(UIScrollView *)sender {if ([sender tag] == 1000) {//設置加載的對象isLoadScrollView0 = YES;if (pageControlUsed) {return;}// Switch the indicator when more than 50% of the previous/next page is visibleCGFloat pageWidth = scrollView0.frame.size.width;NSLog(@"scrollView0.contentOffset.x === %f",scrollView0.contentOffset.x );int page = floor((scrollView0.contentOffset.x - pageWidth / 2) / pageWidth) + 1;pageControl0.currentPage = page;[self loadScrollViewWithPage:page - 1];[self loadScrollViewWithPage:page];[self loadScrollViewWithPage:page + 1];} }// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {if (scrollView.tag == 1000) {pageControlUsed = NO;} }// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {if (scrollView.tag == 1000) {pageControlUsed = NO;} }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // return (interfaceOrientation == UIInterfaceOrientationPortrait);return NO; } @end?
轉載于:https://www.cnblogs.com/ygm900/archive/2013/05/22/3092309.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的实现 scrollview 默认显示指定的页码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CALayer 一些简单的小例子
- 下一篇: 传雅虎有意收购Hulu 竞争者达6家