首页 > 随笔档案 > OC使用inline替代宏

OC使用inline替代宏

Publish:


CG_INLINE voidGCDDelay(int64_t delayInSeconds, dispatch_block_t block) {
	dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
	dispatch_after(popTime, dispatch_get_main_queue(), block);
}
CG_INLINE voidGCDOnce(dispatch_once_t onceToken, DISPATCH_NOESCAPE dispatch_block_t block) {
	dispatch_once( & onceToken, block);
}
CG_INLINE CGFloatWIDTH() {
	static CGFloat ScreenWidth;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		ScreenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
	});
	return ScreenWidth;
};
CG_INLINE CGFloatHEIGHT() {
	static CGFloat ScreenHeight;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		ScreenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
	});
	return ScreenHeight;
};
CG_INLINE CGRectSCREEN() {
	static CGRect ScreenRect;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		if (CGRectIsEmpty(ScreenRect) || CGRectIsNull(ScreenRect)) {
			ScreenRect = [UIScreen mainScreen].bounds;
		}
	});
	return ScreenRect;
};
CG_INLINE CGFloatFontScale() {
	static CGFloat FScale;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		FScale = 72.0 / 96.0;
	});
	return FScale;
};
CG_INLINE CGFloatSizeScaleX() {
	static CGFloat autoSizeScaleX;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		autoSizeScaleX = (WIDTH() > 480 ? WIDTH() / 320.0 : 1.0);
	});
	return autoSizeScaleX;
};
CG_INLINE CGFloatSizeScaleY() {
	static CGFloat autoSizeScaleY;
	static dispatch_once_t onceToken;
	GCDOnce(onceToken, ^ {
		autoSizeScaleY = (WIDTH() > 480 ? HEIGHT() / 568.0 : 1.0);
	});
	return autoSizeScaleY;
};
CG_INLINE CGRectCGLFLMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
	CGRect rect;
	rect.origin.x = x * SizeScaleX();
	rect.origin.y = y * SizeScaleY();
	rect.size.width = width * SizeScaleX();
	rect.size.height = height * SizeScaleY();
	return rect;
};
CG_INLINE CGPointCGPointLFLMake(CGFloat x, CGFloat y) {
	CGPoint pointLFL;
	pointLFL.x = x * SizeScaleX();
	pointLFL.y = y * SizeScaleY();
	return pointLFL;
};
CG_INLINE CGSizeCGSizeLFLMake(CGFloat width, CGFloat height) {
	CGSize sizeLFL;
	sizeLFL.width = width * SizeScaleX();
	sizeLFL.height = height * SizeScaleY();
	return sizeLFL;
};

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: levy