zoukankan      html  css  js  c++  java
  • UI基础 UIScrollView + PageControl

    root.m

    #import "RootViewController.h"
    //1.签订协议
    @interface RootViewController ()<UIScrollViewDelegate>
    {
        UIPageControl *PControl;
        UIScrollView* sv;
        
    }
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        sv=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
        
        sv.backgroundColor=[UIColor yellowColor];
        [self.view addSubview:sv];
        
        //设置大小 多大才能装下四张图片
        sv.contentSize=CGSizeMake(375*4, 667);
        //把图片装进去
        for(int i=1; i<=4; i++){
            NSString* name =[NSString stringWithFormat:@"%d.jpg",i];
            UIImage *image =[UIImage imageNamed:name];
            UIImageView* imageV=[[UIImageView alloc] initWithFrame:CGRectMake(375*(i-1), 0, 375, 667)];
            
            imageV.image=image;
            [sv addSubview:imageV];
            
            
            //分页显示
            sv.pagingEnabled=YES;
            //是否允许反弹
            sv.bounces=NO;
            // 修改滚动条样式
            sv.indicatorStyle=UIScrollViewIndicatorStyleWhite;
            //隐藏滚动条
            sv.showsHorizontalScrollIndicator=NO;
            sv.showsVerticalScrollIndicator=NO;
            //滑动到指定位置
            sv.contentOffset=CGPointMake(375, 0);
    //        2.构造方法
            sv.delegate=self;
    //         创建下边四个小点 (页面控制)
            PControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 600, 375, 50)];
            
            PControl.backgroundColor=[UIColor redColor];
            [self.view addSubview:PControl];
            // 几个点
            PControl.numberOfPages=4;
            //选中点点颜色
            PControl.currentPageIndicatorTintColor=[UIColor greenColor];
            
            //为选中点的颜色
            PControl.pageIndicatorTintColor=[UIColor yellowColor];
            
            //添加事件 UIControl 自带属性
            [PControl addTarget:self action:@selector(PageCon:) forControlEvents:UIControlEventValueChanged];
            
            
            
        
        }
        
        
    }
    -(void)PageCon:(UIPageControl *)p
    {
        NSLog(@"%ld",p.currentPage);
        //我们知道当前小点的位置 通过偏移量设置scrollview的位置就可以了
    //    sv.contentOffset=CGPointMake(375*p.currentPage, 0);
        [sv setContentOffset:CGPointMake(375*p.currentPage, 0)
                    animated:YES];
        
        
    }
    
    
    //3.代理方法
    //UIScrollView 的代理方法 之要移动 就会产生方法
    //偏移量 / 宽度 = 滑动了多少
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        NSLog(@"动了");
        
        
    }
    
    
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        NSLog(@"开始拖拽");
            
    }
    
    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    
    {
        NSLog(@"结束拖拽");
        
    }
    
    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
    {
        
        NSLog(@"开始减速");
    }
    
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        NSLog(@"结束减速");
        int number =(int)scrollView.contentOffset.x/375;
        NSLog(@"%d",number+1);
        
        //设置底部的小点移动到指定的位置
        PControl.currentPage=number;
        
    }
    
    
    @end
  • 相关阅读:
    Kubernetes实战(第二版)--第六章 管理Pod容器的生命周期
    Kubernetes实战(第二版)--第五章 在Pods中运行应用程序
    Kubernetes实战(第二版)--第四章 介绍kubernetes API对象
    第三章 部署第一个应用程序
    Kubernetes实战(第二版)----第2章 理解容器
    dfs 例子演示
    Leetcode 135. 分发糖果
    mysql materialized_MySql优化- 执行计划解读与优化(二)
    说说Golang的使用心得
    最详细之教你Jenkins+github自动化部署.Net Core程序到Docker
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13430587.html
Copyright © 2011-2022 走看看