iOS圖片瀏覽器(UIScrollView + UIPageControl)

UIScrollView


- (void)setUpScrollView {
    CGFloat width = self.scrollView.bounds.size.width;
    CGFloat height = self.scrollView.bounds.size.height;
    for (NSInteger i = 0; i < [self.imagArr count]; i++) {
        UIImage *image = [UIImage imageNamed:self.imagArr[i]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        [imageView setFrame:CGRectMake(i * width, 0, width, height)];
        
        [self.scrollView addSubview:imageView];
    }
    
} 
 

將圖片添加到UIScrollView!

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.imagArr = @[@"1.jpg", @"2.jpg", @"3.jpg"];
    [self setUpScrollView];
    //設(shè)置總體的寬度
    [self.scrollView setContentSize:CGSizeMake([self.imagArr count] * self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
    [self.scrollView setPagingEnabled:YES];
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.pagecontoll = [[UIPageControl alloc] init];
    self.pagecontoll.backgroundColor = [UIColor clearColor];
    self.pagecontoll.bounds = CGRectMake(0, 0, 200, 100);
    self.pagecontoll.center = CGPointMake(self.view.bounds.size.width / 2, self.scrollView.bounds.size.height);
    self.pagecontoll.numberOfPages = [self.imagArr count];
    self.pagecontoll.currentPage = 0;
    self.scrollView.delegate = self;
    [self.pagecontoll addTarget:self action:@selector(switchPage:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.pagecontoll];

}

通過UIPageControl改變UIScrollView

- (void)switchPage:(id)sender {
    UIPageControl *currentControl = (UIPageControl *)sender;
    NSInteger currentPage = currentControl.currentPage;
    _scrollView.contentOffset = CGPointMake(currentPage * self.view.bounds.size.width, 0);
}

通過UIScrollView改變UIPageControl


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentPage = scrollView.contentOffset.x / self.view.bounds.size.width;
    self.pagecontoll.currentPage = currentPage;
}

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

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

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