iPhone SDK开发基础之UIPageControl编程

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

 

程序自定义一个SwipeView类,该类通过子类化UIView类并重载其touchesMoved()方法捕获用户滚动的方向,类的定义如下。
//  SwipeView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface SwipeView : UIView {
 CGPoint startTouchPosition;
 NSString *dirString;
 UIViewController *host;
}

- (void) setHost: (UIViewController *) aHost;

@end


//  SwipeView.m
#import "SwipeView.h"

@implementation SwipeView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}


- (void) setHost: (UIViewController *) aHost
{
 host = aHost;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [touches anyObject];
 startTouchPosition = [touch locationInView:self];
 dirString = NULL;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = touches.anyObject;
 CGPoint currentTouchPosition = [touch locationInView:self];
 
#define HORIZ_SWIPE_DRAG_MIN 12
#define VERT_SWIPE_DRAG_MAX 4
 
 if (fabsf(startTouchPosition.x - currentTouchPosition.x) >=
  HORIZ_SWIPE_DRAG_MIN &&
  fabsf(startTouchPosition.y - currentTouchPosition.y) <=
  VERT_SWIPE_DRAG_MAX)  {
  // Horizontal Swipe
  if (startTouchPosition.x < currentTouchPosition.x) {
   dirString = kCATransitionFromLeft;
  }
  else
   dirString = kCATransitionFromRight;
 }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 if (dirString) [host swipeTo:dirString];
}

@end
在捕获用户滚动的方向后,SwipeView类通过用户设置的host成员变量回调其swipeTo()方法,host成员变量在类中定义为UIViewController,在编译时编译器会产生警告,这里不用管它,只需要SwipeView类的使用者设置host成员变量并实现swipeTo()方法即可。
SwipeView类的使用者为PageViewController类,该类实现程序的主界面,在这个自定义的UIViewController类中实现swipeTo()方法,代码如下。
//  PageViewController.m
- (void) swipeTo: (NSString *) aDirection{
 UIPageControl *pageControl = [[[contentView superview] subviews] lastObject];
 
 if ([aDirection isEqualToString:kCATransitionFromRight])
 {
  if (currentPage == 5) return;
  [pageControl setCurrentPage:currentPage + 1];
 } else {
  if (currentPage == 0) return;
  [pageControl setCurrentPage:currentPage - 1];
 }
 
 [self pageTurn:pageControl];
}
在该回调方法中根据用户滚动的方向来设置UIPageControl的currentPage属性,如果是向右方滚动则页面计数加一,如果用户滚动的方向是向左,则页面计数减一。设置UIPageControl的currentPage属性以后,PageViewController对象再调用其pageTurn()方法交换页面显示内容,并将图片显示出来,代码如下。
- (void) pageTurn: (UIPageControl *) pageControl{
 CATransition *transition;
 int secondPage = [pageControl currentPage];
 if ((secondPage - currentPage) > 0)
  transition = [self getAnimation:@"fromRight"];
 else
  transition = [self getAnimation:@"fromLeft"];
 
 UIImageView *newView = (UIImageView *)[[contentView subviews] objectAtIndex:0];
 [newView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ipad_ wallpaper%02d.jpg", secondPage + 1]]];
 [contentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
 [[contentView layer] addAnimation:transition forKey:@"transitionView Animation"];
 
 currentPage = [pageControl currentPage];
}
在主pageTurn()方法实现中,PageViewController类通过UIView的exchangeSubview AtIndex()方法实现页面内容的切换。
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的PageControl工程。

 

本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》Q一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

 

购买地址:

当当网:
http://product.dangdang.com/product.aspx?product_id=21082051

卓越网:
http://www.amazon.cn/iOS%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91%E6%8F%AD%E5%AF%86-iPhone-iPad%E4%BC%81%E4%B8%9A%E5%BA%94%E7%94%A8%E5%92%8C%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91-%E8%99%9E%E6%96%8C/dp/B0051HAIA4/ref=sr_1_1?s=books&ie=UTF8&qid=1306139777&sr=1-1

 

posted @ 2011-05-30 16:02  我的javaIT  阅读(360)  评论(0编辑  收藏  举报