User Interface
iOS系统架构层次图
-CocoaTouch
UI相关
-媒体层(Media)
音频、视频、图形、动画
-核心服务层(Core services)
内存、网络、文件、线程
-核心操作系统系统层(Core OS)
与硬件交互
app启动原理 main入口 —> UIApplicationMain (设置当前应用程序的代理) —> UIApplication(循环) —> 结束
常见文件
1.info.Plist
默认创建
自定义plist文件不能与之重名
常见属性:
Bundle display name(CFBundleDisplayName)-程序安装后显示的名称,限制在10-12个字符,如果超出,将被显示缩写名称
Bundle version(CFBundleVersion)-应用程序的版本号,每次往App Store上发布一个新版本时,需要增加这个版本号
1.1.3
Bundle identifier(CFBundleIdentifier)-项目的唯一标识,部署到真机时用到
2.全局头文件 .pch文件
宏隔离的使用
#ifdef DEBUG
#define Log(...) NSLog(__VA_ARGS__)
#else
#define Log(...) /* */
#endif
iOS程序中的四大对象
《1》[UIApplication]
单例类,[UIApplication shareApplication]
应用级别的操作权限
比如:
设置联网状态可见
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;
iOS7.0之后需要修改info.plist文件中的配置,才可以让UIApplication获取控制权限,如果不修改,则是由视图控制器控制的
如果使用视图控制器进行控制的话,可以通过重写两个方法:
// These methods control the attributes of the status bar when this view controller is shown. They can be overridden in view controller subclasses to return the desired status bar attributes.
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to NO
强大的OpenURL方法:- (BOOL)openURL:(NSURL*)url
1.打开网页
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://www.baidu.com"]];
2.打电话
tel:// telprompt://
3.发短信
sms://
4.发邮件
mailto://
5.faceTime
facetime://
进入其他应用,等等
使用@“urlb://com.baicu.B”进入其他应用
《2》[AppDelegate]
应用生命周期
所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者锁屏会导致app进入后台甚至被终止
还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理
来处理这些系统事件
delegate可处理的事件包括:
// app接收到内存警告时调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// app进入后台时调用(比如按了home键)
- (void)applicationDidEnterBackground:(UIApplication *)application;
// app启动完毕时调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
int UIApplicationMain (
int argc,
char * _Nonnull argv[],
NSString *principalClassName,
NSString *delegateClassName
);
参数说明:
main函数中执行了一个UIApplicationMain这个函数
UIApplicationMain函数讲解:
int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
argc、argv:直接传递给UIApplicationMain进行相关处理即可
principalClassName:指定应用程序类名(app的象征),该类必须是UIApplication(或子类)。如果为nil,则用UIApplication类作为默认值
delegateClassName:指定应用程序的代理类,该类必须遵守UIApplicationDelegate协议
UIApplicationMain函数会根据principalClassName创建UIApplication对象,根据delegateClassName创建一个delegate对象,并将该delegate对 象赋值给UIApplication对象中的delegate属性
接着会建立应用程序的Main Runloop(事件循环),进行事件的处理(首先会在程序完毕后调用delegate对象的 application:didFinishLaunchingWithOptions:方法)
程序正常退出时UIApplicationMain函数才返回
《3》[UIWindow]
UIView的子类,一般情况下,一个app只有一个UIWindow
如果没有window,程序将看不到任何东西
AppDelegate - window - UIViewController - UIView
320 * 480
320 * 568
375 * 667
414 * 736
《4》[UIViewController]
用来存放UIView的容器
控件分类
1.展示类
2.响应类
3.交互类
控件1---UIButton
UIButton常用基本属性和方法
UIButton
第一、UIButton的定义
UIButton *button=[[UIButton buttonWithType:(UIButtonType);
能够定义的button类型有以下6种
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // 自定义风格
UIButtonTypeSystem // 标准系统按钮
UIButtonTypeDetailDisclosure, //蓝色小箭头按钮
UIButtonTypeInfoLight, // 亮色感叹号
UIButtonTypeInfoDark, // 暗色感叹号
UIButtonTypeContactAdd, // 十字加号按钮
UIButtonTypeRoundedRect = UIButtonTypeSystem,
// 已经弃用 UIButtonTypeSystem 替代
};
第二、设置frame
button.frame = CGRectMake(20, 20, 280, 40);
第三、button背景色
button.backgroundColor = [UIColor clearColor];
第四、state状态
forState:参数的作用是定义按钮的文字或图片在何种状态下才会显现
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0, // 常规状态显现
UIControlStateHighlighted = 1 << 0, // 高亮状态显现
UIControlStateDisabled = 1 << 1, // 禁用的状态才会显现
UIControlStateSelected = 1 << 2, // 选中状态
UIControlStateApplication = 0x00FF0000, // 当应用程序标志时
UIControlStateReserved = 0xFF000000 //为内部框架预留
// 后两个可以不管他
};
// 设置是否可用
@property(nonatomic,getter=isEnabled)BOOL enabled;
// 设置选中状态
@property(nonatomic,getter=isSelected)BOOL selected;
// 设置高亮状态
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
第五 、设置button填充图片和背景图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
第六、设置button标题和标题颜色
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//拓展知识 (十六进制颜色码转UIColor strtoul()函数的使用)
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
第七、添加或删除事件处理
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
iOS UIButton事件:
UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents
通知所有事件。
第九、 设置按钮内部图片间距和标题间距(重写UIButton的两个方法)
- (CGRect)imageRectForContentRect:(CGRect)contentRect
- - (CGRect)titleRectForContentRect:(CGRect)contentRect