获取window的三种方式(最后一种最靠谱)
UIWindow *window = self.view.window ; UIWindow *window = [UIApplication sharedApplication].keyWindow; UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
简述:宏定义的后面没有符号
0.0.Block的使用
(1) 方法调用block
1.在.h里面(建立吧bock)和设置block的方法 typedef void(^BLOCK)(NSString *name); -(void)setBlock:(BLOCK)block;(在.m属性需要实现) 2.在.m里面进行实现block方法和属性的转换 先利用BLOCK设置一个属性 BLOCK valueBlock; -(void)setBlock:(BLOCK)block { valueBlock = block; } 3.在点击的调用的方法里面进行调用 valueBlock(这里放一个字符串传到外面); 4.在外面调用 [类的对象 setBlock:^(NSString *name) { 这里进行执行block这个方法 }];
(2) 属性调用block
1.在.h里面设置2个字符串 @property(nonatomic,strong) void(^guwen)(NSString *paGe,NSString *name); 2.在.m里面点击调用 if (self.guwen) { self.guwen(pageNumber,self.nameLabel.text); } 3.在外面把值取出来 利用这个类的对象进行调用 累的对象.guwen = ^(NSString *paGe,NSString *name){ 在此进行调用传值 };
0.Profile文件的设置
platform :ios,’7.0’ target “JLCardAnimation” do pod 'AFNetworking', '~> 3.1.0' pod 'MJExtension', '~> 3.0.13' pod 'SDWebImage', '~> 3.8.1' end
1.尺寸的宏定义
宽高的设置 #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height 尺寸的设置 #define iPhone5AndEarlyDevice (([[UIScreen mainScreen] bounds].size.height*[[UIScreen mainScreen] bounds].size.width <= 320*568)?YES:NO) #define Iphone6 (([[UIScreen mainScreen] bounds].size.height*[[UIScreen mainScreen] bounds].size.width <= 375*667)?YES:NO)
2.颜色的宏定义
//RGB颜色 #define CWColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] //随机色(和上面的保持 一致) #define CWRandomColor CWColor(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256)) 自定义随机色 #define CWRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
3.开发模式(名字自己起)
#ifdef DEBUG //处于开发阶段才打印(调试) #define CWLog(...) NSLog(__VA_ARGS__) #else //处于发布阶段(没有输出)(发布) #define CWLog(...) #endif
4.NSUserDefaults取值,存值
//取值 #define UserDefaultObject(A) [[NSUserDefaults standardUserDefaults]objectForKey:A] //存值(可变的值不可以存) #define UserDefaultSetValue(B,C) [[NSUserDefaults standardUserDefaults]setObject:B forKey:C] //存BOOL值 #define UserDefaultBool(D,E) [[NSUserDefaults standardUserDefaults]setBool:D forKey:E]
5.弱引用
__weak typeof(self) weakSelf = self; /* 弱引用 */ #define STWeakSelf __weak typeof(self) weakSelf = self;
6.判断版本是ios7以后版本
#define IOS_CURRENT_VERSION_IOS7 ([[UIDevice currentDevice].systemVersion floatValue] < 7.0 ? NO : YES)
7.打印函数,可以打印所在的函数,行数,以及你要打印的值
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); NSLog(@"==1==%s 2==[Line %d] 3==%@", __PRETTY_FUNCTION__, __LINE__,string);
8.cell高度根据文本来确定(注意字体的大小和外面的保持一致)
CGFloat height =[@"文字的内容" boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:11] forKey:NSFontAttributeName] context:nil].size.height;
9.请求,上拉,下拉刷新
一般都有页码和数组 NSArray *tempArray; NSMutableArray *dataArray; @property(nonatomic,assign) int p; -(void)refreshData { self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.p = 1; dataArray = [[NSMutableArray alloc]init]; [HUD showLoadingHUDWithText:@"加载中..." inView:self.view]; //调用网络请求的方法 [self request:1]; }]; self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{ self.p ++; [HUD showLoadingHUDWithText:@"加载中..." inView:self.view]; //调用网络请求的方法 [self request:self.p]; }]; }
10.网络请求
-(void)request:(int)pp { self.title = @"酒店"; #define DFTurl @"http://192.168.80.254" NSString *url = [NSString stringWithFormat:@"%@/jiekou/index.php?g=home&m=index&a=jiudian&p=%d",DFTurl,pp]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ /* 网络数据的请求 */ [[SXHTTPEngine shareManager]POST:url params:nil success:^(id responseObject) { DFTLog(@"====%@",responseObject); tempArray = responseObject[@"data"]; tempArray = [HostelHomeModel mj_objectArrayWithKeyValuesArray:tempArray]; [dataArray addObjectsFromArray:tempArray]; [HUD hideHUD]; /* 刷新主线程 */ dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; /* 上拉下拉刷新的截止 */ [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; }); } failure:^(NSError *error) { DFTLog(@"加载失败"); /* 上拉下拉刷新的截止 */ [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; [HUD showLoadingHUDWithText:@"加载失败"]; [HUD hideHUD]; }]; }); }
11. 设置导航栏文字的颜色以及文字大小(如导航的背景颜色,以及导航栏中间字体的颜色)
/* 设置导航栏文字的颜色以及文字大小(如导航的背景颜色,以及导航栏中间字体的颜色) [UINavigationBar appearance]; 设置Item的样式 [UIBarButtonItem appearance]; */ //在第一次使用调用一次 +(void)initialize { //设置整个项目所有item的主题样式() UIBarButtonItem *item = [UIBarButtonItem appearance]; //设置普通状态的字体颜色和大小 [item setTitleTextAttributes:@{NSForegroundColorAttributeName:CWColor(123, 123, 123),NSFontAttributeName:[UIFont systemFontOfSize:20]} forState:UIControlStateNormal]; //设置点击的时候颜色和大小 [item setTitleTextAttributes:@{NSForegroundColorAttributeName:CWColor(252, 108, 8),NSFontAttributeName:[UIFont systemFontOfSize:20]} forState:UIControlStateSelected]; }
导航栏上button图片防止被渲染
UIImage *img = [[UIImage imageNamed:@"icon_cog"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //宽度为负数的固定间距的系统item UIBarButtonItem *rightNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [rightNegativeSpacer setWidth:-15]; UIBarButtonItem *rightBtnItem1 = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemClicked:)]; UIBarButtonItem *rightBtnItem2 = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemClicked:)]; self.navigationItem.rightBarButtonItems = @[rightNegativeSpacer,rightBtnItem1,rightBtnItem2];
12 NSData 转化我 string
/* 1.把 NSData 转化我 string */ NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; /* 2.把string 转化我 NSData */ NSData *data = [respondString dataUsingEncoding:NSUTF8StringEncoding] //NSString转化为NSNumber NSString *string = @"9000"; NSNumber *number = @([string integerValue]); NSLog(@"number=%@",number); //NSNumber转化为NSString NSString *string1 = [NSString stringWithFormat:@"%@",number]; NSLog(@"string1=%@",string1);
13主线程刷新
<1>.GCD dispatch_async(dispatch_get_main_queue(), ^{ }); <2>.NSOperation [[NSOperationQueue mainQueue]addOperationWithBlock:^{ }];
13.改变图片的大小
我们需要传UIImage 和 CGSize 返回的还是UIImage - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{ // 创建一个bitmap的context // 并把它设置成为当前正在使用的context UIGraphicsBeginImageContext(newsize); // 绘制改变大小的图片 [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)]; // 从当前context中创建一个改变大小后的图片 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈 UIGraphicsEndImageContext(); // 返回新的改变大小后的图片 return scaledImage; }
14.有关block的使用
这仅仅是调用的展示 @property(strong,nonatomic)void(^agriculturalBtnClick)(); @property(strong,nonatomic)void(^forestryBtnClick)(); @property(strong,nonatomic)void(^AnimalBtnClick)(); -(void)homeBtnClick:(UIButton *)sender { switch (sender.tag) { case 0:{ if (self.agriculturalBtnClick) { self.agriculturalBtnClick(); } }break; case 1:{ if (self.forestryBtnClick) { self.forestryBtnClick(); } }break; case 2:{ if (self.AnimalBtnClick) { self.AnimalBtnClick(); } }break; } } /* 下面是对方法的调用(这是cell里面的调用) */ cell.agriculturalBtnClick = ^(){ }; cell.forestryBtnClick = ^(){ }; cell.AnimalBtnClick = ^(){ };
15.一个方法的延迟执行(2s之后执行)
[self performSelector:@selector(logInShouYe) withObject:nil afterDelay:2];
16.打电话
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",[user objectForKey:@"电话"]]; UIWebView * callWebview = [[UIWebView alloc] init]; [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; [self.view addSubview:callWebview];
17.弱引用的使用
__weak typeof(self) Wself = self;
18.UIWebView加载连接
NSURL *url = [NSURL URLWithString:@"http://www.pgyer.com/iHLM"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:webView]; [webView loadRequest:request];
19.按钮的点击方法(其实只是缩放率的改变,动画的加载)
按钮的点击放大,其实改变的只是按钮的transform(缩放率)
- 下面以一个button为例
UIButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.liekBtn.frame = CGRectMake(CGRectGetMaxX(self.disLikeBtn.frame)+100 , HEIGHT*0.5, 60, 60); [self.liekBtn setImage:[UIImage imageNamed:@"likeBtn"] forState:UIControlStateNormal]; [self.liekBtn addTarget:self action:@selector(liek) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.liekBtn]; -(void)liek { [UIView animateWithDuration:0.5 animations:^{ self.liekBtn.transform = CGAffineTransformMakeScale(1.2, 1.2); } completion:^(BOOL finished) { self.liekBtn.transform = CGAffineTransformMakeScale(1, 1); }]; }
3D放大
对象.transform3D = CATransform3DMakeScale(x, y, z);
20.手势冲突的解决方式
//重写手势的方法 手势会影响 cell的点击 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { return NO; }else{ return YES; } }
21打电话
挂代理 <UIActionSheetDelegate> UIAlertView * alertView2 = [[UIAlertView alloc]initWithTitle:@"全国统一客服热线:4007-114-115" message:nil delegate:self cancelButtonTitle:@"拨打" otherButtonTitles:@"取消", nil]; [alertView2 show]; - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { UIWebView*callWebview =[[UIWebView alloc] init] ; NSURL *telURL =[NSURL URLWithString:[NSString stringWithFormat:@"tel://4007114115"]]; [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; [self.backScrollView addSubview:callWebview]; return; }
22.导航栏的颜色
可以设置全局,也可以单独设置 self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont systemFontOfSize:20]}; /颜色自己随便设置 self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.101 green:0.705 blue:0.652 alpha:1.000];
23.自定义导航控制器上面的按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //设置图片的两种状态 //正常 [button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal]; //选中 [button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateHighlighted]; button.frame = CGRectMake(0, 0, 30, 30); [button addTarget:self action:@selector(back1) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
24.获取版本号
//获取版本号 NSString *version1 = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
25.相机问题
最近做头像上传功能需要使用系统相册、相机,在调用系统相册、相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文,纠结半天,最终在info.plist设置解决问题。发现在项目的info.plist里面添加Localized resources can be mixed YES(表示是否允许应用程序获取框架库内语言)即可解决这个问题。特此记录下以便以后查看和别人解决问题。