UI的学习(一)

UI的学习(一)

文章目录

  • UI的学习(一)
    • UIlabel
    • UIButton
      • UIButton的两种形式
      • UIButton的事件触发
    • UIView
      • 多个视图之间的关系
    • UIWindow
    • UIViewController
      • 一个视图推出另一个视图
    • 定时器和视图移动
    • UISwitch
    • UISlider和UIProgressSlid
    • 步进器与分栏控制器
    • UITextField
    • UIScrollView
      • 有关实现它的代理函数
    • UIAlertController和UIActivityIndicatorView

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]; //显示labellabel.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 { //创建普通的UIButtonUIButton *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 { //创建普通的UIButtonUIButton *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:UIAlertActionStyleCancelhandler:nil];[_alertController addAction:cancelAction];UIAlertAction *newAction = [UIAlertAction actionWithTitle:@"新的"style:UIAlertActionStyleDefaulthandler:nil];[_alertController addAction:newAction];// 添加一个"确认"按钮UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleDefaulthandler:^(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学习的一个简单的总结。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/22050.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

个人笔记-随意记录

常见问题&#xff1f; 1.linux重启服务 端口被占用如何解决&#xff1f; 查看某个端口被占用的进程 netstat -tulnp | grep :23454 强制杀死进程 kill -9 1776 重启服务即可

解决Chat打开时Unable to load conversation 的问题

在开梯子的情况下打开chat依然很卡&#xff0c;这里选择edge的浏览器无痕模式&#xff08;新建InPrivate窗口&#xff09;&#xff0c;在无痕窗口下打开chat就可以了。

python ---requests

python包管理工具 pip 若发现报错&#xff0c;则可以通过 -i 命令指定软件源 requests库安装 通过 pip &#xff0c;如上 或通过 pycharm 搜索 requests &#xff0c;并安装即可 下载成功的证明 requests库使用 模拟 http 重要参数如下 如何模拟发包 支持模拟各种 http meth…

SpringMVC:消息转换器

1. HttpMessageConvertor 简介 HttpMessageConverter是Spring MVC中非常重要的一个接口。翻译为&#xff1a;HTTP消息转换器。该接口下提供了很多实现类&#xff0c;不同的实现类有不同的转换方式。 转换器 如上图所示&#xff1a;HttpMessageConverter接口的可以将请求协议转…

基于ESP32-S3芯片的通用型无线模组方案,启明云端乐鑫一级代理商

随着物联网技术的飞速发展&#xff0c;智能设备正以前所未有的速度进入到我们的日常生活中&#xff0c;AIoT&#xff08;人工智能物联网&#xff09;已成为智能家居、智能设备、智能安防等领域的核心技术。 作为乐鑫一级代理商&#xff0c;基于ESP32-S3芯片&#xff0c;启明云…

科技云报道:走出“实验室”,GenAI迎来关键拐点

科技云报道原创。 对传统产业来说&#xff0c;GenAI是一场“哥白尼式的革命”&#xff0c;它改变了传统的业务模式&#xff0c;开启了人类与AI合作的新纪元。基于AI助手和大语言模型&#xff0c;企业能够实现智能运营的目标。 如果说&#xff0c;2022年是AI大模型元年&#x…

【全开源】Java AI绘画MJ绘画源码小程序APP公众号源码AI绘图

&#x1f3a8; 探索AI绘画的奥秘 一、引言&#xff1a;AI绘画的魅力 &#x1f308; 在这个数字化飞速发展的时代&#xff0c;AI绘画已经不再是遥不可及的梦想。通过源码小程序&#xff0c;我们可以轻松探索AI绘画的奥秘&#xff0c;感受科技与艺术的完美结合。今天&#xff0…

电脑误删除文件如何恢复?几种常用的数据恢复方法分享!

处理电脑文件时误删是大部分电脑用户可能都会面临的一个问题。如果是比较重要的文件&#xff0c;很多用户就会开始心慌&#xff0c;不知道如何是好。那么&#xff0c;电脑怎么恢复删除的文件呢&#xff1f; 其实方法很简单&#xff0c;下面小编就给大家分享几种常用的数据恢复方…

react-学习基础偏

1.新建文件夹 2.vscode引入这个文件夹 3.打开vscode终端 执行命令 npx create-react-app react-basic 创建基本项目&#xff08;react-basic项目文件夹名&#xff09; 4.进入到这个文件夹 可用的一些命令 这就算启动成功 5. 这是项目的核心包 渲染流程

java自学阶段二:JavaWeb开发06(mybatis学习)

目录&#xff1a; 学习目标mybatis的基础用法&#xff08;新增、删除、修改、查询&#xff09; 一&#xff1a;学习目标&#xff1a; 1&#xff09;了解mybatis的基础概念&#xff1b; 2&#xff09;学会mybatis的基础用法&#xff1b; 二、mybatis的基础概念&#xff1a; M…

手把手教你改进YOLOv8小目标检测(多尺度特征融合iAFF)

1,YOLOv8改进策略指南 YOLOv8是目标检测领域中一个重要的模型,它在YOLO系列的基础上进行了进一步的改进和优化。 根据搜索结果,YOLOv8的一些改进策略包括: 注意力机制的增加:通过引入注意力机制,可以提高模型对目标特征的捕捉能力,从而提升检测性能2369。 卷积和Block的…

Unity Obi Rope失效

文章目录 前言一、WebGL端Obi Rope失效二、Obi Rope 固定不牢三、使用Obi后卡顿总结 前言 Obi 是一款基于粒子的高级物理引擎&#xff0c;可模拟各种可变形材料的行为。 使用 Obi Rope&#xff0c;你可以在几秒内创建绳索和杆子&#xff0c;同时完全控制它们的形状和行为&…

scipy.io.loadmat加载.mat文件,出现KeyError: ‘xxx‘

源代码&#xff1a; input_image loadmat(rC:\Users\admin\Downloads\Indian_Pines\SVM/aa.mat)[aa] #影像图 错误显示&#xff1a; 解决方法&#xff1a; 因为loadmat函数读取出来的高光谱数据是dict格式的所以需要定位才能进行后续操作&#xff0c;定位通常是通过列名&a…

运筹说 第116期 | 算法介绍之排队论

在这个快节奏的时代&#xff0c;无论是线上购物、线下服务&#xff0c;还是工业生产&#xff0c;我们都不可避免地与“排队”打交道。今天小编将带你一起探索利用Python和MATLAB这两种编程工具&#xff0c;来求解排队论中的常见模型和排队优化问题。我们将从排队论的基础模型开…

U盘杀毒是否会导致文件丢失?误删文件如何恢复?

在数字化时代&#xff0c;U盘作为便携的数据存储设备&#xff0c;广泛应用于我们的日常生活与工作中。然而&#xff0c;随着网络环境的复杂化&#xff0c;U盘也时常成为病毒传播的媒介。因此&#xff0c;对U盘进行杀毒成为保护数据安全的重要步骤。但许多用户担心&#xff0c;给…

centos安装vscode的教程

centos安装vscode的教程 步骤一&#xff1a;打开vscode官网找到历史版本 历史版本链接 步骤二&#xff1a;找到文件下载的位置 在命令行中输入&#xff08;稍等片刻即可打开&#xff09;&#xff1a; /usr/share/code/bin/code关闭vscode后&#xff0c;可在应用程序----编程…

10Linux 进程管理学习笔记

Linux 进程管理 目录 文章目录 Linux 进程管理一.进程1.显示当前进程状态(ps)进程树(pstree)1.1实时显示进程信息(top)顶部概览信息&#xff1a;CPU 状态&#xff1a;内存状态&#xff1a;进程信息表头&#xff1a;进程列表&#xff1a;1.2(htop) 2.终止进程(kill)2.1通过名称…

Jetson Orin Nano安装使用;cuda、pytorch安装;yolo使用

参考: https://blog.csdn.net/q839039228/article/details/126278528 1、jtop工具安装 安装jtop资源查看: sudo apt update sudo apt upgrade安装: sudo apt install curl nanosudo pip install jetson-stats查看: jtop 按2查看GPU 按3查看CPU 2、JetPack套件 参…

修改云主机配置 - 内存增容

文章目录 一、修改云主机配置缘由二、修改云主机配置步骤1、查看云主机概述2、查看master云主机3、更改master云主机配置4、查看master云主机 三、使用Spark Shell玩Saprk SQL1、启动HDFS服务2、启动Spark集群3、启动集群模式Spark Shell4、读取文件生成单例数据帧5、将单列数据…

C语言----字符串、字符数组

一、定义 C语言中的字符串是以字符数组的形态存在的 在C语言中&#xff0c;没有字符串类型&#xff0c;字符串实际上是使用空字符\0结尾的一维字符数组。因此&#xff0c;\0是用于标记字符串的结束。 二 、如何创建字符串&#xff1f; 1.通过字符数组来创建字符串&#xff0…