其中最主要的部分
//宏定義了一下#define VIEW_WIDTH? ? self.view.frame.size.width#define VIEW_HEIGHT? ? self.view.frame.size.height//簽訂一下協(xié)議@interface RootViewController ()//設(shè)置了一個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循環(huán)講圖片放進(jìn)去
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];
}
//建立一個小圓點(diǎn)
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);
//設(shè)置一個代理 想要使圖片動的時候小圓點(diǎn)也動
self.sc.delegate = self;
}
//在簽訂協(xié)議以后,通過tag值來使圖片動的時候,小圓點(diǎn)也懂
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pc = [self.view viewWithTag:1000];
pc.currentPage = self.sc.contentOffset.x / VIEW_WIDTH;
}
//點(diǎn)擊小圓點(diǎn)的時候使圖片也跟著動,
- (void)click: (UIPageControl *)page
{
//里面方法的含義就是小圓點(diǎn)挪動的位置 = 圖片動了多少的位置
[UIView animateWithDuration:1 animations:^{
self.sc.contentOffset = CGPointMake(VIEW_WIDTH *page.currentPage, 0);
}];
}