系统相关
系统相关
系统版本
[[[UIDevice currentDevice] systemVersion] floatValue] > 6.0;
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#ifdef __IPHONE_9_0
新特性
#if __has_feature(objc_arc)
是否在前台
[UIApplication sharedApplication].applicationState == UIApplicationStateActive;
判断类是否存在
Class target = NSClassFromString(@"NSString");
if (target) {
// do something
} else {
// do something
}
if([Class class]) {
} else {
}
判断接口是否存在
cocoa 函数
[instance respondsToSelector:@selector(selector)]
[Class instancesRespondToSelector:@selector(selector)];
其他
C 函数
if (function != NULL) {
} else {
}
OC 基础数据类型判断
strcmp([obj objCType], @encode(double)) == 0;
strcmp([obj objCType], @encode(unsigned long long)) == 0
int a = 100;
id judge = @(a);
if (strcmp([judge objCType], @encode(int)) == 0) {
NSLoge(@"type = int");
}
UI
Window
获取主 Window
NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows) {
BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
BOOL windowIsVisible = !window.hidden && window.alpha > 0;
BOOL windowLevelSupported = (window.windowLevel >= UIWindowLevelNormal && window.windowLevel <= self.maxSupportedWindowLevel);
BOOL windowKeyWindow = window.isKeyWindow;
if(windowOnMainScreen && windowIsVisible && windowLevelSupported && windowKeyWindow) {
return window;
}
}
View 及其子类相关
判断是否点击到某个 subView
- (void)viewDidLoad {
[self.view addSubview:_testView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.view];
if ([_testView.layer.presentationLayer hitTest:point]) {
_testView.backgroundColor = [UIColor colorWithRed:(arc4random()%255)/255.0 green:(arc4random()%255)/255.0 blue:(arc4random()%255)/255.0 alpha:1];
} else {
[CATransaction begin];
[CATransaction setAnimationDuration:10.0];
_testView.center = point;
[CATransaction commit];
}
}
参考文章