自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析iPhone SDK開發(fā)基礎之UIPageControl編程

移動開發(fā) iOS
iPhone SDK開發(fā)基礎之UIPageControl編程是本文要介紹的內容,當用戶界面需要按頁面進行顯示時,使用iOS提供的UIPageControl控件將要顯示的用戶界面內容分頁進行顯示會使編程工作變得非??旖?,

iPhone SDK開發(fā)基礎之UIPageControl編程是本文要介紹的內容,當用戶界面需要按頁面進行顯示時,使用iOS提供的UIPageControl控件將要顯示的用戶界面內容分頁進行顯示會使編程工作變得非常快捷,如圖3-47所示就是一個使用UIPageControl控件逐頁進行圖片顯示的程序,用戶按下屏幕即可進行左右滾動顯示,在屏幕的正上方使用白色的點顯示當前滾動到的頁面位置,如圖:

iPhone SDK開發(fā)基礎之UIPageControl編程

程序自定義一個SwipeView類,該類通過子類化UIView類并重載其touchesMoved()方法捕獲用戶滾動的方向,類的定義如下。

  1. //  SwipeView.h  
  2. #import <UIKit/UIKit.h> 
  3. #import <QuartzCore/QuartzCore.h> 
  4.  
  5. @interface SwipeView : UIView {  
  6.  CGPoint startTouchPosition;  
  7.  NSString *dirString;  
  8.  UIViewController *host;  
  9. }  
  10.  
  11. - (void) setHost: (UIViewController *) aHost;  
  12.  
  13. @end  
  14.  
  15.  
  16. //  SwipeView.m  
  17. #import "SwipeView.h"  
  18.  
  19. @implementation  
  20.  
  21. SwipeView  
  22.  
  23. - (id)initWithFrame:(CGRect)frame {  
  24.     if ((self = [super initWithFrame:frame])) {  
  25.         // Initialization code  
  26.     }  
  27.     return self;  
  28. }  
  29.  
  30.  
  31. - (void) setHost: (UIViewController *) aHost  
  32. {  
  33.  host = aHost;  
  34. }  
  35.  
  36. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {   
  37.  UITouch *touch = [touches anyObject];   
  38.  startTouchPosition = [touch locationInView:self];   
  39.  dirString = NULL;  
  40. }   
  41.  
  42. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {   
  43.  UITouch *touch = touches.anyObject;   
  44.  CGPoint currentTouchPosition = [touch locationInView:self];   
  45.    
  46. #define HORIZ_SWIPE_DRAG_MIN 12   
  47. #define VERT_SWIPE_DRAG_MAX 4   
  48.    
  49.  if (fabsf(startTouchPosition.x - currentTouchPosition.x) >=   
  50.   HORIZ_SWIPE_DRAG_MIN &&   
  51.   fabsf(startTouchPosition.y - currentTouchPosition.y) <=   
  52.   VERT_SWIPE_DRAG_MAX)  {   
  53.   // Horizontal Swipe  
  54.   if (startTouchPosition.x < currentTouchPosition.x) {  
  55.    dirString = kCATransitionFromLeft;  
  56.   }  
  57.   else   
  58.    dirString = kCATransitionFromRight;  
  59.  }  
  60. }   
  61.  
  62. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
  63.  if (dirString) [host swipeTo:dirString];  
  64. }  
  65.  
  66. @end 

在捕獲用戶滾動的方向后,SwipeView類通過用戶設置的host成員變量回調其swipeTo()方法,host成員變量在類中定義為UIViewController,在編譯時編譯器會產(chǎn)生警告,這里不用管它,只需要SwipeView類的使用者設置host成員變量并實現(xiàn)swipeTo()方法即可。

SwipeView類的使用者為PageViewController類,該類實現(xiàn)程序的主界面,在這個自定義的UIViewController類中實現(xiàn)swipeTo()方法,代碼如下。

  1. //  PageViewController.m  
  2. - (void) swipeTo: (NSString *) aDirection{  
  3.  UIPageControl *pageControl = [[[contentView superview] subviews] lastObject];  
  4.    
  5.  if ([aDirection isEqualToString:kCATransitionFromRight])  
  6.  {  
  7.   if (currentPage == 5) return;  
  8.   [pageControl setCurrentPage:currentPage + 1];  
  9.  } else {  
  10.   if (currentPage == 0) return;  
  11.   [pageControl setCurrentPage:currentPage - 1];  
  12.  }  
  13.    
  14.  [self pageTurn:pageControl];  

在該回調方法中根據(jù)用戶滾動的方向來設置UIPageControl的currentPage屬性,如果是向右方滾動則頁面計數(shù)加一,如果用戶滾動的方向是向左,則頁面計數(shù)減一。設置UIPageControl的currentPage屬性以后,PageViewController對象再調用其pageTurn()方法交換頁面顯示內容,并將圖片顯示出來,代碼如下。

  1. - (void) pageTurn: (UIPageControl *) pageControl{  
  2.  CATransition *transition;  
  3.  int secondPage = [pageControl currentPage];  
  4.  if ((secondPage - currentPage) > 0)  
  5.   transition = [self getAnimation:@"fromRight"];  
  6.  else  
  7.   transition = [self getAnimation:@"fromLeft"];  
  8.    
  9.  UIImageView *newView = (UIImageView *)[[contentView subviews] objectAtIndex:0];  
  10.  [newView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ipad_ wallpaper%02d.jpg", secondPage + 1]]];  
  11.  [contentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  12.  [[contentView layer] addAnimation:transition forKey:@"transitionView Animation"];  
  13.    
  14.  currentPage = [pageControl currentPage];  

在主pageTurn()方法實現(xiàn)中,PageViewController類通過UIView的exchangeSubview AtIndex()方法實現(xiàn)頁面內容的切換。

小結:淺析iPhone SDK開發(fā)基礎之UIPageControl編程的內容介紹完了,希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 博客園
相關推薦

2011-08-18 10:02:47

iPhone SDKOpenFlow

2011-07-18 09:35:29

iPhone 框架

2011-08-18 09:44:33

iPhone SDK儀表控件UIDialView

2011-07-18 14:39:53

iPhone SDK UIKit

2011-08-12 18:18:03

iPhone開發(fā)UIPageContr按鈕

2011-07-06 17:53:40

iPhone SDK Xcode

2011-07-06 17:40:43

iPhone SDK

2011-08-02 13:46:43

iPhone開發(fā) iPhone SDK

2011-07-22 18:25:20

XCode iPhone SDK

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2011-08-10 10:10:21

iPhoneUIPopoverCo

2010-12-10 13:57:45

PHP Extensi

2009-08-06 09:18:01

ASP.NET自定義控ASP.NET控件開發(fā)

2011-05-31 14:03:13

2009-07-02 10:51:21

腳本編程JSP開發(fā)

2011-08-08 13:57:19

iPhone開發(fā) 打包 DEB

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-12 13:19:24

iPhoneSDK安裝

2011-07-05 17:19:47

元編程

2009-08-21 17:19:36

C#網(wǎng)絡編程入門
點贊
收藏

51CTO技術棧公眾號