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
    
  • 相关阅读:
    python之路day08--文件的操作
    python之路day07-集合set的增删查、列表如何排重(效率最高的方法)、深浅copy
    python之路day06-python2/3小区别,小数据池的概念,编码的进阶str转为bytes类型,编码和解码
    python之路day05--字典的增删改查,嵌套
    python之路day04--列表的增删改查,嵌套、元组的嵌套、range、for循环嵌套
    python之路day03--数据类型分析,转换,索引切片,str常用操作方法
    python之路day02--格式化输出、初始编码、运算符
    python之路day01--变量
    线程、进程、协程 异步io
    Ubuntu学习(转载)
  • 原文地址:https://www.cnblogs.com/0515offer/p/4638954.html
Copyright © 2011-2022 走看看