美文网首页iOSiOS开发之实战资源
iOS图片浏览器(UIScrollView + UIPageCo

iOS图片浏览器(UIScrollView + UIPageCo

作者: MWY | 来源:发表于2015-07-27 08:00 被阅读1038次

    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];
        //设置总体的宽度
        [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;
    }
    
    

    相关文章

      网友评论

        本文标题:iOS图片浏览器(UIScrollView + UIPageCo

        本文链接:https://www.haomeiwen.com/subject/fpsmqttx.html