UI的学习(一)

UI的学习(一)

UIlabel

首先在我们的创建的项目中的ViewController 在 实现部分添加创建UILabel的函数。

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

label的主要参数为:

  • text:显示的文字
  • background:label的背景颜色
  • shadowColor:字体的阴影颜色
  • shadowOffset:阴影的偏离位置
  • textAlignment:设置文字的位置
  • numberOfLine:长文字按照设计的行数来显示
  • textColor:设置文字的颜色
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (void) creatUI {
    UILabel *label = [[UILabel alloc] init];
    label.text = @"486是个好人"; //显示文字的赋值
    label.frame = CGRectMake(10, 400, 410, 200); //显示位置
    label.backgroundColor = [UIColor redColor]; //label的背景颜色
    self.view.backgroundColor = [UIColor yellowColor]; //整个屏幕的背景颜色
    [self.view addSubview: label]; //显示label
    label.font = [UIFont systemFontOfSize:34]; //label的大小和字体
    label.textColor = [UIColor blueColor]; //字体颜色
    label.shadowColor = [UIColor greenColor]; //字体阴影颜色
    label.shadowOffset = CGSizeMake(10, 10); //阴影的偏离位置
    
    label.textAlignment = NSTextAlignmentCenter; //设置居中对齐
    label.numberOfLines = 3; //文字尽量按设计的行数来显示
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
  	//调用和创建UI函数
    // Do any additional setup after loading the view.
}


@end

这里主要注意的是两个类型,一个是CGRectMake类型(这个结构体又包括了origin和size两个成员变量),origin表示的是一个label的起始点,size表示的是一个显示出来的矩阵的宽和高,我们的坐标系是以屏幕左上角为基准点,向下为y,向右为x。

在这里插入图片描述

UIButton

UIButton是UIKit框架中的一个类,它是继承自UIView的子类,用于创建可点击的按钮控件。UIButton通常用于响应用户的触摸事件,并执行相应的操作或触发特定的动作。主要分成UIButton的类型和事件触发两个方面来展开。

UIButton的两种形式

UIButton的主要参数为

  • frame:大小
  • backgroundColor:背景颜色

这里还要注意一个点就是我们的button有两个状态一个是正常状态一个是高亮状态,可以根据不同的状态给用户一个反馈。

普通的按钮

- (void)creatUIRectButton { //创建普通的UIButton
    UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];//根据类型来创建
    btn.frame = CGRectMake(10, 100, 150, 80);//一个矩型设置的一个位置
    [btn setTitle:@"button1" forState: UIControlStateNormal]; //一个是按钮的位置,一个是按钮文字的现显示的状态
    [btn setTitle:@"fitst button1" forState: UIControlStateHighlighted];
    btn.backgroundColor = [UIColor grayColor]; // 设置背景颜色
    [btn setTitleColor:[UIColor redColor] forState: UIControlStateNormal]; //设置字面的颜色未按下的时候
    [btn setTitleColor:[UIColor greenColor] forState: UIControlStateHighlighted];//设置字面按下时候的颜色
    [btn setTintColor: [UIColor whiteColor]];//同一设置颜色,优先级在前两个函数的后面
    btn.titleLabel.font = [UIFont systemFontOfSize:18];//设置字体大小
    [self.view addSubview: btn]; //添加到视图中显示
}

这是这个按钮的显示效果

在这里插入图片描述

图片按钮

- (void) creatImageButton {
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 300, 150, 100);
    UIImage* icon1 = [UIImage imageNamed:@"btn02.jpeg"];//设置路径
    UIImage* icon2 = [UIImage imageNamed:@"btn03.jpg"];
    [btn setImage:icon1 forState:UIControlStateNormal];
    [btn setImage:icon2 forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
}

实现效果:

在这里插入图片描述

UIButton的事件触发

我们这里考虑用addTarget来给我们的一个UIButton来添加点击事件,在通过我们编写的一个函数通过判断按钮不同的一个tag值来执行不同的操作

tips:这里我们的tag从100开始,这样方便自己去记录。

- (void)creatUIRectButton { //创建普通的UIButton
    UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];//根据类型来创建
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(10, 400, 120, 80);
    [btn1 setTitle:@"test button" forState:UIControlStateNormal];
    [btn1 setTitle:@"按下时候" forState:UIControlStateHighlighted];
    [btn1 addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];//手指离开时在按钮范围内
    [btn1 setTintColor:[UIColor whiteColor]];
    btn1.backgroundColor = [UIColor grayColor];
    
    btn.frame = CGRectMake(10, 100, 150, 80);//一个矩型设置的一个位置
    [btn setTitle:@"button1" forState: UIControlStateNormal]; //一个是按钮的位置,一个是按钮文字的现显示的状态
    [btn setTitle:@"fitst button1" forState: UIControlStateHighlighted];
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
    [btn setTitleColor:[UIColor greenColor] forState: UIControlStateHighlighted];
    [btn setTintColor: [UIColor whiteColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 101;//添加号码用于判断
    btn1.tag = 102;
    [self.view addSubview: btn1];
    [self.view addSubview: btn]; //添加到视图中显示
}
- (void) press:(UIButton*) btn {
    if (btn.tag == 102) {
        NSLog(@"btn 被按下");
    }
    if (btn.tag == 101) {
        NSLog(@"btn1被按下");
    }
}

实现效果:

在这里插入图片描述

每一次按下我们的控制台会打印:

在这里插入图片描述

UIView

视图对象是显示在我们屏幕上的所有对象的基类,也就是我们上面的一个UIButton和UIlabel的基类。

基本属性:

  • background:背景颜色
  • frame:大小
  • alpha:透明度。1为不透明,0为透明
//所有看到的对象全部都是UIView的子类
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView* view = [[UIView alloc] init];
    //设置一个位置
    view.frame = CGRectMake(10, 100, 230, 70);
    view.backgroundColor = [UIColor blueColor];
    self.view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];//父视图添加子视图
    //view.hidden = YES为不显示
    //view.alpha = 0.6;
    view.opaque = YES;//是否显示不透明
    //1不透明
    //0透明
     //将新建的视图显示到屏幕上
    //子视图会受到父视图的管理
    //[self creatUIRectButton];
    //[self creatImageButton];
    // Do any additional setup after loading the view.
}


@end

显示效果:

在这里插入图片描述

多个视图之间的关系

在多个视图同时被添加到我们的父亲视图上的时候,会出现一个先后添加的问题,这时候我们就要理解它先后添加带来的视觉效果,这里我们通过三个视图的添加到同一个父视图上来展现一个它的效果。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView* view = [[UIView alloc] init];
    //设置一个位置
    view.frame = CGRectMake(100, 100, 150, 150);
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];//父视图添加子视图
    
    UIView* view1 = [[UIView alloc] init];
    //设置一个位置
    view1.frame = CGRectMake(125, 125, 150, 150);
    view1.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view1];//父视图添加子视图
    
    UIView* view2 = [[UIView alloc] init];
    //设置一个位置
    view2.frame = CGRectMake(150, 150, 150, 150);
    view2.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view2];//父视图添加子视图
    //[self.view bringSubviewToFront:view];//将视图跳涨到最前面
    //[self.view sendSubviewToBack:view2];//调整到最后面
    UIView* viewfront = self.view.subviews[0];
    if (viewfront == view) {
        NSLog(@"dddd");
    }
}


@end

在这里插入图片描述

这里可以看到我们的第三个视图会覆盖第二个视图,第二个视图会覆盖第一个视图,所以我们可以简单理解成一个后面的视图会覆盖前面的视图。

控制台的输出:

在这里插入图片描述

这就说明了一个点就是,我们的添加视图到自己的subview中间的顺序是后添加的视图插入到后面部分。

UIWindow

这里引用一段chatgpt的内容来介绍我的UIWindow,我们主要通过一个根视图控制器来展示出我们不同的一个

UIWindow 是应用程序中最顶层的视图容器,所有其他的视图控件都是建立在 UIWindow 之上的。它为应用程序的视图层次结构提供了一个容器和根视图。

在现在这个版本中我们已经不需要创建UIWindow了,程序自己会创建,我们只用创建一个根视图控制器来决定我们要推出那一个视图控制器展示在用户眼前。

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    //UIWindow继承于UIView,它是一个特殊的UIView
    //UIScreen:屏幕硬件表示类
    //mainScreen表示主屏幕的设备信息
    //bounds表示屏幕的宽高值
    self.window.rootViewController = [[UIViewController alloc] init];//创建根视图控制器
    self.window.backgroundColor = [UIColor blueColor];
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
    view.backgroundColor = [UIColor orangeColor];
    UIView* backview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 240, 360)];
    backview.backgroundColor = [UIColor redColor];
    //子视图的坐标是参照父亲视图的坐标系
    //当父亲视图移动的时候,所有的子视图都会移动
    
    [backview addSubview: view];
    [self.window addSubview: backview];
    
    [self.window makeKeyAndVisible]; //显示我们的根视图
    NSLog(@"%@\n, %@\n, %@\n", view.window, backview.window, self.window);
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
}


- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end

显示的效果为:

在这里插入图片描述

如果移动父亲视图会改变状况也就是如下图所示:

在这里插入图片描述

UIViewController

我们的项目在创建之初就会自动给我们创建一个UIViewController的两个文件

但是我们也仅仅只是知道我们在UIViewController文件中viewDidLoad函数会被调用,并不清楚它中间的调用关系。下面来介绍一下它的一个调用关系:

AppDelegate.m → SceneDelegate.m → ViewController.m

self.window.rootViewController = [[UIViewController alloc] init];//创建根视图控制器
//这里把根视图控制器设置成我的UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

上面就是有关与他的一个调用过程

一个视图推出另一个视图

接下来我们来尝试一下如何推出另一个视图

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    // Do any additional setup after loading the view.
}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {//我们触碰空白处会执行事件的一个函数
    //使当前的控制器消失掉
    //P1:新的视图对象
    //P2:使用动画切换效果
    //P3:切换结束后功能调用,不需要就传入一个nil。
    [self dismissViewControllerAnimated:YES completion: nil];//消失这个view
}

这时候我们在根视图控制器中推出我们的另一个view。

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    View02* vc = [[View02 alloc] init];
    [self presentViewController:vc animated:YES completion: nil];//这里推出我们的view页面。
}

实现的效果:

在这里插入图片描述

点击后:

在这里插入图片描述

定时器和视图移动

这里讲一下我们的定时器和视图移动的内容

先讲一下有关设置定时器的内容:

  • _timerView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:@"xiaoming" repeats:YES];这里一共有5个参数他分别表达的内容是
  • P1:每隔多长时间调用一下定时器函数,以秒为单位
  • P2:表示定时器函数的对象
  • P3:定时器函数对象
  • P4:可以定时器函数中的一个参数,无参数可以传入nil
  • P5:定时器是否重复操作,YES为重复

这里是代码内容

@interface ViewController ()

@end

@implementation ViewController
@synthesize timerView = _timerView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 80, 40);
    [btn setTitle:@"timer to begin" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor greenColor];
    
    UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 200, 80, 40);
    [btn1 setTitle:@"timer to stop" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(pressStop:) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:btn];
    [self.view addSubview:btn1];
    
    UIView* view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, 80, 80);
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];
    view.tag = 101;//设置一个标签
}
- (void) pressStart {
    if (_timerView == nil) { //这个部分是为了防止重复创建
      //P1:每隔多长时间调用一下定时器函数,以秒为单位
    //P2:表示定时器函数的对象
    //P3:定时器函数对象
    //P4:可以定时器函数中的一个参数,无参数可以传入nil
    //P5:定时器是否重复操作,YES为重复
    //返回值为一个新建好的定时器对象
        _timerView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:@"xiaoming" repeats:YES];
    } else {
        [_timerView setFireDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; //定时器在这之后的0.1秒时候调用
    }
    
}
- (void) updateTimer:(NSTimer*) timer {
    NSLog(@"name == %@", timer.userInfo);
    UIView* view = [self.view viewWithTag:101];
    view.frame = CGRectMake(view.frame.origin.x + 1, view.frame.origin.y + 1, 80, 80);
}
- (void) pressStop:(id)sender{
    
    if (_timerView != nil) {
        [_timerView invalidate];//让定时器失效
        _timerView = nil;
    }
}

@end

这里笔者出现了一个问题:就是按下两次我们的按钮后,我们的视图无法停止的问题,后面修改定时器部分的代码为这个情况后解决了这个问题:

- (void) pressStart {
    if (_timerView == nil) { //这个部分是为了防止重复创建,导致无法停下的问题
        _timerView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:@"xiaoming" repeats:YES];
    } else {
        [_timerView setFireDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; //定时器在这之后的0.1秒时候调用
    }
    
}

实现的效果为点下按钮后视图开始移动,按下停止后视图停止移动:

在这里插入图片描述

UISwitch

这是我们苹果官方定义的一个控件,他的主要属性是

  • frame:但是他仅仅只能修改位置,不能修改长度和宽度
@interface ViewController : UIViewController {
    UISwitch* _mySwitch;
    //定义开关控件
    //开:关两种状态可以用来切换
    //所有的UIkit框架中的控件均以UI开头
    //苹果官方的控件都定义在UIKit
}
@property(strong, nonatomic) UISwitch* mySwtich;

@end
 #import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mySwtich = _mySwtich;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _mySwitch = [[UISwitch alloc] init];
    _mySwitch.frame = CGRectMake(100, 100, 80, 40);//开关长度和宽度无法修改,只能修改位置
    _mySwitch.on = YES;
    //[_mySwitch setOn:YES animated:YES];
    //p2:是否开启动画
    [self.view addSubview: _mySwitch];
    [_mySwitch setOnTintColor:[UIColor redColor]];//修改颜色
    [_mySwitch setThumbTintColor:[UIColor greenColor]];//修改圆形按钮颜色
    [_mySwitch setTintColor:[UIColor purpleColor]];
    [_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
    //开关状态发生变化,重要的是第三个参数是指这个控件状态变化的情况他发生改变
}

- (void) swChange:(UISwitch*) sw {
    //ON表示当前结束的状态
    if (sw.on == YES) {
        NSLog(@"开关打开");
    } else {
        NSLog(@"开关关闭");
    }
}
@end

实现的效果为:

在这里插入图片描述

UISlider和UIProgressSlid

UISlider 是一种常用的用于控制数值大小的 UI 控件。UIProgressView 是一种用于显示进度的 UI 控件。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mySwtich = _mySwtich;
@synthesize progressView = _progressView;
@synthesize slider = _slider;
- (void)viewDidLoad {
    [super viewDidLoad];
    _progressView = [[UIProgressView alloc] init];
    _progressView.frame = CGRectMake(50, 100, 200, 40);//位置可变化,高度不可以变化
    _progressView.progressTintColor = [UIColor redColor];
    _progressView.progressViewStyle = UIProgressViewStyleDefault;
    _progressView.progress = 0.7;//设置进度条的进度值
    [self.view addSubview:_progressView];
    _progressView.trackTintColor = [UIColor blackColor];
    _slider = [[UISlider alloc] init];
    _slider.frame = CGRectMake(10, 200, 300, 40);
    _slider.maximumValue = 100;
    _slider.minimumValue = -100;
    _slider.value = -100;
    _slider.minimumTrackTintColor = [UIColor blueColor];
    _slider.maximumTrackTintColor = [UIColor greenColor];
    _slider.thumbTintColor = [UIColor orangeColor];
    [_slider addTarget:self action:@selector(pressSlider:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_slider];
}
- (void) pressSlider:(UISlider*) slider {
    _progressView.progress = (_slider.value - _slider.minimumValue) / (_slider.maximumValue - _slider.minimumValue);
    NSLog(@"value = %lf", slider.value);
}


@end

实现的效果为:

在这里插入图片描述

步进器与分栏控制器

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize stepper = _stepper;
@synthesize segControl = _segControl;
- (void)viewDidLoad {
    [super viewDidLoad];
    _stepper = [[UIStepper alloc] init];
    _stepper.frame = CGRectMake(100, 100, 80, 32321);
    _stepper.minimumValue = 0;
    _stepper.maximumValue = 100;
    _stepper.autorepeat = YES;
    //是否可以重复响应事件操作
    //在前者为正确的情况下,将步进结果通过事件函数来打印。
    _stepper.continuous = NO;
    _stepper.value = 10;
    [_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];
    _stepper.stepValue = 5;
    [self.view addSubview:_stepper];
    
    //分栏控件
    _segControl = [[UISegmentedControl alloc] init];
    _segControl.frame = CGRectMake(10, 200, 300, 40);//宽度可变
    [_segControl insertSegmentWithTitle:@"dd" atIndex:0 animated:NO];
    [_segControl insertSegmentWithTitle:@"cc" atIndex:1 animated:NO];
    [_segControl insertSegmentWithTitle:@"bb" atIndex:2 animated:NO];
    _segControl.selectedSegmentIndex = 0;
    [_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview: _segControl];
    // Do any additional setup after loading the view.
}
-(void) segChange {
    NSLog(@"%lu", _segControl.selectedSegmentIndex);
}
-(void) stepChange {
    NSLog(@"step press ! value = %lf", _stepper.value);
}

@end

在这里插入图片描述

UITextField

UITextField是iOS开发中常用的控件之一,用于在应用程序中接收用户的文本输入。UITextField可以放置在视图层次结构中的任何位置,并通过键盘输入文本。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize textField = _textField;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField = [[UITextField alloc] init];
    //创建一个文本输入区对象
    self.textField.frame = CGRectMake(100, 100, 100, 40);
    //设定位置
    self.textField.text = @"用户名";
    self.textField.font = [UIFont systemFontOfSize:15];//设置字体大小
    self.textField.textColor = [UIColor blackColor];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;//设置圆角风格
    //self.textField.borderStyle = UITextBorderStyleLine; // 线框风格
    self.textField.keyboardType = UIKeyboardTypeNumberPad;
    //设置虚拟键盘风格
    //UIKeyboardTypeDefault默认风格
    //UIKeyboardTyprNamePhonePad字母和数字的组合风格
    //UIKeyboradTypeNumberPad:纯数字风格
    self.textField.placeholder = @"请输入用户名";
    //提示文字
    self.textField.secureTextEntry = NO;
    //是否为密码输入
    //YES:作为密码处理,原点加密
    //NO:正常显示
    [self.view addSubview:self.textField];
    self.textField.delegate = self;
    // Do any additional setup after loading the view.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textField resignFirstResponder];//让虚拟键盘回收,不再作为第一消息响应者
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"开始编辑了");
}
-(void) textFieldDidEndEditing:(UITextField *)textField {
    self.textField.text = @"";
    NSLog(@"开始结束编辑了");
}
//是否可以进行输入
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
//是否可以结束输入
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (self.textField.text.length < 8) {
        return NO;
    } else {
        return YES;
    }
    
}
@end

在这里插入图片描述

UIScrollView

UIScrollView是一个强大的视图容器,我们可以通过这个视图容器实现一个滑动的效果,他主要有以下几个属性

  • frame:指定滚动视图的大小(也就是可以滚动视图显示的那一部分内容)
  • contentSize:设置的是滚动视图内容的大小,也就是一个画布的大小
  • contentOffset:设置的是滚动视图的内容的偏移量,CGPoint类型的变量
  • bounces:是否有弹簧效果
  • pagingEnable:是否按照整页滚动
  • scrollEnale:是否开启滚动效果
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定义一个滚动视图
    UIScrollView* sv = [[UIScrollView alloc] init];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    sv.frame = screenBounds;
    //是否按照整页滚动
    sv.pagingEnabled = YES;
    //是否可以开启滚动效果
    sv.scrollEnabled = YES;
    //设置画布的大小,画布显示在滚动视图内部,一般大于frame的大小
    sv.contentSize = CGSizeMake(screenBounds.size.width * 5, screenBounds.size.height);
    //设置边缘弹动
    sv.bounces = YES;
    //开启横向弹动
    sv.alwaysBounceHorizontal = YES;
    //开启纵向弹动
    sv.alwaysBounceVertical = YES;
    //是否显示横向滚动条
    sv.showsHorizontalScrollIndicator = YES;
    //是否显示纵向滚动条
    sv.showsVerticalScrollIndicator = YES;
    //设置背景颜色
    sv.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:sv];
    for (int i = 1; i <= 5; i++) {
        NSString* strName = [NSString stringWithFormat:@"%d.jpg", i];
        UIImage* image  = [UIImage imageNamed:strName];
        UIImageView* iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(screenBounds.size.width * (i - 1), 0, screenBounds.size.width, screenBounds.size.height);
        [sv addSubview:iView];
    }
    [self.view addSubview:sv];
    // Do any additional setup after loading the view.
}


@end

实现的一个效果:

在这里插入图片描述

有关实现它的代理函数

这里我们主要是可以通过下面的协议函数,可以通过协议函数来获取我们的一个当前位置

  • -(void) scrollViewDidScroll:(UIScrollView *)scrollView

同时可以明白一个拖动这一个动作是由几个步骤组成的。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrolView = [[UIScrollView alloc] init];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    _scrolView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height*0.75);
    _scrolView.bounces = NO;
    //_scrolView.userInteractionEnabled = NO;
    //是否接受触碰事件,yes接受,no不接受
    _scrolView.contentSize = CGSizeMake(screenBounds.size.width, screenBounds.size.height * 9 * 0.75);
    for (int i = 0; i < 9; i++) {
        NSString* str = [NSString stringWithFormat:@"%d.jpg", i + 1];
        UIImage* image = [UIImage imageNamed:str];
        UIImageView* iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(0, screenBounds.size.height * i * 0.75, screenBounds.size.width, screenBounds.size.height * 0.75);
        [_scrolView addSubview:iView];
    }
    [self.view addSubview:_scrolView];
    _scrolView.contentOffset = CGPointMake(0, 0);
    _scrolView.pagingEnabled = NO;
    _scrolView.delegate = self;
    //当前视图控制器作为代理对象
    // Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    [_scrolView scrollRectToVisible:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height * 0.75) animated:YES];
}
//当视图移动时,都会调用这个函数
//调用这个协议的滚动视图对象
//使用这个函数来监控滚动视图的位置
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"y = %lf", scrollView.contentOffset.y);
}
//结束拖动的时候调用这个函数
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"结束拖动的时候调用这个函数");
}
//滚动视图即将开始被拖动的时候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"滚动视图即将开始被拖动的时候");
}
//即将结束拖动的时候调用
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"即将结束拖动的时候调用");
}
//视图即将减速的时候
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"视图即将减速的时候");
}
//视图即将结束减速的时候调用,视图停止的瞬间调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"视图即将结束减速的时候调用");
}
@end

实现效果:

在这里插入图片描述

我们进行一次拖动后打印台的结果:

在这里插入图片描述

这就展示了一个我们拖动后的一个变化的顺序:

先开始拖动,拖动后会开始进行一个减速,直到减速停止,一共这几个步骤。

UIAlertController和UIActivityIndicatorView

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize activi = _activi;
@synthesize alertController = _alertController;
- (void)viewDidLoad {
    [super viewDidLoad];
    for (int i = 0; i < 2; i++) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(100, 100 + 100 * i, 100, 40);
        if (i == 0) {
            [btn setTitle:@"警告对话框" forState:UIControlStateNormal];
        } else if (i == 1) {
            [btn setTitle:@"等待提示器" forState:UIControlStateNormal];
        }
        btn.tag = 101 + i;
        [btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: btn];
    }
    
    // Do any additional setup after loading the view.
}
- (void) press:(UIButton*) btn {
    if (btn.tag == 101) {
        _alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"手机电量过低" preferredStyle:UIAlertControllerStyleAlert];
        // 添加一个"取消"按钮
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                              style:UIAlertActionStyleCancel
                                                            handler:nil];
        [_alertController addAction:cancelAction];
        UIAlertAction *newAction = [UIAlertAction actionWithTitle:@"新的"
                                                              style:UIAlertActionStyleDefault
                                                            handler:nil];
        [_alertController addAction:newAction];
        // 添加一个"确认"按钮
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了确认按钮");
                                                             }];
        [_alertController addAction:confirmAction];
        [self presentViewController: _alertController animated:YES completion:nil];
        
    } else if (btn.tag == 102) {
        _activi = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];
        _activi.activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;
        [self.view addSubview:_activi];
        [_activi startAnimating];
        //[_activi stopAnimating];
    }
}
@end

我们现在的警告弹窗都是通过UIAlertController去实现的,我们是通过给这个控制器添加UIAlertAction去实现的,UIAlertAction相当于一个按钮,我们设置这种按钮主要通过actionWithTitle: style: handler:这个方法来添加文本,风格,执行的函数来实现的一个按钮,两个按钮的话会是左右对称,三个按钮则是竖向排列。

实现效果:

在这里插入图片描述

另一个则是一个等待提示器:

在这里插入图片描述

在iOS13之后设定等待提示器的风格只有UIActivityIndicatorViewStyleLarge和UIActivityIndicatorViewStyleMedium两种风格**,**舍弃了原来小白、小灰、大白三种风格。

以上就是对于UI学习的一个简单的总结。

相关推荐

  1. UI/UX学习资料

    2024-06-07 07:46:02       37 阅读
  2. Selenium——基于WebUI自动化测试工具(

    2024-06-07 07:46:02       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 07:46:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 07:46:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 07:46:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 07:46:02       18 阅读

热门阅读

  1. UIScrollView的相关笔记

    2024-06-07 07:46:02       9 阅读
  2. 如何把linux安装到单片机中

    2024-06-07 07:46:02       7 阅读
  3. Swift对比版本号

    2024-06-07 07:46:02       8 阅读
  4. iOS13之后获取状态栏高度的方法

    2024-06-07 07:46:02       9 阅读
  5. db2实例的环境变量

    2024-06-07 07:46:02       6 阅读
  6. Ansible——command 模块

    2024-06-07 07:46:02       11 阅读
  7. 简述浏览器和 Node.js 中的事件循环 ?

    2024-06-07 07:46:02       10 阅读
  8. 统计每天某个时间范围内得 数据状态

    2024-06-07 07:46:02       8 阅读
  9. 45-4 护网溯源 - 溯源相关思路

    2024-06-07 07:46:02       7 阅读
  10. http和websocket区别

    2024-06-07 07:46:02       8 阅读
  11. 前端面试题日常练-day56 【面试题】

    2024-06-07 07:46:02       9 阅读
  12. PostgreSQL中有没有类似Oracle的dba_objects系统视图

    2024-06-07 07:46:02       7 阅读
  13. UDP声音传输:播放的声音有很大的噪音

    2024-06-07 07:46:02       10 阅读
  14. MySQL DBA项目实战系列培训课程【MySQL 8.4最新版】

    2024-06-07 07:46:02       10 阅读