生活随笔
收集整理的這篇文章主要介紹了
IOS初级:UIScrollView UIPageControl
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
UIScrollView其實(shí)構(gòu)建的就像一列很長的火車,每滑動一個屏幕,展示一節(jié)車廂。
//主屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
//主屏幕寬度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
CGFloat kImgCount = 10;CGFloat scrollY = 20; //scrollView距離屏幕頂部的距離
CGFloat pageCtrlWidth = 200;@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageCtrl;//構(gòu)建UIScrollView代碼
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollY, kScreenWidth, kScreenHeight - scrollY)];
self.scrollView.delegate = self;
for (int i = 0; i<kImgCount; i++) {UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, scrollY, kScreenWidth, kScreenHeight - scrollY)];imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"news%04d",i]];[self.scrollView addSubview:imgview];
}
self.scrollView.contentSize = CGSizeMake(kScreenWidth * kImgCount, kScreenHeight - scrollY);//設(shè)置這一列火車的總長度
self.scrollView.pagingEnabled = YES;//分頁顯示
[self.view addSubview:self.scrollView];//構(gòu)建UIPageControl代碼
self.pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - pageCtrlWidth)/2, kScreenHeight - scrollY, pageCtrlWidth, scrollY)];
self.pageCtrl.numberOfPages = kImgCount;
self.pageCtrl.pageIndicatorTintColor = [UIColor greenColor];
self.pageCtrl.currentPageIndicatorTintColor = [UIColor yellowColor];
[self.view insertSubview:self.pageCtrl aboveSubview:self.scrollView];
UIScrollView常用代理方法
//當(dāng)視圖滑動時
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{}
// 當(dāng)視圖將要拖動的時候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{}
//當(dāng)視圖停止拖拽的時候調(diào)用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{}
轉(zhuǎn)載于:https://www.cnblogs.com/ruanjianxian/p/6126872.html
總結(jié)
以上是生活随笔為你收集整理的IOS初级:UIScrollView UIPageControl的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。