iOS纯手写UI小例子

在iOS开发中,纯手写UI(不使用Interface Builder,简称IB)是一种常见的技术实践,尤其对于提升代码理解和控制力非常有帮助。本教程通过一些基础的小例子,旨在帮助新手掌握如何在iOS应用中手工创建用户界面。我们将讨论以下几个关键知识点: 1. **Auto Layout**:在没有IB的情况下,我们需要手动实现Auto Layout来确定视图的位置和大小。这涉及到NSLayoutConstraint的使用,通过设置约束(如leading, trailing, top, bottom, width, height等)确保视图在不同屏幕尺寸和设备方向下的适配。 2. **UIButton**: UIButton是iOS中用于交互的基础元素。创建一个UIButton需要指定其类型(如System, Custom等),设置标题、图像、选中状态以及添加点击事件处理方法。例如: ```swift let button = UIButton(type: .system) button.setTitle("点击我", for: .normal) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) ```这里`buttonTapped`是点击事件的处理函数。 3. **UIScrollView**: UIScrollView允许用户在内容超过屏幕范围时滚动查看。配置UIScrollView需要设置contentSize,添加子视图,并可能需要调整contentInset和contentOffset。例如: ```swift let scrollView = UIScrollView() scrollView.contentSize = CGSize(width: view.bounds.width * 3, height: view.bounds.height) //添加子视图... ``` 4. **手势识别(Gesture Recognizer)**: iOS提供了多种手势识别器,如UITapGestureRecognizer,UILongPressGestureRecognizer等。我们可以为视图添加手势识别器来响应用户的特定操作。例如,为按钮添加长按手势: ```swift let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressDetected)) button.addGestureRecognizer(longPressGesture) ```在这里,`longPressDetected`是长按事件的处理函数。 5. **响应链(Responder Chain)**: iOS中的事件传递是通过响应链来实现的。理解并正确设置视图的isUserInteractionEnabled属性以及理解responder对象间的事件传递顺序是至关重要的。 6. **布局和更新视图**:在手写代码时,需要关注视图的layoutSubviews方法,这通常用于在视图大小或位置变化后进行重新布局。例如: ```swift override func layoutSubviews() { super.layoutSubviews() //更新子视图的位置和大小... } ``` 7. **颜色与字体**:在代码中设置颜色和字体,如: ```swift let label = UILabel() label.textColor = UIColor.blue label.font = UIFont.systemFont(ofSize: 18) ``` 8. **事件传递与委托模式**:许多控件支持委托协议,如UITableViewDataSource和UITableViewDelegate,通过遵循这些协议,我们可以定制视图的行为,如填充表格数据、响应用户选择等。 9. **动画效果**:通过Core Animation或UIView的动画块可以实现平滑的过渡效果。例如,淡入淡出一个视图: ```swift UIView.animate(withDuration: 0.3) { myView.alpha = 1.0 } ```这个小例子集合将涵盖以上这些基本概念,通过实践加深理解,有助于开发者逐步掌握iOS手写UI的技巧。不断练习和探索,你将能够创建出更加复杂且动态的用户界面。
folder
iOS纯手写UI小例子 预估大小:240个文件
file
main.m 334B
file
CustomCell.h 338B
file
Default.png 6KB
file
CustomCell.m 4KB
file
headImage.jpg 12KB
file
AppDelegate.h 362B
file
AppDelegate.m 2KB
file
Default-568h@2x.png 18KB
file
LUITableViewCustomCell-Info.plist 1KB
file
.DS_Store 6KB
file
Default@2x.png 16KB
file
ViewController.h 213B
file
ViewController.m 4KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
file
LUITableViewCustomCell-Prefix.pch 327B
file
.DS_Store 6KB
file
contents.xcworkspacedata 167B
file
UserInterfaceState.xcuserstate 14KB
file
WorkspaceSettings.xcsettings 332B
file
UserInterfaceState.xcuserstate 13KB
file
UserInterfaceState.xcuserstate 7KB
file
project.pbxproj 14KB
file
xcschememanagement.plist 494B
file
LUITableViewCustomCell.xcscheme 3KB
file
xcschememanagement.plist 494B
file
LUITableViewCustomCell.xcscheme 3KB
file
Breakpoints.xcbkptlist 91B
file
xcschememanagement.plist 494B
file
LUITableViewCustomCell.xcscheme 3KB
file
.DS_Store 6KB
file
main.m 334B
file
Default.png 6KB
file
LUITableView-Prefix.pch 327B
file
AppDelegate.h 389B
file
AppDelegate.m 2KB
file
Default-568h@2x.png 18KB
file
LUITableView-Info.plist 1KB
file
Default@2x.png 16KB
file
ViewController.h 257B
file
ViewController.m 3KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
file
contents.xcworkspacedata 157B
file
UserInterfaceState.xcuserstate 19KB
file
UserInterfaceState.xcuserstate 11KB
file
UserInterfaceState.xcuserstate 12KB
file
project.pbxproj 13KB
file
xcschememanagement.plist 484B
file
LUITableView.xcscheme 3KB
file
xcschememanagement.plist 484B
file
LUITableView.xcscheme 3KB
file
xcschememanagement.plist 484B
file
LUITableView.xcscheme 3KB
file
.DS_Store 6KB
file
.DS_Store 6KB
file
main.m 345B
file
MMViewController.m 979B
file
Default.png 6KB
file
MMViewController.h 228B
file
AppDelegate.h 286B
file
AppDelegate.m 4KB
file
Default-568h@2x.png 18KB
file
LLViewController.m 983B
file
LUITabBarViewController-Prefix.pch 349B
file
LLViewController.h 228B
file
LUITabBarViewController-Info.plist 1KB
file
Default@2x.png 16KB
file
InfoPlist.strings 45B
file
Demo璇存槑.txt 445B
file
.DS_Store 6KB
file
contents.xcworkspacedata 168B
file
UserInterfaceState.xcuserstate 7KB
file
UserInterfaceState.xcuserstate 16KB
file
UserInterfaceState.xcuserstate 7KB
file
project.pbxproj 13KB
file
xcschememanagement.plist 495B
file
LUITabBarViewController.xcscheme 3KB
file
xcschememanagement.plist 495B
file
LUITabBarViewController.xcscheme 3KB
file
xcschememanagement.plist 495B
file
LUITabBarViewController.xcscheme 3KB
file
.DS_Store 6KB
file
main.m 329B
file
Default.png 6KB
file
bg_for_stretch.png 3KB
file
AppDelegate.h 270B
file
AppDelegate.m 4KB
file
Default-568h@2x.png 18KB
file
LWindow-Prefix.pch 317B
file
.DS_Store 6KB
file
LWindow-Info.plist 1KB
file
Default@2x.png 16KB
file
InfoPlist.strings 45B
file
Demo璇存槑.txt 830B
file
.DS_Store 6KB
file
contents.xcworkspacedata 152B
file
UserInterfaceState.xcuserstate 11KB
file
UserInterfaceState.xcuserstate 13KB
file
project.pbxproj 12KB
file
xcschememanagement.plist 479B
file
LWindow.xcscheme 3KB
file
xcschememanagement.plist 479B
file
LWindow.xcscheme 3KB
file
.DS_Store 6KB
file
main.m 335B
file
Default.png 6KB
file
AppDelegate.h 363B
file
AppDelegate.m 2KB
file
Default-568h@2x.png 18KB
file
LUIScrollView-Prefix.pch 329B
file
Default@2x.png 16KB
file
ViewController.h 214B
file
LUIScrollView-Info.plist 1KB
file
ViewController.m 5KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
file
Demo璇存槑.txt 389B
file
contents.xcworkspacedata 158B
file
UserInterfaceState.xcuserstate 13KB
file
project.pbxproj 13KB
file
xcschememanagement.plist 485B
file
LUIScrollView.xcscheme 3KB
file
Demo璇存槑.txt 787B
file
main.m 329B
file
Default.png 6KB
file
AppDelegate.h 270B
file
AppDelegate.m 4KB
file
Default-568h@2x.png 18KB
file
LUIView-Prefix.pch 317B
file
LUIView-Info.plist 1KB
file
Default@2x.png 16KB
file
InfoPlist.strings 45B
file
.DS_Store 6KB
file
contents.xcworkspacedata 152B
file
UserInterfaceState.xcuserstate 11KB
file
project.pbxproj 12KB
file
LUIView.xcscheme 3KB
file
xcschememanagement.plist 479B
file
.DS_Store 6KB
file
.DS_Store 15KB
file
.DS_Store 6KB
file
Demo璇存槑.txt 375B
file
contents.xcworkspacedata 170B
file
UserInterfaceState.xcuserstate 10KB
file
UserInterfaceState.xcuserstate 11KB
file
project.pbxproj 13KB
file
xcschememanagement.plist 497B
file
LUIViewUIImageViewUILabel.xcscheme 3KB
file
xcschememanagement.plist 497B
file
LUIViewUIImageViewUILabel.xcscheme 3KB
file
.DS_Store 6KB
file
LUIViewUIImageViewUILabel-Info.plist 1KB
file
main.m 347B
file
Default.png 6KB
file
LUIViewUIImageViewUILabel-Prefix.pch 353B
file
AppDelegate.h 375B
file
AppDelegate.m 2KB
file
Default-568h@2x.png 18KB
file
Default@2x.png 16KB
file
ViewController.h 226B
file
ViewController.m 3KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
file
.DS_Store 6KB
file
Demo璇存槑.txt 442B
file
.DS_Store 6KB
file
main.m 331B
file
MMViewController.m 2KB
file
Default.png 6KB
file
MMViewController.h 214B
file
AppDelegate.h 359B
file
AppDelegate.m 3KB
file
Default-568h@2x.png 18KB
file
LLViewController.m 5KB
file
LLViewController.h 272B
file
VCNavDemo-Prefix.pch 321B
file
Default@2x.png 16KB
file
VCNavDemo-Info.plist 1KB
file
ViewController.h 210B
file
ViewController.m 3KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
file
contents.xcworkspacedata 154B
file
UserInterfaceState.xcuserstate 15KB
file
UserInterfaceState.xcuserstate 14KB
file
project.pbxproj 14KB
file
VCNavDemo.xcscheme 3KB
file
xcschememanagement.plist 481B
file
VCNavDemo.xcscheme 3KB
file
xcschememanagement.plist 481B
file
Breakpoints.xcbkptlist 91B
file
.DS_Store 6KB
file
contents.xcworkspacedata 162B
file
UserInterfaceState.xcuserstate 10KB
file
UserInterfaceState.xcuserstate 12KB
file
project.pbxproj 12KB
file
xcschememanagement.plist 489B
file
LUIViewController.xcscheme 3KB
file
xcschememanagement.plist 489B
file
LUIViewController.xcscheme 3KB
file
Demo璇存槑.txt 739B
file
.DS_Store 6KB
file
main.m 339B
file
Default.png 6KB
file
LUIViewController-Prefix.pch 337B
file
AppDelegate.h 280B
file
AppDelegate.m 5KB
file
Default-568h@2x.png 18KB
file
MyFirstViewController.h 232B
file
MyFirstViewController.m 790B
file
LUIViewController-Info.plist 1KB
file
Default@2x.png 16KB
file
InfoPlist.strings 45B
file
.DS_Store 6KB
file
Demo璇存槑.txt 378B
file
contents.xcworkspacedata 175B
file
UserInterfaceState.xcuserstate 12KB
file
UserInterfaceState.xcuserstate 12KB
file
UserInterfaceState.xcuserstate 7KB
file
project.pbxproj 13KB
file
LUIButtonUITextFieldUITextView.xcscheme 3KB
file
xcschememanagement.plist 502B
file
LUIButtonUITextFieldUITextView.xcscheme 3KB
file
xcschememanagement.plist 502B
file
LUIButtonUITextFieldUITextView.xcscheme 3KB
file
xcschememanagement.plist 502B
file
.DS_Store 6KB
file
main.m 352B
file
LUIButtonUITextFieldUITextView-Info.plist 1KB
file
Default.png 6KB
file
AppDelegate.h 380B
file
AppDelegate.m 2KB
file
Default-568h@2x.png 18KB
file
LUIButtonUITextFieldUITextView-Prefix.pch 363B
file
Default@2x.png 16KB
file
ViewController.h 231B
file
ViewController.m 8KB
file
ViewController.xib 6KB
file
InfoPlist.strings 45B
...
zip 文件大小:458.32KB