iPhone SDK开发基础之UIPageControl编程
当用户界面需要按页面进行显示时,使用iOS提供的UIPageControl控件将要显示的用户界面内容分页进行显示会使编程工作变得非常快捷,如图3-47所示就是一个使用UIPageControl控件逐页进行图片显示的程序,用户按下屏幕即可进行左右滚动显示,在屏幕的正上方使用白色的点显示当前滚动到的页面位置。
▲图3-47 UIPageControl编程实例界面
程序自定义一个SwipeView类,该类通过子类化UIView类并重载其touchesMoved()方法捕获用户滚动的方向,类的定义如下。
#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()方法,代码如下。
- (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()方法交换页面显示内容,并将图片显示出来,代码如下。
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工程。