zoukankan      html  css  js  c++  java
  • UIScrollView和UIPageController

    实现代码:

    //
    //  ViewController.m
    //  UIpageControl
    //
    //  Created by dllo on 16/3/10.
    //  Copyright © 2016年 dllo. All rights reserved.
    //
    
    #import "ViewController.h"
    #define WIDTH self.view.frame.size.width
    #define HEIGHT self.view.frame.size.height
    
    @interface ViewController ()<UIScrollViewDelegate>
    
    @property (nonatomic, retain)UIScrollView *scrollView;
    @property (nonatomic, retain)UIPageControl *pageControl;
    @property (nonatomic, retain)UILabel *titleLabel;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor yellowColor];
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 550, 200, 30)];
        self.pageControl.backgroundColor = [UIColor redColor];
        [self.view addSubview:self.pageControl];
        [self.pageControl release];
        self.pageControl.numberOfPages = 7;
        self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
        self.pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
        [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,WIDTH, 500)];
        [self.view addSubview:self.scrollView];
        [self.scrollView release];
        self.scrollView.contentSize = CGSizeMake(WIDTH * 9, 500);
        
        for (NSInteger i = 0; i < 7; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i + WIDTH , 0, WIDTH, HEIGHT)];
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i]];
            [self.scrollView addSubview:imageView];
        }
        UIImageView *firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        firstImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h6.jpeg"]];
        [self.scrollView addSubview:firstImageView];
        [firstImageView release];
        
        UIImageView *lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];
        lastImageView.image = [UIImage imageNamed:@"h0.jpeg"];
        [self.scrollView addSubview:lastImageView];
        [lastImageView release];
        
        //创建label
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(130, 600, 150, 50)];
        [self.view addSubview:self.titleLabel];
        [_titleLabel release];
        self.titleLabel.backgroundColor = [UIColor grayColor];
        [self.titleLabel setTextColor:[UIColor whiteColor]];
        self.titleLabel.textAlignment = 1;
        self.titleLabel.text = @"1 / 7";
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
    }
    - (void)pageAction:(UIPageControl *)page {
        NSLog(@"%ld", self.pageControl.currentPage);
    //    self.scrollView.contentOffset = CGPointMake(WIDTH * page.currentPage + WIDTH, 0);
        [self.scrollView setContentOffset:CGPointMake(WIDTH *page.currentPage + WIDTH, 0) animated:YES];
        [self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(page.currentPage + 1)]];
    }
    //滑动结束
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(WIDTH * 7, 0);
        }else if(scrollView.contentOffset.x == 8 * WIDTH){
            scrollView.contentOffset = CGPointMake(WIDTH , 0);
        }
        
         self.pageControl.currentPage = scrollView.contentOffset.x / WIDTH - 1;
        [self.titleLabel setText:[NSString stringWithFormat:@"%ld / 7", (NSInteger)(self.pageControl.currentPage + 1)]];
    }
    - (void)dealloc{
        [_scrollView release];
        [_pageControl release];
        [super dealloc];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    结果填空:青蛙爬井
    天上的星星 (前缀和)
    Poj3253 Fence Repair (优先队列)
    Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)
    数字图像处理之几种滤波器
    CodeForces
    直方图部分
    Codeforces Round #431 (Div. 2)
    2017中国大学生程序设计竞赛
    C++中数字与字符串之间的转换(转载自http://www.cnblogs.com/luxiaoxun/)
  • 原文地址:https://www.cnblogs.com/mafeng/p/5289138.html
Copyright © 2011-2022 走看看