现在的位置: 首页 > 综合 > 正文

UIScrollView、UIPageControl

2013年10月08日 ⁄ 综合 ⁄ 共 1246字 ⁄ 字号 评论关闭
 //创建滚动视图
    scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    scroll.pagingEnabled=YES;//设置翻页效果
    scroll.showsHorizontalScrollIndicator=NO;//不显示水平滚动条
    scroll.delegate=self;
    scroll.contentSize=CGSizeMake(320*NUM, 460);//内容大小
    for (int i=0; i<NUM; i++) {
        UIImageView * imgView=[[UIImageView alloc] initWithFrame:CGRectMake(i*320, 0, 320, 460)];
        UIImage * img=[UIImage imageNamed:@"IMG_0012.JPG"];
        imgView.image=img;
        [scroll addSubview:imgView];
        [imgView release];
    }
    [self.view addSubview:scroll];
    [scroll release];




---------------------------------------------------------


    //创建分页视图
    page=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 350, 320, 60)];
    page.numberOfPages=NUM;//页码数
    page.pageIndicatorTintColor=[UIColorblackColor];//非当前页的点颜色
    page.currentPageIndicatorTintColor=[UIColorblueColor];//当前页的点颜色
    [pageaddTarget:selfaction:@selector(pageEventAction:) forControlEvents:UIControlEventValueChanged];//添加事件处理
    [self.view addSubview:page];
    [page release];





-----------------------------------------------------------




//滚动视图跟分页控制器关联
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint p=scrollView.contentOffset;//偏移量
    int n=p.x/320;
    page.currentPage=n;//当前页码
}
//点击分页视图触发事件
-(void) pageEventAction:(UIPageControl *) sender
{
    scroll.contentOffset=CGPointMake(sender.currentPage*320, 0);
}

抱歉!评论已关闭.