Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1487175
  • 博文数量: 465
  • 博客积分: 8915
  • 博客等级: 中将
  • 技术积分: 6365
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-30 15:05
文章分类

全部博文(465)

文章存档

2017年(33)

2016年(2)

2015年(4)

2014年(29)

2013年(71)

2012年(148)

2011年(178)

分类: IT业界

2011-05-30 18:18:13

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

 

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

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

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

@end


//  SwipeView.m
#import 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];

在捕获用户滚动的方向后,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企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

购买地址:

互动出版网:

阅读(1912) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~