类似新闻客户端.UIPageControl和UIScroll的结合使用,滑点控制图片页码.显示图片页码

//设置协议.
@interface RootViewController : UIViewController<UIScrollViewDelegate>
//声明两个公用属性,也可以使用延展
@property (nonatomic,retain)UIScrollView * aScrollView;
@property (nonatomic,retain)UIPageControl * aPageControl;
@end
//以下为.m文件
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor clearColor];
       self.aPageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(80, 320, 150, 30)];
        _aPageControl.numberOfPages=5;
        _aPageControl.currentPage=0;
        _aPageControl.backgroundColor=[UIColor clearColor];
        _aPageControl.currentPageIndicatorTintColor=[UIColor redColor];
        _aPageControl.pageIndicatorTintColor=[UIColor blueColor];
       [self.view addSubview:_aPageControl];
       [_aPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    //添加scroll
    self.aScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 240)];
    _aScrollView.backgroundColor=[UIColor greenColor];
    //_aScrollView.bounces=YES;
    _aScrollView.userInteractionEnabled=YES;
    _aScrollView.pagingEnabled=YES;
    _aScrollView.scrollEnabled=YES;
    _aScrollView.contentSize=CGSizeMake(320*5, 240);
    _aScrollView.bounces=NO;
    [self.view addSubview:_aScrollView];
    
    NSMutableArray *array=[[NSMutableArray alloc] init];
    for (int i=0; i<5; i++) {
        [array addObject:[NSString stringWithFormat:@"test-%d(被拖移).tiff",i+1]];
    }
    for (int i=0; i<5; i++) {
        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:array[i]]];
        imageView.frame=CGRectMake(320*i, 0, 320, 240);
        [_aScrollView addSubview:imageView];
    }
    _aScrollView.delegate=self;
    
    
}
- (void)changePage:(UIPageControl *)aPage

{
    _aScrollView.contentOffset=CGPointMake(_aPageControl.currentPage*320, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  _aPageControl.currentPage=_aScrollView.contentOffset.x/320;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


posted @ 2013-12-14 21:29  徐坤很无聊  阅读(175)  评论(0编辑  收藏  举报