• IOS 学习笔记,scrollView添加分页


     1 #import "ViewController.h"
     2  
     3 #define Width self.view.frame.size.width
     4 #define Height self.view.frame.size.height
     5 #define viewH 200
     6 @interface ViewController ()<UIScrollViewDelegate>
     7 
     8 
     9 @property(nonatomic,retain) UIScrollView * scrollView;
    10 @property(nonatomic,retain) UIPageControl * pageControl;
    11 
    12 @end
    13 
    14 @implementation ViewController
    15 
    16 
    17 
    18 #pragma mark - View lifecycle
    19 
    20 
    21 - (void)viewDidLoad
    22 {
    23  
    24     [super viewDidLoad];
    25     // Do any additional setup after loading the view, typically from a nib.
    26     
    27     self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, Height-viewH, Width, viewH)];
    28     self.scrollView.delegate = self;
    29     [self.scrollView setContentSize:CGSizeMake(Width*4, viewH)];
    30     //self.scrollView.showsHorizontalScrollIndicator = YES;
    31     self.scrollView.showsVerticalScrollIndicator = YES;
    32     self.scrollView.pagingEnabled = YES;
    33     [self.scrollView setBackgroundColor:[UIColor redColor]];
    34     
    35     NSArray*arr =[[NSArray alloc]initWithObjects:[UIColor grayColor],[UIColor greenColor],[UIColor blueColor],[UIColor yellowColor], nil];
    36     for (int i=0; i<4; i++) {
    37         UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(Width*i, 0, Width, viewH)];
    38         [view1 setBackgroundColor:arr[i]];
    39         [self.scrollView addSubview:view1];
    40 
    41     }
    42     [self.view addSubview:self.scrollView];
    43     
    44     
    45     self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
    46     self.pageControl.center = CGPointMake(Width/2, Height-20);
    47   
    48     self.pageControl.numberOfPages = 4;
    49     [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    50     
    51     [self.view addSubview:self.pageControl];
    52 }
    53 -(void)pageChanged:(UIPageControl*)page
    54 {
    55     int p = (int)page.currentPage;
    56     [self.scrollView scrollRectToVisible:CGRectMake(Width*p, 0, Width, viewH) animated:YES];
    57 }
    58 
    59 -(void)scrollViewDidScroll:(UIScrollView*)scrollView
    60 {
    61     CGFloat pageWith = scrollView.frame.size.width;
    62     int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
    63     self.pageControl.currentPage = page;
    64 }
    65 
    66 
    67 
    68 
    69 
    70 - (void)didReceiveMemoryWarning {
    71     [super didReceiveMemoryWarning];
    72     // Dispose of any resources that can be recreated.
    73 }
    74 
    75 @end
  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/mojiewei/p/5039491.html
Copyright © 2020-2023  润新知