1. iphone/ipad大小
Device | Screen dimensions(in points) |
iphone and ipod | 320 X 480 |
ipad | 768 X 1024 |
2. UIScreen bounds and applicationFrame
[UISCreen mainScreen].bounds, 永遠返回portait模式的width/height, 也就是說width:320 height:480 for iPhone
[UISCreen mainScreen].applicationFrame更加復雜一些,它考慮了status bar的高度。
portait模式, x:0, y:20, width:320, height:460
landscape, x:20, y:0, width:300, height:480
關于frame和bounds的區別,請參考UIView中的frame和bounds屬性的區別
3. status bar height: 20, tool bar height: 44, tab bar height:44
4. UIScrollView
// Called when the UIKeyboardDidShowNotification is sent.- (void)keyboardWasShown:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it's visible // Your application might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { //判斷該text field是否在可見區域 CGPoint scrollPoint = CGPointMake(0.0, instance); //這里需要計算需要scroll的長度,假設instance是計算出來的結果。 [scrollView setContentOffset:scrollPoint animated:YES]; }}
原文來自這里, 這里介紹幾個關鍵的內容:
1. pagingMode需要設置為YES
2. contentSize是所有頁數的width總和, 例如你需要顯示100頁,那contentSize的寬度就是100*scroll.frame.size.width
3. showsHorizontalScrollIndicator和showsVerticalScrollIndicator設置為NO
然后需要在scroll view的delegate中實現scrollViewDidScroll:方法,這個方法在scroll view的contentOffSet發生變化的時候被調用。翻頁模式的核心思想就是scroll view需要顯示下一頁的時候,創建一個view, 在這個view上準備好數據,然后把這個view加到scroll view當前顯示的位置。
代碼如下所示,另外還可以改進的一個地方是需要翻頁之前,需要把后面一頁的內容也顯示出來,否則會有一半黑的屏幕顯示。
-(void) loadPageView:(NSUInteger)page{ UIView* view = [[UIView alloc] init]; view.backgroundColor = [UIColor whiteColor]; CGRect rc = self.view.frame; rc.origin.y = 0; rc.origin.x = page * self.view.frame.size.width; //創建一個view,調整x直,因為其他直和scroll view都是一樣的 view.frame = rc; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, rc.size.width, 40)]; label.text = [NSString stringWithFormat:@"current page is :%d", page]; [view addSubview:label]; [(UIScrollView*)self.view addSubview:view];}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView.contentOffset.x >= (myCurrentPage+0.5) * self.view.frame.size.width) //需要顯示下一頁了 { myCurrentPage++; [self loadPageView:myCurrentPage]; }}
新聞熱點
疑難解答