UI_ScrollView復(fù)習(xí)

其中最主要的部分

//宏定義了一下#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);

}];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容