ios菜鸟之路:uiscrollview和pagecontrol的使用方法

一、UIScrollview以及pagecontrol的声明

@interface rootViewController : UIViewController//加上scrollviewdelegate的协议
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;

- (IBAction)pageTurn:(UIPageControl *)sender;  //pagecontrol的界面转换方法

二、实现

 scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 380)];//初始化scrollview的界面 (坐标x,坐标y,宽度,高度)屏幕左上角为原点
    [scrollView setContentSize:CGSizeMake(960, 380)];//设置scrollview画布的大小,此设置为三页的宽度,380的高度。用来实现三页照片的转换。
    scrollView.pagingEnabled=YES;//立刻翻页到下一页 没中间的拖动过程
    scrollView.bounces=NO;//去掉翻页中的白屏
    [scrollView setDelegate:self];
    scrollView.showsHorizontalScrollIndicator=NO;//不现实水平滚动条
    
    //将图像添加到scrollview中
    UIImageView *imageview1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 380)];
    [imageview1 setImage:[UIImage imageNamed:@"Default1.png"]];
    UIImageView *imageview2=[[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 320, 380)];
     [imageview2 setImage:[UIImage imageNamed:@"Default2.png"]];
     UIImageView *imageview3=[[UIImageView alloc]initWithFrame:CGRectMake(640, 0, 320, 380)];
     [imageview3 setImage:[UIImage imageNamed:@"Default3.png"]];
    [scrollView addSubview:imageview1];
    [scrollView addSubview:imageview2];
    [scrollView addSubview: imageview3];
    [self.view addSubview:scrollView];
        //将pagecontrol添加到scrollview中
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 350, 320, 30)];
    pageControl.numberOfPages=3;
    pageControl.currentPage=0;
    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:pageControl];


//pagecontrol的点跟着页数改变

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1{
    CGPoint offset=scrollView1.contentOffset;
    CGRect bounds=scrollView1.frame;
    [pageControl setCurrentPage:offset.x/bounds.size.width];
}

//点击pagecontrol的点 跳到那一页的实现

- (IBAction)pageTurn:(UIPageControl *)sender {
    CGSize viewsize=scrollView.frame.size;
    CGRect rect=CGRectMake(sender.currentPage*viewsize.width, 0, viewsize.width, viewsize.height);
    [scrollView scrollRectToVisible:rect animated:YES];
    
}

以上内容只是多张图片的转换功能。scrollview是将一屏放不了的东西组织在一起的功能,在相册和地图中比较常见。

详细contentoffset及contentsize contentinset的介绍 请见http://blog.csdn.net/bl1988530/article/details/7043936

你可能感兴趣的:(iphone学习)