Github地址
1.注釋明晰
/** 來(lái)瘋直播Session */
@property (nonatomic, strong) LFLiveSession *session;
/** 覆蓋層View */
@property (weak, nonatomic) IBOutlet UIView *overlayView;
/** 頭像imageView */
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
/** 標(biāo)題標(biāo)簽 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/** 直播狀態(tài)標(biāo)簽 */
@property (weak, nonatomic) IBOutlet UILabel *liveStatusLabel;
/** 開(kāi)始直播按鈕 */
@property (weak, nonatomic) IBOutlet UIButton *startLiveBtn;
/** 相機(jī)按鈕 */
@property (weak, nonatomic) IBOutlet UIButton *cameraBtn;
/** 美顏按鈕 */
@property (weak, nonatomic) IBOutlet UIButton *beautyBtn;
/** 我是燈泡 []~( ̄▽?zhuān)?~* */
@property (weak, nonatomic) IBOutlet UIButton *lightBtn;
/** 燈泡狀態(tài),默認(rèn)為關(guān)閉 */
@property (nonatomic) AVCaptureTorchMode torchMode;
/** 我是鏡子 []~( ̄▽?zhuān)?~* */
@property (weak, nonatomic) IBOutlet UIButton *mirrorBtn;
2.多情況判斷枚舉
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,
AVAuthorizationStatusRestricted,
AVAuthorizationStatusDenied,
AVAuthorizationStatusAuthorized
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
switch (status) {
case AVAuthorizationStatusNotDetermined: {
// 許可對(duì)話沒(méi)有出現(xiàn)芭概,發(fā)起授權(quán)許可
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.session setRunning:YES];
});
}
}];
break;
}
case AVAuthorizationStatusAuthorized: {
// 已經(jīng)開(kāi)啟授權(quán)虐急,可繼續(xù)
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.session setRunning:YES];
});
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
// 用戶(hù)明確地拒絕授權(quán)虐骑,或者相機(jī)設(shè)備無(wú)法訪問(wèn)
break;
default:
break;
}
4.自定義打印
#define YPLog(FORMAT, ...) fprintf(stderr, "[%s:%d行] %s\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
5.自定義常用語(yǔ)句和常用配置
#define YPNotificationCenter [NSNotificationCenter defaultCenter]
#define YPUserDefaults [NSUserDefaults standardUserDefaults]
#define YPApplication [UIApplication sharedApplication]
#define YPFileManager [NSFileManager defaultManager]
#define YPDevice [UIDevice currentDevice]
/**
*顏色
*/
#define YPBlackColor [UIColor blackColor]
#define YPBlueColor [UIColor blueColor]
#define YPRedColor [UIColor redColor]
#define YPWhiteColor [UIColor whiteColor]
/**
*隨機(jī)色和RGB
*/
/** RGB顏色 */
#define YPColor_RGB(r, g, b) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1.0]
#define YPColor_RGBA(r, g, b, a) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:(a)]
#define YPColor_RGBA_256(r, g, b, a) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:(a) / 255.0]
/** 隨機(jī)色 */
#define YPRandomColor_RGB YPColor_RGB(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define YPRandomColor_RGBA YPColor_RGBA_256(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
6.判斷設(shè)備類(lèi)型
#define iPHone6Plus ([UIScreen mainScreen].bounds.size.height == 736) ? YES : NO
#define iPHone6 ([UIScreen mainScreen].bounds.size.height == 667) ? YES : NO
#define iPHone5 ([UIScreen mainScreen].bounds.size.height == 568) ? YES : NO
#define iPHone4 ([UIScreen mainScreen].bounds.size.height == 480) ? YES : NO
7.屏幕尺寸相關(guān)
/** 屏幕 */
#define YPScreen [UIScreen mainScreen]
/** 屏幕寬度 */
#define YPScreenW [UIScreen mainScreen].bounds.size.width
/** 屏幕高度 */
#define YPScreenH [UIScreen mainScreen].bounds.size.height
/** 屏幕bounds */
#define YPScreenBounds [UIScreen mainScreen].bounds
/** 屏幕伸縮度(Retina時(shí)值為2,非Retina值為1)*/
#define YPScreenScale [UIScreen mainScreen].scale
8.系統(tǒng)其它高度設(shè)置
/** 系統(tǒng)狀態(tài)欄高度 */
UIKIT_EXTERN CGFloat const kAppStatusBarHeight;
/** 系統(tǒng)導(dǎo)航欄高度 */
UIKIT_EXTERN CGFloat const kAppNavigationBarHeight;
/** 系統(tǒng)tabbar高度 */
UIKIT_EXTERN CGFloat const kAppTabBarHeight;
9.善于使用Layer
// containerView
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, YPScreenW, self.containerView.height) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = CGRectMake(0, 0, YPScreenW, self.containerView.height);
maskLayer.path = maskPath.CGPath;
self.containerView.layer.mask = maskLayer;
10.善于使用分類(lèi)擴(kuò)展方法
// 我要直播按鈕
[[_phoneLiveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
// 跳轉(zhuǎn)到手機(jī)直播控制器
[self.navigationController pushViewController:[YPPhoneLiveViewController controller] animated:YES];
}];
11.攔截push事件
/**
* 可以在這個(gè)方法中攔截所有push進(jìn)來(lái)的控制器
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count > 0) { // 如果push進(jìn)來(lái)的不是第一個(gè)控制器
if ([viewController isKindOfClass:NSClassFromString(@"YPBilibiliWebViewController")]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button sizeToFit];
[button setTitleColor:YPMainColor forState:UIControlStateNormal];
[button setTitleColor:YPMainColor forState:UIControlStateHighlighted];
@weakify(self);
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
[self popViewControllerAnimated:YES];
}];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
// 隱藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
}
// 這句super的push要放在后面, 讓viewController可以覆蓋上面設(shè)置的leftBarButtonItem
[super pushViewController:viewController animated:animated];
}
12.善于封裝方法
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
self.tabBar.tintColor = YPMainColor;
[self addChildVc:[YPHomeController controller] andTitle:@"首頁(yè)" andImage:@"home_home_tab" andSelectedImage:@"home_home_tab_s"];
[self addChildVc:[YPProfileViewController controller] andTitle:@"我的" andImage:@"home_mine_tab" andSelectedImage:@"home_mine_tab_s"];
}
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
#pragma mark - Private
- (void)addChildVc:(UIViewController*)childVc andTitle:(NSString*)title andImage:(NSString*)image andSelectedImage:(NSString*)selectedImage
{
childVc.tabBarItem.title = title;
childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
YPNavigationController* nav = [[YPNavigationController alloc] initWithRootViewController:childVc];
[self addChildViewController:nav];
}
13.善于封裝ViewDidLoad中的工作
- (void)viewDidLoad {
[super viewDidLoad];
// UI
[self createUI];
// Data
[self loadData];
}
14.不避諱使用通知换途,該用則用.用則有良好封裝
[[YPNotificationCenter rac_addObserverForName:kCycleBannerWillBeginDraggingNotification object:nil] subscribeNext:^(id x) {
@strongify(self);
self.contentTableView.scrollEnabled = NO;
}];
15.將Xib約束拉出來(lái)掀潮,代碼中靈活使用
_coverImageViewHeightCons.constant = height;
[self layoutIfNeeded];