其中最主要的部分
//宏定义了一下#define VIEW_WIDTH self.view.frame.size.width#define VIEW_HEIGHT self.view.frame.size.height//签订一下协议@interface RootViewController ()//设置了一个UIScrollView的一个属性
@property (nonatomic, retain)UIScrollView *sc;
@end
@implementation RootViewController
-(void)dealloc
{
[self.sc release];
[super dealloc];
}
//在viewDidload中写属性
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//建立一个UIScrollView
self.sc = [[UIScrollView alloc]initWithFrame:self.view.frame];
//最基本的属性
self.sc.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.sc];
[self.sc release];
self.sc.contentSize = CGSizeMake(VIEW_WIDTH *6, VIEW_HEIGHT);
self.sc.pagingEnabled = YES;
//用for循环讲图片放进去
for (NSInteger i = 1; i < 7; i++) {
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(VIEW_WIDTH *(i - 1), 0, VIEW_WIDTH, VIEW_HEIGHT )];
NSString *name = [NSString stringWithFormat:@"S%ld", i];
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:@"jpg"];
imgview.image = [UIImage imageWithContentsOfFile:path];
[self.sc addSubview:imgview];
[imgview release];
}
//建立一个小圆点
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
page.backgroundColor = [UIColor blackColor];
[self.view addSubview:page];
[page release];
page.tag = 1000;
page.numberOfPages = 6;
[page addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
page.center = CGPointMake(self.view.center.x, VIEW_HEIGHT - 50);
//设置一个代理 想要使图片动的时候小圆点也动
self.sc.delegate = self;
}
//在签订协议以后,通过tag值来使图片动的时候,小圆点也懂
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pc = [self.view viewWithTag:1000];
pc.currentPage = self.sc.contentOffset.x / VIEW_WIDTH;
}
//点击小圆点的时候使图片也跟着动,
- (void)click: (UIPageControl *)page
{
//里面方法的含义就是小圆点挪动的位置 = 图片动了多少的位置
[UIView animateWithDuration:1 animations:^{
self.sc.contentOffset = CGPointMake(VIEW_WIDTH *page.currentPage, 0);
}];
}
网友评论