代码编织梦想


一、UILable

UILable是可以显示在屏幕上并且可以显示文字的一种UI视图

1.UILable基本属性

//定义并创建一个UILable对象
    UILabel* label = [[UILabel alloc] init];
    //显示文字的赋值,需要为字符串对象
    label.text = @"hello, world!";
    //设定label的显示位置
    label.frame = CGRectMake(100, 100, 160, 40);
    //设置label的背景颜色,clearColor表示透明颜色
    label.backgroundColor = [UIColor blackColor];
    //设置label文字的大小,使用系统默认字体,大小为12
    label.font = [UIFont systemFontOfSize:24];
    //设置label文字的颜色
    label.textColor = [UIColor blueColor];
    //将label显示到屏幕上
    [self.view addSubview:label];

2.CGRectMake函数

在iOS开发中,CGRectMake是一个用于创建CGRect结构体的函数。CGRect结构体表示一个矩形,包括位置和大小。
CGRectMake函数的定义如下:

CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

它接受四个参数:矩形的左上角点的x坐标、y坐标、宽度和高度,然后返回一个CGRect结构体。
例如,以下代码创建了一个位于(50, 50)位置,宽度为100,高度为200的矩形:

CGRect rect = CGRectMake(50, 50, 100, 200);

在实际开发中,CGRectMake函数经常用于布局界面元素,并与其他函数和属性一起使用来设置元素的位置和大小。

3.UILable高级属性

 //label高级属性
    //设置阴影颜色
    label.shadowColor = [UIColor grayColor];
    //设置阴影偏移量
    label.shadowOffset = CGSizeMake(0, 0);
    //设置文字对齐方式,默认为靠左对齐
    //NSTextAlignmentCenter为居中对齐,NSTextAlignmentRight为靠右对齐,NSTextAlignmentLeft为靠左对齐,NSTextAlignmentNatural为自然对齐
    label.textAlignment = NSTextAlignmentNatural;
    //设置label文字显示的行数,默认值为1, 也就是说只用一行来显示
    //其他大于0的行数,文字会尽量按照设定行数来显示
    //如果值为0,iOS会对文字自动计算所需要的行数,按照需要的行数来显示文字
    label.numberOfLines = 0;

二、UIButton基础

UIButton是一个按钮控件,分为两种:一种是button,创建一个按钮控件,另一种是imagebutton,可以传入图片。

1.button

//根据类型来创建一个btn对象
    //圆角类型btn:UIButtonTypeRoundedRect
    //通过类方法来创建buttonWithType:类名+方法名
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //设置button按钮的位置
    btn.frame = CGRectMake(100, 100, 100, 40);
    //设置按钮的文字内容
    //第一个参数是字符串类型,表示显示到按钮上的文字
    //第二个参数是设置文字显示的状态类型:UIControlStateNormal表示正常状态
    [btn setTitle:@"按钮01" forState:UIControlStateNormal];
    //第一个参数是字符串类型,表示显示到按钮上的文字
    //第二个参数是设置文字显示的状态类型:UIControlStateHighlight表示高亮状态
    [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
    //添加到视图中显示
    [self.view addSubview:btn];
    //设置背景颜色
    btn.backgroundColor = [UIColor grayColor];
    //设置按钮文字显示的颜色
    //第一个参数是颜色
    //第二个参数是状态
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置按钮按下状态文字显示的颜色
    //第一个参数是颜色
    //第二个参数是状态
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    //设置按钮风格文字颜色,需要注意的是如果同时设置setTitleColor和tintColor以setTitleColor为主
//    btn.tintColor = [UIColor blackColor];
    //设置按钮字体大小
    //titleLabel是UILabel属性
    btn.titleLabel.font = [UIFont systemFontOfSize:18];

运行结果如下:
请添加图片描述

2.imagebutton

- (void)createImageBtn {
    //创建一个自定义类型的btn
    UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮的位置
    btnImage.frame = CGRectMake(100, 200, 100, 100);
    //通过imageNamed类方法将图片传入文件中,btn01和btn02是图片命名
    UIImage* icon01 = [UIImage imageNamed:@"btn01"];
    UIImage* icon02 = [UIImage imageNamed:@"btn02"];
    //设置按钮图片
    //第一个参数是图片对象
    //第二个参数是控件状态
    [btnImage setImage:icon01 forState:UIControlStateNormal];
    [btnImage setImage:icon02 forState:UIControlStateHighlighted];
    [self.view addSubview:btnImage];
}

需要注意的是,传入的图片需要在当前文件之中,并且imageNamed函数传入图片时,需要将图片名包括后缀都写进去。

运行结果如下:
请添加图片描述

三、UIButton事件处理

- (void)createButton {
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 80, 40);
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    //向按钮添加事件函数
    //第一个参数表示事件函数的调用者,也就是说谁来实现事件函数,实现的对象就是谁
    //第二个参数:@ selector(pressBtn):函数对象,当按钮满足第三个参数的事件类型时,就调用该函数
    //第三个参数:UIControlEvent:事件处理函数类型
    //UIControlEventTouchUpInside:当手指离开屏幕时并且手指的位置在按钮范围内则触发事件函数
    //UIControlEventTouchUpOutside:当手指离开屏幕时并且手指的位置在按钮范围外则触发事件函数
    //UIControlEventTouchDown:当手指触碰屏幕上时触发事件函数
    [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpOutside];
    [btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
    UIButton* btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn02.frame = CGRectMake(100, 200, 80, 40);
    [btn02 setTitle:@"按钮02" forState:UIControlStateNormal];
    
    //可以多个按钮使用同一事件函数来处理不同按钮的事件
    [btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn02];
    //设置按钮的标记值
    btn02.tag = 101;
    btn.tag = 102;
}
//参数为调用此函数按钮对象本身
- (void)pressBtn:(UIButton*)btn {
    if (btn.tag == 102) {
        NSLog(@"按钮1被按下");
    }
    if (btn.tag == 101) {
        NSLog(@"按钮2被按下");
    }
}
- (void)touchDown {
    NSLog(@"btn press");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createButton];
}

运行结果如下:
在这里插入图片描述

四、UIView基础

1.基本属性

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个UIView对象
    //UIView是iOS中的视图对象
    //它是显示在屏幕上的所有的对象的基础类
    //也就是说所有显示在屏幕上的对象一定都继承于UIView
    //屏幕上能看到的对象都是UIView的子类
    //UIView是一个矩形对象,有背景颜色,可以显示,有层级关系
    UIView* view = [[UIView alloc] init];
    //设置UIView的位置
    view.frame = CGRectMake(100, 100, 100, 100);
    //设置UIView的背景色
    view.backgroundColor = [UIColor blueColor];
    //将新建的视图添加到父亲视图上
    //1.将新建的视图显示到屏幕上
    //2.将视图作为父亲视图的子视图管理起来
    [self.view addSubview:view];
    //设置hiddden值来决定是否隐藏视图对象,如果为YES则隐藏,为NO则不隐藏,一般情况下默认为NO
    view.hidden = NO;
    //设置视图的透明度
    //alpha的值在0~1之间,为1则不透明,为0则透明,在0~1之间则半透明且透明度随数值变化
    view.alpha = 0.5;
    self.view.backgroundColor = [UIColor yellowColor];
    //设置是否显示不透明
    view.opaque = NO;
    //将视图从父亲视图中删除
    //1:将视图从父亲的管理队列中删除
    //2:视图将不显示在屏幕上
    [view removeFromSuperview];
}


@end

运行结果如下:
请添加图片描述

在这里插入图片描述

五、UIView层级关系

1.基本属性

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建三个视图对象
    UIView* view01 = [[UIView alloc] init];
    view01.frame = CGRectMake(100, 100, 150, 150);
    view01.backgroundColor = [UIColor redColor];
    UIView* view02 = [[UIView alloc] init];
    view02.frame = CGRectMake(125, 125, 150, 150);
    view02.backgroundColor = [UIColor blueColor];
    UIView* view03 = [[UIView alloc] init];
    view03.frame = CGRectMake(150, 150, 150, 150);
    view03.backgroundColor = [UIColor greenColor];
    //将三个视图对象添加到父亲视图中并显示到屏幕上
    //哪一个视图先被添加到父亲视图中,哪一个视图就先被绘制
    //如果若干视图范围相交,先被绘制的视图会被后来绘制的视图将相交区域遮挡
    [self.view addSubview:view01];
    [self.view addSubview:view02];
    [self.view addSubview:view03];
    //将某一个视图调整到最前面显示
    //参数:UIView对象,调整哪一个视图就把哪一个视图对象作为参数传入
    [self.view bringSubviewToFront:view02];
    //将某一个视图调整到最后面显示
    //参数:UIView对象,调整哪一个视图就把哪一个视图对象作为参数传入
    [self.view sendSubviewToBack:view03];
    UIView* viewFront = self.view.subviews[2];
    [view01 removeFromSuperview];
    UIView* viewBack = self.view.subviews[0];
    if (viewBack == view01) {
        NSLog(@"相等");
    }
}

需要注意的是,如果删除view01那么viewBack将不等于view01。

运行结果如下:

请添加图片描述

六、UIWindow对象

1.注意事项

在XCode13之后我们使用代码加载来初始化UIWindow不在使用AppDelegate类而是使用SceneDelegate类,并且不需要创建UIWindow对象。

2.基本属性

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    //整个程序只有一个UIWindow对象
    //在程序级别上表示屏幕窗口
    //UIWindow也继承于UIView并且UIWindoe是一个特殊的UIView
    //创建一个视图控制器作为UIWindow的根视图控制器
    UIViewController *tVC = [[UIViewController alloc] init];
    self.window.rootViewController = tVC;
    //设置窗口背景颜色
    self.window.backgroundColor = [UIColor blueColor];
    //创建一个背景视图
    UIView* backview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    //设置背景视图背景颜色
    backview.backgroundColor = [UIColor greenColor];
    //将背景视图显示到窗口上
    [self.window addSubview:backview];
    //创建一个子视图
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    //设置子视图背景颜色
    view.backgroundColor = [UIColor yellowColor];
    //将子视图显示到背景视图上,当改变背景视图的位置时子视图会随着其位置改变而改变,也就是说子视图的坐标系参照父亲视图的坐标系
    [backview addSubview:view];
}

在这里插入图片描述

当我们打印view.window,backview.window,self.window时发现它们的地址一样,这也印证了前面所说的整个程序只能有一个UIWindow对象,当我们使用addSubview方法时,将self的window属性赋给backView的window属性,再将backView的window属性赋给view.window。

运行结果如下:
请添加图片描述

在这里插入图片描述

六、UIViewController基础

1.基本属性

sceneDelegate.h

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
//定义一个window属性
@property (strong, nonatomic) UIWindow * window;

@end

sceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    //创建视图控制器对象
    ViewController* vcRoot = [[ViewController alloc] init];
    //对窗口的根视图控制器进行赋值操作
    //整个UIKit框架中只能有一个根视图控制器,属于window属性,但是可以有多个视图控制器
    //视图控制器用来管理界面和处理界面的逻辑类对象
    //程序启动前必须对根视图控制器赋值
    self.window.rootViewController = vcRoot;
    self.window.backgroundColor = [UIColor greenColor];
}

viewController.h

#import <UIKit/UIKit.h>
//所有控制器都需要自定义来完成
//继承于官方的UIViewController,实现部分自己来实现比如自己写一些属性或事件函数
@interface ViewController : UIViewController


@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
//当视图控制器第一次加载显示视图时调用viewDidLoad函数
//布局初始化视图来使用,初始化资源使用
- (void)viewDidLoad {
    //调用父亲类的加载视图函数 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView* view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 100, 100, 200);
    [self.view addSubview:view];
    view.backgroundColor = [UIColor greenColor];
    self.view.backgroundColor = [UIColor blueColor];
}


@end

main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
//整个APP程序的主函数,入口函数
int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    //UIApplicationMain是UIKit框架结构的启动函数
    //参数一:argc,启动时带有参数的个数
    //参数二:argv,参数列表
    //参数三:要求传入一个主框架类类名,如果参数为nil,系统会自动使用默认的框架类作为主框架类名
    //参数四:主框架的代理类对象名字
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

运行结果如下:
请添加图片描述

七、UIViewController使用

1.基本属性

sceneDelegate.h

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

sceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    self.window.rootViewController = [FirstViewController new];
}

firtstViewController.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface FirstViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

firstViewController.m

#import "FirstViewController.h"
#import "twoViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

//当屏幕被点击时,调用此函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //创建视图控制器二
    twoViewController* tVC = [[twoViewController alloc] init];
    tVC.modalPresentationStyle = UIModalPresentationFullScreen;
    //显示一个新的视图控制器到屏幕上
    //第一个参数:新的视图控制器对象
    //第二个参数:是否使用动画切换效果
    //第三个参数:切换结束后功能调用,使用block代码块,不需要则传nil值
    [self presentViewController:tVC animated:YES completion:nil];
}

//第一次显示视图时调用
//视图初始化时调用并且只能调用一次
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    NSLog(@"viewDidLoad!第一次加载视图!");
}
//视图状态分为三种状态
//1:显示前(不显示)
//2:正在处于显示状态
//3:已经被隐藏

//当视图控制器的视图即将显示时,调用此函数
//BOOL参数表示是否用动画切换后显示
//每一次视图显示时都要被调用
- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"视图即将显示!");
}
//视图即将消失时,调用此函数
//BOOL参数表示是否用动画切换后显示
//当前的状态:视图还是显示在屏幕上
- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"视图即将消失");
}
//当视图已经显示到屏幕上时调用此函数
//BOOL参数表示是否用动画切换后显示
//当前状态:视图已经显示到屏幕上
- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"视图已经显示!");
}
//当前视图已经从屏幕上消失
//BOOL参数表示是否用动画切换后显示
//当前状态:视图已经从屏幕上消失
- (void)viewDidDisappear:(BOOL)animated {
    NSLog(@"视图已经消失!");
}

twoViewController.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface twoViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

twoViewController.m

#import "twoViewController.h"

@interface twoViewController ()

@end

@implementation twoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
}
//点击当前控制器二的界面屏幕时
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //使当前的控制器消失掉
    //第一个参数:是否有动画效果
    //第二个参数:结束后是否调用block块来完成一些功能
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

运行结果如下:
在这里插入图片描述
在这里插入图片描述

七、定时器和视图移动

1.基本属性

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//定义一个定时器对象
//可以在每隔固定时间发送一个消息
//接着可以通过此消息来调用相应的时间函数
//通过此函数可在固定时间段来完成一个根据时间间隔的任务
@property (retain, nonatomic)NSTimer* timerView;

@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize timerView = _timeView;
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton* btn01 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn01.frame = CGRectMake(100, 100, 100, 100);
    btn01.backgroundColor = [UIColor yellowColor];
    [btn01 setTitle:@"启动定时器" forState:UIControlStateNormal];
    [btn01 addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside];
    UIButton* btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn02.frame = CGRectMake(100, 300, 100, 100);
    btn02.backgroundColor = [UIColor yellowColor];
    [btn02 setTitle:@"停止定时器" forState:UIControlStateNormal];
    [btn02 addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn01];
    [self.view addSubview:btn02];
    UIView* view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, 100, 100);
    view.backgroundColor = [UIColor greenColor];
    view.alpha = 0.5;
    //设置view的标签值
    //通过父亲视图对象以及view的标签值可以获得相应的子视图对象
    view.tag = 101;
    [self.view addSubview:view];
}
//启动定时器函数
- (void)pressStart {
    //NSTimer的类方法创建一个定时器并且启动这个定时器
    //第一个参数:每隔多少时间调用定时器函数,以秒为单位
    //第二个参数:表示实现定时器函数的对象(指针)
    //第三个参数:定时器函数对象
    //第四个参数:可以给定时器函数中传入参数,无参数就传nil
    //第五个参数:定时器是否重复操作,YES为重复,NO为只完成一次函数调用
    self.timerView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:@"小明" repeats:YES];
}
//停止定时器函数
- (void)pressStop {
    if (self.timerView != nil) {
        NSLog(@"成功停止定时器");
        [self.timerView invalidate];
    }
}
//定时器函数
//可以将定时器本身作为参数传入
- (void)updateTimer:(NSTimer*)timer {
    NSLog(@"test!!!");
    //传入的参数保存在timer的userInfo属性中,该userInfo为id类型
    NSLog(@"name = %@", timer.userInfo);
    //tag值最好从100开始
    UIView* view = [self.view viewWithTag:101];
    //让view视图在屏幕上移动
    view.frame = CGRectMake(view.frame.origin.x + 1, view.frame.origin.y + 1, 100, 100);
}
@end

运行结果如下:
在这里插入图片描述

在这里插入图片描述

八、UISwitch控件

1.基本属性

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    //定义一个开关控件
    //可以进行状态的改变
    //开:关:两种状态可以切换
    //所有UIKit框架库中的控件均以UI开头
    //苹果官方的控件都定义在UIKit框架库中
    UISwitch* _mySwitch;
}
@property (retain, nonatomic)UISwitch* mySwitch;
@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建UISwitch对象
    _mySwitch = [[UISwitch alloc] init];
    //苹果官方的该控件的位置设置
    //位置X,Y的值可以改变
    //宽度和高度值无法改变
    _mySwitch.frame = CGRectMake(100, 100, 80, 40);
    //开关状态设置属性
    //YES:开起状态
    //NO:关闭状态
//    _mySwitch.on = YES;
    //也可以使用set函数
//    [_mySwitch setOn:YES];
    
    //也可以使用下面的set函数
    //第一个参数:状态设置
    //第二个参数:是否开启动画效果
    [_mySwitch setOn:YES animated:YES];
    [self.view addSubview:_mySwitch];
    //设置开启状态的风格颜色
    [_mySwitch setOnTintColor:[UIColor blueColor]];
    //设置开关按钮的风格颜色
    [_mySwitch setThumbTintColor:[UIColor greenColor]];
    //设置整体风格颜色
    [_mySwitch setTintColor:[UIColor purpleColor]];
    //向开关控件添加事件函数
    //第一个参数:函数实现对象
    //第二个参数:函数对象
    //第三个参数:事件响应时的事件类型,其中UIControlEventValueChanged表示状态发生变化时触发函数
    [_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
}
//如果想判断当前开关的状态可以为函数传入参数
//参数传入开关对象本身
- (void)swChange: (UISwitch*)sw{
    if (sw.on == YES) {
        NSLog(@"开关被打开!");
    }
    else {
        NSLog(@"开关被关闭!");
    }
    NSLog(@"开关状态发生变化!");
}


@end

运行结果如下:
在这里插入图片描述

在这里插入图片描述

九、UISlider和UIProgressView(进度条和滑动条)

1.进度条(UIProgressView)

viewController.h

 //进度条对象
    //一般用来表示下载或视频播放的进度
    //进度条不能响应事件
    UIProgressView* _progressView;
//定义进度条属性
@property(retain, nonatomic)UIProgressView* progressView;

viewController.m

//进度条的创建
    _progressView = [[UIProgressView alloc] init];
    //进度条的位置及大小设置
    //需要注意的是进度条的高度不能变其它都可以改变
    _progressView.frame = CGRectMake(100, 100, 200, 40);
    //设置进度条的背景颜色
    _progressView.trackTintColor = [UIColor grayColor];
    //设置进度条的风格颜色
    _progressView.progressTintColor = [UIColor blackColor];
    //设置进度条的进度值
    //范围从0~1
    //最小值为0
    //最大值为1
    _progressView.progress = 0.5;
    //设置进度条的风格特征
    _progressView.progressViewStyle = UIProgressViewStyleDefault;
    [self.view addSubview: _progressView];

运行结果如下:

请添加图片描述

2. 滑动条(UISlider)

viewController.h

//滑动条对象
    //一般用来进行音量调整等
    UISlider* _slider;
//定义滑动条属性
@property(retain, nonatomic)UISlider* slider;

viewController.m

//创建滑动条对象
    _slider = [[UISlider alloc] init];
    //滑动条的位置及大小设置
    //需要注意的是滑动条的高度不能变其它都可以改变
    _slider.frame = CGRectMake(10, 200, 300, 40);
    //设置滑动条最大值,需要注意的是进度条不能设置最大值
    _slider.maximumValue = 100;
    //设置滑动条最小值,可以为负值
    _slider.minimumValue = 0;
    //设置滑动条的滑块位置,其位置取决于最大值于最小值,其值为float值
    _slider.value = 50;
    //左侧滑条背景颜色
    _slider.minimumTrackTintColor = [UIColor blueColor];
    //右侧滑条背景颜色
    _slider.maximumTrackTintColor = [UIColor greenColor];
    //设置滑块颜色
    _slider.thumbTintColor = [UIColor blackColor];
    //对滑动条添加事件函数,只要 ——slider的value发生变化就会调用事件函数
    [_slider addTarget:self action:@selector(pressSlider) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview: _slider];
}
//事件函数
- (void)pressSlider {
    //如果想通过滑动条改变进度条可以通过下面这个代码实现
    _progressView.progress = (_slider.value - _slider.minimumValue) / (_slider.maximumValue - _slider.minimumValue);
    NSLog(@"value = %f", _slider.value);
}
@end

运行结果如下:

请添加图片描述

在这里插入图片描述

总结

以上就是本篇文章的所有内容,如果对你有帮助的话,请点赞支持一下~

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_73974920/article/details/130912373