1.產生隨機數
int allTextIndex = arc4random_uniform(30)+1;
2.禁止視圖自動布局
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
3.添加單擊事件
UITapGestureRecognizer *userTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickUserPhotoImage)];
[self.userPhotoImageView addGestureRecognizer:userTap];
4.延時加載方法
double delayInSeconds = 1;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
//執行的方法
});
5.簡單彈出框,可直接寫在pch文件中
#define kTipAlert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil] show]
6.通知的簡單使用
//發送登錄成功的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserLoginSuccess" object:nil];
//監聽用戶登錄成功后的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserDetail) name:@"UserLoginSuccess" object:nil];
//移除通知
移除單個通知:[[NSNotificationCenter defaultCenter] removeObserver:self name:@"JPUSHNOTIFICATION" object:self];
移除當前所有通知:[[NSNotificationCenterdefaultCenter]removeObserver:self];
7.獲取當前國家地區
NSString *systemCountryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
8.當有名稱相同的代理協議方法時如何解決?
[WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
比如微信和QQ的代理中有個方法名稱都叫 onResp:
微信:-(void)onResp:(BaseResp*)resp;
QQ: - (void)onResp:(QQBaseResp *)resp;
將參數改成id類型即可
- (void)onResp:(id)resp
9.隱藏狀態欄
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
10.NSTimer的簡單使用
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:@"" repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:UITrackingRunLoopMode];
11..iOS中switch-case的優化用法(http://www.cnblogs.com/easonoutlook/archive/2012/08/16/2642011.html)
之前使用switch-case的時候一直無法使用聲明語句,只能使用調用函數的語句,今天看到了高手使用
其實也就是加一個 { } 即可。
switch (indexPath.row) {
case 0:{
//錢包
ZMWalletViewController *vc = [[ZMWalletViewController alloc] init];
[self.viewController.navigationController pushViewController:vc animated:YES];
break;
}
default:
break;
}
12.使用Masnory 獲取Frame
[self.rightButton.superview layoutIfNeeded];
CGRect frame = self.rightButton.frame;
CGRect frame2 = self.leftButton.frame;
NSLog(@"%@",NSStringFromCGRect(frame));
13.使用UIBezierPath 創建分割線
UIBezierPath *path = [[UIBezierPath alloc] init];
//path創建好后,就可以設置其線寬,顏色等屬性
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(UISCREENWIDTH, 0)];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
CAShapeLayer *shapeLayer=[CAShapeLayer layer];
shapeLayer.path=path.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充顏色
shapeLayer.strokeColor = [ZMColor colorWithRed:210 withGreen:210 withBlue:212 withAlpha:0.45].CGColor;//邊框顏色
shapeLayer.lineWidth = 0.5;
[self.layer addSublayer:shapeLayer];
14.判斷含有某個字符串
//第一種方式
if ([actionSheet.title rangeOfString:@"://"].location == NSNotFound 如果為false,表示含有://字符
//第二種方式
NSString *testStr = @"我是test";
[testStr containsString:@"test"];
15.Library not loaded: 錯誤解決方法
在build phases 改為optional
16.Xcode調試不能停在代碼區域
只要 XCode中 Debug -> Debug Workflow - Always Shaw Disassembly 取消打勾就可以了
17.主線程更新UI
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.tableView.tableHeaderView = weakSelf.headView;
[weakSelf.tableView reloadData];
});
18.xcode 7.0 CAAnimationDelegate 報錯
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
@interface XWFilterTransitionView () <CAAnimationDelegate>
#else
@interface XWFilterTransitionView ()
#endif
19.判斷對象是否為空
- (BOOL)isNull
{
if ([self isEqual:[NSNull null]])
{
return YES;
}
else
{
if ([self isKindOfClass:[NSNull class]])
{
return YES;
}
else
{
if (self==nil)
{
return YES;
}
}
}
if ([self isKindOfClass:[NSString class]]) {
if ([((NSString *)self) isEqualToString:@"(null)"]) {
return YES;
}
}
return NO;
}
20.encodeWithCoder:]: unrecognized selector sent to instance 0x17462d4c0 錯誤
歸檔解檔要遵守NSCoding 協議
21.cocoapods添加了新庫但不想更新之前的庫
pod install --no-repo-update
22.將視圖放到最下面 或最上面
[self.mainView addSubview:imageView];
[self.mainView sendSubviewToBack:imageView]; 底下
[self.mainView bringSubviewToFront :imageView]; 朝上
23.GPUImage err = CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault, coreVideoTextureCache, renderTarget錯誤
Product -> Scheme -> Edit Scheme -> Disabled 這只是在Debug才會報錯,不連接xcode不會報錯。
24.IQKeyboardManager鍵盤庫的簡單使用
// 獲取類庫的單例變量
IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
// 控制整個功能是否啟用
keyboardManager.enable = YES;
// 控制點擊背景是否收起鍵盤
keyboardManager.shouldResignOnTouchOutside = YES;
// 控制鍵盤上的工具條文字顏色是否用戶自定義
keyboardManager.shouldToolbarUsesTextFieldTintColor = YES;
// 有多個輸入框時,可以通過點擊Toolbar 上的“前一個”“后一個”按鈕來實現移動到不同的輸入框
keyboardManager.toolbarManageBehaviour = IQAutoToolbarBySubviews;
// 控制是否顯示鍵盤上的工具條
keyboardManager.enableAutoToolbar = YES;
// 是否顯示占位文字
keyboardManager.shouldShowTextFieldPlaceholder = YES;
// 設置占位文字的字體
keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:17];
// 輸入框距離鍵盤的距離
keyboardManager.keyboardDistanceFromTextField = 10.0f;
keyboardManager.preventShowingBottomBlankSpace = NO;
25.跳轉到評價頁面
NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=你的AppId" ];
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)){
str = [NSString stringWithFormat:@"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=你的AppId&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
26.自動布局 autoLayout
setNeedsLayout:告知頁面需要更新,但是不會立刻開始更新。執行后會立刻調用layoutSubviews。
layoutIfNeeded:告知頁面布局立刻更新。所以一般都會和setNeedsLayout一起使用。如果希望立刻生成新的frame需要調用此方法,利用這點一般布局動畫可以在更新布局后直接使用這個方法讓動畫生效。
layoutSubviews:系統重寫布局 setNeedsUpdateConstraints:告知需要更新約束,但是不會立刻開始
updateConstraintsIfNeeded:告知立刻更新約束
updateConstraints:系統更新約束
27.UIView 使用陰影產生離屏渲染卡頓相關鏈接(http://blog.csdn.net/zixiweimi/article/details/39889623)
[self.view layer].shadowPath =[UIBezierPath bezierPathWithRect:self.mainView.bounds].CGPath;
28.有時候視圖莫名其妙會動畫效果會消失,變的很生硬,在網上找到了一種很巧的辦法,(http://blog.csdn.net/chenyong05314/article/details/50592299?from=singlemessage&isappinstalled=0),這個問題困擾了我一天,希望能幫到大家
大家可以在合適的時間加入這段代碼,比如ViewWillAppear這里
[UIView setAnimationsEnabled:YES];