zoukankan      html  css  js  c++  java
  • UI2_ScrollView&UIPageControl

    //
    //  ViewController.h
    //  UI2_ScrollView&UIPageControl
    //
    //  Created by zhangxueming on 15/7/10.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIScrollViewDelegate>
    
    
    @end
    
    //
    //  ViewController.m
    //  UI2_ScrollView&UIPageControl
    //
    //  Created by zhangxueming on 15/7/10.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "ViewController.h"
    
    #define ClipsBundleWidth ((self.view.frame.size.width-300)/2)
    
    @interface ViewController ()
    {
        UIScrollView *_scrollView;
        UIPageControl *_pageControl;//页码
        NSInteger _currentIndex;//记录当前显示的页码
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ClipsBundleWidth, 64, 300, 450)];
        _scrollView.contentSize = CGSizeMake(300*6, 450);
        _scrollView.pagingEnabled = YES;
        
        
        for (int i=0;  i<6; i++) {
            NSString *imageName = [NSString stringWithFormat:@"%d",i];
            NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(300*i, 0, 300, 450)];
            imageView.image = [UIImage imageWithContentsOfFile:path];
            [_scrollView addSubview:imageView];
        }
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(ClipsBundleWidth, 64+450, 300, 30)];
        _pageControl.numberOfPages = 6;
        _currentIndex = 0;
        _pageControl.currentPage = _currentIndex;
        _pageControl.backgroundColor = [UIColor blackColor];
        
        [self.view addSubview:_pageControl];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        backBtn.frame = CGRectMake(50, 64+450+30, (self.view.frame.size.width-100)/2, 30);
        [backBtn setTitle:@"back" forState:UIControlStateNormal];
        backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
        [backBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backBtn];
        
        UIButton *forwardBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        forwardBtn.frame = CGRectMake(50+(self.view.frame.size.width-100)/2, 64+450+30, (self.view.frame.size.width-100)/2, 30);
        [forwardBtn setTitle:@"forward" forState:UIControlStateNormal];
        forwardBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
        [forwardBtn addTarget:self action:@selector(forwardBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:forwardBtn];
        
        //代理
        _scrollView.delegate = self;
        
        [self.view addSubview:_scrollView];
        
    }
    
    - (void)backBtnClicked
    {
        _currentIndex--;
        if (_currentIndex>=0) {
            CGPoint point = CGPointMake(_currentIndex*300   , _scrollView.contentOffset.y);
            [_scrollView setContentOffset:point animated:YES];
            _pageControl.currentPage = _currentIndex;
        }
        else
        {
            _currentIndex = 5;
            CGPoint point = CGPointMake(_currentIndex*300   , _scrollView.contentOffset.y);
            [_scrollView setContentOffset:point animated:YES];
            _pageControl.currentPage = _currentIndex;
        }
    }
    
    - (void)forwardBtnClicked
    {
        _currentIndex++;
        if (_currentIndex<=5) {
            CGPoint point = CGPointMake(_currentIndex*300, _scrollView.contentOffset.y);
            [_scrollView setContentOffset:point animated:YES];
            _pageControl.currentPage = _currentIndex;
        }
        else
        {
            _currentIndex = 5;
        }
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        _currentIndex = scrollView.contentOffset.x/300;
        _pageControl.currentPage = _currentIndex;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
  • 相关阅读:
    2019春总结
    2019春第一周编程总结
    第十二周
    2019春第七周编程总结
    2019春第四周作业
    人生路上对我影响最大的三位老师
    第五周课程总结&试验报告(三)
    第四周课程总结&试验报告(二)
    第三周课程总结&实验报告一
    第二周java学习总结
  • 原文地址:https://www.cnblogs.com/0515offer/p/4638954.html
Copyright © 2011-2022 走看看