本文摘自:http://tech.it168.com/a2011/0620/1206/000001206580_2.shtml

 代码下载 

 

 iPhone SDK开发基础之UIPageControl编程

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

iphone  pageController  的实现
▲图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

相关文章:

  • 2022-12-23
  • 2021-10-28
  • 2021-06-25
  • 2022-12-23
  • 2021-12-27
  • 2022-01-01
  • 2022-12-23
猜你喜欢
  • 2021-08-16
  • 2021-07-27
  • 2021-08-09
  • 2021-11-16
  • 2021-08-19
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案