浅析iPhone SDK开发基础之UIPageControl编程

移动开发 iOS
iPhone SDK开发基础之UIPageControl编程是本文要介绍的内容,当用户界面需要按页面进行显示时,使用iOS提供的UIPageControl控件将要显示的用户界面内容分页进行显示会使编程工作变得非常快捷,

iPhone SDK开发基础之UIPageControl编程是本文要介绍的内容,当用户界面需要按页面进行显示时,使用iOS提供的UIPageControl控件将要显示的用户界面内容分页进行显示会使编程工作变得非常快捷,如图3-47所示就是一个使用UIPageControl控件逐页进行图片显示的程序,用户按下屏幕即可进行左右滚动显示,在屏幕的正上方使用白色的点显示当前滚动到的页面位置,如图:

iPhone SDK开发基础之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,在编译时编译器会产生警告,这里不用管它,只需要SwipeView类的使用者设置host成员变量并实现swipeTo()方法即可。

SwipeView类的使用者为PageViewController类,该类实现程序的主界面,在这个自定义的UIViewController类中实现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];  

在该回调方法中根据用户滚动的方向来设置UIPageControl的currentPage属性,如果是向右方滚动则页面计数加一,如果用户滚动的方向是向左,则页面计数减一。设置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()方法实现中,PageViewController类通过UIView的exchangeSubview AtIndex()方法实现页面内容的切换。

小结:浅析iPhone SDK开发基础之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开发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开发 iPhone SDK

2011-07-22 18:25:20

XCode iPhone SDK

2011-08-01 18:27:58

iPhone开发 UISearchBa

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-05-31 14:03:13

2010-12-10 13:57:45

PHP Extensi

2011-08-12 13:19:24

iPhoneSDK安装

2009-07-02 10:51:21

脚本编程JSP开发

2009-08-06 09:18:01

ASP.NET自定义控ASP.NET控件开发

2011-08-09 16:58:22

XCodeSDK

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-08 13:57:19

iPhone开发 打包 DEB

2011-09-14 10:03:46

Android SDK
点赞
收藏

51CTO技术栈公众号