OC-UI阶段学习12-UIControl与子类:UIPageControl、UIStepper

一、UIPageControl


UIPageControl就是下面的三个小白点,与UIScrollView配合可以切换视图

OC-UI阶段学习12-UIControl与子类:UIPageControl、UIStepper_第1张图片


常用属性不多,主要是回调方法要配合UIScrollView的contentOffset属性进行页面切换

//创建UIPageControl
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 200, 150, 60)];
    //设置页数
    pageControl.numberOfPages = 3;
    //设置默认显示页
    pageControl.currentPage = 0;
    //设置背景色
    pageControl.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    
    //当前小点的颜色
    pageControl.pageControlCurrentPageIndicatorTintColor = [UIColor redColor];

    //非当前的小点颜色
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];


    //添加方法
    [pageControl addTarget:self action:@selector(changeViewPage:) forControlEvents:UIControlEventValueChanged];
    
    //添加到父视图
    [self.view addSubview:pageControl];

具体说一下通过pageControl切换ScrollVIew

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建scrollView
    UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    //设置滚动区域
    myScrollView.contentSize = CGSizeMake(WIDTH*3,HEIGHT);
    //设置tag
    myScrollView.tag = 1000;
    
    //给myScrollView添加三页内容
    for (int i = 0; i<3; i++) {
        UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH*i, 0, WIDTH, HEIGHT)];
        myLabel.text = [NSString stringWithFormat:@"第%d页",i];
        myLabel.font = [UIFont systemFontOfSize:100];
        myLabel.textAlignment = NSTextAlignmentCenter;
        myLabel.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
        [myScrollView addSubview:myLabel];
    }
    
    //创建UIpageController
    UIPageControl *myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 630, WIDTH, 40)];
    
    //设置页数
    myPageControl.numberOfPages = 3;
    
    //设置背景色
    myPageControl.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
    
    //给pageControl添加方法
    [myPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    //添加到父视图
    [self.view addSubview:myScrollView];
    [self.view addSubview:myPageControl];
    
}

//实现pageControl方法
-(void)changePage:(UIPageControl*)sender
{
    //根据tag得到scrollView
    UIScrollView *myScrollView = (UIScrollView*)[self.view viewWithTag:1000];
    //根据page的页数变换scrollView的偏移量
    myScrollView.contentOffset = CGPointMake(WIDTH*sender.currentPage,0);
    
}

实现效果如下

OC-UI阶段学习12-UIControl与子类:UIPageControl、UIStepper_第2张图片  OC-UI阶段学习12-UIControl与子类:UIPageControl、UIStepper_第3张图片



二、UIStepper

一个加号一个减号的控件,可以当做音量加减按钮

//uiSteppear + -
    //初始化
    UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(10, 20, 100, 100)];
    
    //设置数值范围
    stepper.maximumValue = 100;
    stepper.minimumValue = 0;
    
    //设置每点一次的增减量
    stepper.stepValue = 20;
    
    //设置是否可以循环
    stepper.wraps = YES;
    
    //设置一直按住是否连续增加
    stepper.autorepeat = YES;
    
    //设置默认值
    stepper.value = 0;
    
    //添加方法
    [stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
    
    //添加到父视图
    [self.view addSubview:stepper];




你可能感兴趣的:(UI,控件,UIPageControl,UIStepper,uicontrol)