//
// MainViewController.m
// UI06_UIScorollView
//
// Created by dllo on 15/8/5.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import MainViewController.h
//定義兩個宏,分別為屏幕尺寸的寬,高
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@property(nonatomic,retain)UIScrollView *scrollView;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];
//創建一個scrollView,和View尺寸一樣大
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:scrollView];
[scrollView release];
//重要的屬性,這個屬性可以讓ScrollView滾動起來
//contenSize設置scrollView的滾動范圍
scrollView.contentSize =CGSizeMake(15 * WIDTH, HEIGHT);
//按頁來進行滾動
scrollView.pagingEnabled=YES;
//
for (NSInteger i=0; i < 13; i++) {
//最常用的拼接
NSString *picName =[NSString stringWithFormat:@cymbal_%02ld.jpg,i];
//通過圖片名來創建UIImageView
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
//設置每一張圖片的起始位置
imageView.frame=CGRectMake(((i+1) * WIDTH),0, WIDTH, HEIGHT);
//把imageView圖片放到scrollView
[scrollView addSubview:imageView];
[imageView release];
}
//額外添加兩張圖,一張放在第一個,一個放在最後一個,對應的上面的整體范圍+2
UIImageView *last=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@cymbal_00.jpg]];
last.frame=CGRectMake(14 * WIDTH, 0, WIDTH, HEIGHT);
[scrollView addSubview:last];
[last release];
UIImageView *FIRST=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@cymbal_12.jpg]];
FIRST.frame=CGRectMake(0 * WIDTH, 0, WIDTH, HEIGHT);
[scrollView addSubview:FIRST];
[FIRST release];
//偏移量(把第幾張放到最前面)
scrollView.contentOffset=CGPointMake(WIDTH , 0);
//關掉邊界回彈效果,默認是YES
scrollView.bounces=NO;
//關閉滾動條,豎直,橫的 水平和豎直兩個滾動條會作為兩個子視圖添加到scrollView的子視圖中,如果把滾動條效果關閉,這兩個視圖就不會添加到子視圖中
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.showsVerticalScrollIndicator=NO;
//設置代理人
scrollView.delegate=self;
scrollView.tag=1000;
//創建一個計時器
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
#pragma mark 只要滾動就會觸發的協議方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(WIDTH * 14, 0))) {
scrollView.contentOffset=CGPointMake(WIDTH, 0);
}
if (CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, 0))) {
scrollView.contentOffset=CGPointMake(WIDTH * 13, 0);
}
}
#pragma mark 當scrollView減速時觸發停止
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@滾動停止);
}
-(void)changeImage
{
//通過tag找scrollView
UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:1000];
// scrollView.contentOffset=CGPointMake(scrollView.contentOffset.x + WIDTH, 0);
[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + WIDTH, 0) animated:YES];
if (scrollView.contentOffset.x ==WIDTH * 14) {
scrollView.contentOffset =CGPointMake(WIDTH, 0);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end