淺析iPhone SDK開發(fā)基礎之UIPageControl編程
iPhone SDK開發(fā)基礎之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,在編譯時編譯器會產(chǎn)生警告,這里不用管它,只需要SwipeView類的使用者設置host成員變量并實現(xiàn)swipeTo()方法即可。
SwipeView類的使用者為PageViewController類,該類實現(xiàn)程序的主界面,在這個自定義的UIViewController類中實現(xiàn)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];
- }
在該回調方法中根據(jù)用戶滾動的方向來設置UIPageControl的currentPage屬性,如果是向右方滾動則頁面計數(shù)加一,如果用戶滾動的方向是向左,則頁面計數(shù)減一。設置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()方法實現(xiàn)中,PageViewController類通過UIView的exchangeSubview AtIndex()方法實現(xiàn)頁面內容的切換。
小結:淺析iPhone SDK開發(fā)基礎之UIPageControl編程的內容介紹完了,希望通過本文的學習能對你有所幫助!