current position:Home>iOSUIKit and Swift | Youth Training Notes

iOSUIKit and Swift | Youth Training Notes

2022-08-06 18:17:4899 is always one point away

这是我参与「第四届青训营 -iOS场」笔记创作活动的的第4篇笔记

UIKit

UIApplication

  • app入口
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);
}
复制代码
  • 单例模式,一个应用程序只会有一个
  • 应用实例,Responsible for monitoring application-level time
  • AppDelegate,Application-level time proxy,管理应用程序的生命周期、通知事件、外部调用

different events are taking place,Call different callback methods

App生命周期

  • Not Running,未运行
  • Inactive,前台非活动状态,处于前台,但是不能接受事件处理
  • Active,前台活动状态,能接受事件处理
  • Background,后台状态,If there is executable code, execute it,否则挂起
  • Suspended,挂起状态,如果内存不足,会被杀死 image.png

视图

  • UIKit,Provides a base view class library
  • UIView,The root class for all views
  1. 实际上是CALayer的封装
  2. UIView,Handle layout and touch events;CALayer,负责绘图、动画、变化
  3. For separation of duties,但是UIView灵活性差,需要进入CALayer层进行处理
  • UIViewController,Base class for all view controllers,Responsible for view display、界面跳转
  1. 生命周期 init -> loadView -> viweDidLoad -> viewWillAppear -> viewDidAppear -> 开始退出 -> viewWillDisappear -> viewDidDisappear -> viewWillUnload -> viewDidUnload -> dealloc
  2. init进行数据初始化,不要出现创建view
  3. loadView进行view创建和初始化
  4. viweDidLoad,附加的view和空间
  5. viewWillAppear,显示前处理,Keyboard popup or special animation
  6. viewDidAppear,Action after showing animation

Graphic geometry

  • UIView重要属性,frame,bounds,center,Just an access interface,Ultimately it's changeCALayer的frame、bounds、position
  1. frame,The interface size of the frame
  2. bounds,长宽
  3. center,中心
  • 锚点,The center point of the graph movement/把柄
  • 坐标系,一个图层的positionDepends on the parent layer,The Cartesian coordinate system established relative to the parent layer
  • 布局,设置(x, y, height, width),on the interface
  1. 自动布局,Care about constraints between views,Relative position between multiple views
  2. masonry/snapkit
  • zPosition,前后位置,Just put the draw order on top,实际的viewThe layer is still below,Then the actual touch is still based onadd顺序,需要调用bringSubviewToFront
  • 仿射变化,利用transform属性,Then rotate in two-dimensional space、缩放、平移 image.png
  • 3D变化,Use the individual elements of the matrix to vary,增加对z轴的变化 image.png
  • 视觉效果,圆角、边框、阴影,修改layer的属性;Shadows affect subviews,Borders don't
  1. cornerRadius
  2. borderWidth
  3. shadow

Swift

优势

  • nil值
  1. OC,nilA pointer representing an empty object
  2. swift,有optional的概念,如果是nil,不会进行任何处理
  • 类型安全
  1. OC implicit transformation,会导致精度缺失
  2. swift Shown transformation,Need to point out when converting
  • 运行快

基础知识

  • 变量定义,There is type automatic inference
// 不可变
let a = 10
// 可变
var b = 20
// 数组
var names: [String] = ["Tom"]
// 字典
var classes: [String : String] = ["class" : "Tom"]
// 合集,无序set
var id: Set<String> = ["1", "2", "3"]
复制代码
  • 可选值
var name: String?
复制代码
  • 循环
for idx in 0..<10 {}
while(condition) {}
复制代码
  • 函数
// inout Indicates that the parameter is passed by reference,The address is passed in when calling
// 参数前面加 _,Indicates that the parameter name can be omitted
func funcName(argName: aryType, ...) -> returnType {code...}
复制代码
class ClassName: FatherClass {}
复制代码
  • 类的扩展,增加方法
extension ClassName {}
复制代码
  • 协议
protocol ProrocalName: NSObjectProtocal {}
// 定义变量
var name: String {get set}
// 定义函数,不用实现
func funcName(argName: aryType, ...) -> returnType
复制代码
  • Protocol modification
  1. mutating,This method can change the variables in the protocol
  2. required,Implements the designated initializer,Meet the initialization requirements of the protocol,子类必须要实现
  3. required & override,Parent classes and protocols have the same constructor,And the constructor of the parent class is uselessrequired修饰,Then the overridden subclass constructor is usedrequired override修饰;If subclass takesfinal,就不需要用required,finalIndicates that it will not be inherited by any class,There will be no question of whether subclasses need to inherit
  • 泛型
// 函数
func swap<T>(_  a: **inout** T, _ b: **inout** T) {
    let temp = a
    a = b
    b = temp
}
// 类
class ClassName<Element> {}
复制代码
  • 闭包
  1. Use context to infer formal parameter and return value types {(s1: String, s2: String) -> Bool in} -> {s1, s2 in return s1 > s2}
  2. 单表达式闭包隐式返回 {arg1, arg2 in arg1 > arg2} 类似java的lmabda表达式,There is only a single statement in the closure,直接写
  3. 简写实际参数名 {arg1, arg2 in arg1 > arg2} -> { 0 > 0 > 1}
  4. 简写实际参数名 { 0 > 0 > 1} -> ( > )
  5. 尾随闭包,Passed as a function parameter value,It needs to be written outside the function parameter list
var block: (argType, ...) -> returnType = {arg... in
    //code...
    return ...
}
// 简写
var block: (String) -> String = {
    print($0)
    return $0 + "1232"
}
// sort
let s1 = Student(name: "Tom", score: 300)
let s2 = Student(name: "Anna", score: 100)
let s3 = Student(name: "Aana", score: 100)
var students = [s2, s1, s3]
students = students.sorted {
    if $0.score != $1.score {
        // greater than descending order
        return $0.score > $1.score
    }
    // less than ascending
    return $0.name < $1.name
}
复制代码

swfitUI

  • Just an engineering form,与storyboard是平级的
  • contentView,Manage page layouts
  • PreviewProvider,Declare view preview
  • Set the styles manually

image.png

栈视图

  • Three layout forms
// 垂直
VStack {
}
// 水平
HStack {
}
// 覆盖布局
ZStack {
}
复制代码
  • 嵌套布局
VStack {
    Text("Hello, world!")
        .padding()
    HStack {
        Text("Hello, world!")
            .font(.headline)
            .foregroundColor(Color.red)
            .padding()
        Text("Hello, world!")
            .padding()
    }
}
复制代码

Button

struct CaseView: View {
    @State var change = false
    var body: some View {
        VStack {
            Text(change ? "欢迎关注" : "下次再来")
                .padding()
            Button {
                // 取反change
                self.change.toggle()
            } label: {
                Text("button")
                    .padding()
            }

        }
    }
}
复制代码

页面push跳转

  • 第一个页面
struct ContentView: View {
    var body: some View {
        NavigationView() {
            VStack {
                Text("Hello, world!")
                    .padding()
                HStack {
                    Text("Hello, world!")
                        .font(.headline)
                        .foregroundColor(Color.red)
                        .padding()
                    Text("Hello, world!")
                        .padding()
                }
                NavigationLink(destination: NextView()) {
                    Text("go to next")
                        .foregroundColor(.yellow)
                }
            }
            
        }
    }
}
复制代码

copyright notice
author[99 is always one point away],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061758490921.html

Random recommended