zoukankan      html  css  js  c++  java
  • IOS scrollView以及pageControl使用

    本文转自 http://www.999dh.net/article/iphone_ios_art/45.html  转载请注明,谢谢!

    #import <UIKit/UIKit.h>

    @interface CRViewController : UIViewController
    {
        UIScrollView * scrollView;
        UIPageControl * pageControl;
    }

    @property(nonatomic,retain) UIScrollView * scrollView;
    @property(nonatomic,retain) UIPageControl * pageControl;

    @end



    //
    //  CRViewController.m
    //  pageControl
    //
    //  Created by chaoxiao zhuang on 12-12-22.
    //  Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
    //

    #import "CRViewController.h"

    @implementation CRViewController

    @synthesize scrollView;
    @synthesize pageControl;

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    -(void)pageChanged:(UIPageControl*)page
    {
        int p = page.currentPage;
        [scrollView scrollRectToVisible:CGRectMake(320*p, 0, 320, 480) animated:YES];
    }

    -(void)scrollViewDidScroll:(UIScrollView*)scrollView
    {
        CGFloat pageWith = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
        self.pageControl.currentPage = page;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        scrollView.delegate = self;
        [scrollView setContentSize:CGSizeMake(320*4, 480)];
        //scrollView.showsHorizontalScrollIndicator = YES;
        scrollView.showsVerticalScrollIndicator = YES;
        scrollView.pagingEnabled = YES;
        [scrollView setBackgroundColor:[UIColor redColor]];
        
        UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        [view1 setBackgroundColor:[UIColor grayColor]];
        [scrollView addSubview:view1];
        
        UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(320, 0, 320, 480)];
        [view2 setBackgroundColor:[UIColor blueColor]];
        [scrollView addSubview:view2];
        
        UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(320*2, 0, 320, 480)];
        [view3 setBackgroundColor:[UIColor greenColor]];
        [scrollView addSubview:view3];
        
        UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(320*3, 0, 320, 480)];
        [view4 setBackgroundColor:[UIColor yellowColor]];
        [scrollView addSubview:view4];
        
        [self.view addSubview:scrollView];
        
        
        pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
        pageControl.center = CGPointMake(160, 40);
        pageControl.numberOfPages = 4;
      
        [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:pageControl];
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

  • 相关阅读:
    windows 创建指定大小文件
    python pip命令 报错‘unknow or unsupported command install’
    PyCharm:ModuleNotFoundError: No module named 'selenium'
    毕设图片链接
    本地localhost:端口号(自己设置的Apache的端口号)打不开问题解决了!开心、哭泣
    python之freshman00
    Python之freshman07 面向对象编程jinjie
    Python之freshman08 Socket
    Python之freshman04
    Python之freshman05
  • 原文地址:https://www.cnblogs.com/rollrock/p/2830295.html
Copyright © 2011-2022 走看看