工作中小技巧总结(一)
OC和swift混编
swift调用OC
1、创建一个swift或者oc的工程:我这里是创建的Swift语言的工程,工程名SwiftTest
2、在工程中代码目录下创建一个oc的类,选择oc语言, 会出一个对话框,选择YES
3、这时会在工程里看到下图这样一个头文件 *-Bridging-Header.h
4、在这个头文件里添加你的OC文件的.h文件,就可以在任意swift文件中自行调用所包含的oc文件了。 #import “aaa.h”
OC调用swift
在工程的target-》build Setting->package下个性如下两项
Defines Module 改为YES
Product Module Name的值加上“-swift.h”作为头文件被OC包含
图片拉伸填满
+(UIImage *)resizeImageWithImageName:(NSString *)imageName{
UIImage *image=[self imageNamed:imageName];
return [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
}
改变图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
隐藏状态栏
-(BOOL)prefersStatusBarHidden{
return YES;
}
跳转时隐藏tabbar
viewController.hidesBottomBarWhenPushed=YES;
Json和NSString转换
+(NSString *)coverseObj2JsonStrWithObj:(id)obj{
NSError *error=nil;
NSData *data=[NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
if(!error){
return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}else{
return nil;
}
}
+(id)converseJsonStr2ObjWithJsonStr:(NSString *)jsonStr{
NSError *error=nil;
NSData *data=[jsonStr dataUsingEncoding:NSUTF8StringEncoding];
id obj =[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(!error){
return obj;
}else{
return nil;
}
}
改变状#改变状态栏的颜色
self.navigationController.navigationBar.barStyle = UIBarStyleBlack
改变导航栏上左右视图的位置
_weatherView=[[WeatherView alloc]initWithFrame:CGRectMake(0, 0, 45, 50)];
UIBarButtonItem *leftBarButon = [[ UIBarButtonItem alloc ] initWithCustomView :_weatherView];
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarButon];
}else{
self.navigationItem.leftBarButtonItem = leftBarButon;
}
AF网络检测
-(void)internet{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"无网络");
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"WiFi网络");
if (!_weatherView.isLoad) {
[_weatherView download];
}
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
NSLog(@"无线网络");
if (!_weatherView.isLoad) {
[_weatherView download];
}
break;
}
default:
break;
}
}];
}
AF的接受数据类型设置
manger.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",@"text/html",@"text/plain", nil]
弹簧动画
[UIView animateWithDuration:0.2 delay:0.0 usingSpringWithDamping:1 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
_imageview.transform=CGAffineTransformMakeTranslation(0,-3);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.3 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
_imageview.transform=CGAffineTransformMakeTranslation(0, 0);
} completion:^(BOOL finished) {
}];
}];
判断代理实现代理方法
if(self.delegate&&[self.delegate respondsToSelector:@selector(SignBtnClick: ScoreLabel:)]){
[self.delegate SignBtnClick:_TitleLabel ScoreLabel:_ScoreLabel];
}
tabbar按钮自定义图片
user.tabBarItem.image = [[UIImage imageNamed:@"user"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
富文本改颜色
NSDictionary *dic =@{NSForegroundColorAttributeName:UIColorFromRGB(0xFE4500),NSFontAttributeName:[UIFont systemFontOfSize:13]};
[home.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
态栏的颜色
self.navigationController.navigationBar.barStyle = UIBarStyleBlack
改变导航栏上左右视图的位置
_weatherView=[[WeatherView alloc]initWithFrame:CGRectMake(0, 0, 45, 50)];
UIBarButtonItem *leftBarButon = [[ UIBarButtonItem alloc ] initWithCustomView :_weatherView];
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarButon];
}else{
self.navigationItem.leftBarButtonItem = leftBarButon;
}
AF网络检测
-(void)internet{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"无网络");
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"WiFi网络");
if (!_weatherView.isLoad) {
[_weatherView download];
}
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
NSLog(@"无线网络");
if (!_weatherView.isLoad) {
[_weatherView download];
}
break;
}
default:
break;
}
}];
}
AF的接受数据类型设置
manger.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",@"text/html",@"text/plain", nil]
弹簧动画
[UIView animateWithDuration:0.2 delay:0.0 usingSpringWithDamping:1 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
_imageview.transform=CGAffineTransformMakeTranslation(0,-3);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.3 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
_imageview.transform=CGAffineTransformMakeTranslation(0, 0);
} completion:^(BOOL finished) {
}];
}];
判断代理实现代理方法
if(self.delegate&&[self.delegate respondsToSelector:@selector(SignBtnClick: ScoreLabel:)]){
[self.delegate SignBtnClick:_TitleLabel ScoreLabel:_ScoreLabel];
}
tabbar按钮自定义图片
user.tabBarItem.image = [[UIImage imageNamed:@"user"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
富文本改颜色
NSDictionary *dic =@{NSForegroundColorAttributeName:UIColorFromRGB(0xFE4500),NSFontAttributeName:[UIFont systemFontOfSize:13]};
[home.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
富文本划线
NSString *str=[NSString stringWithFormat:@"¥%.2f",[moudel.lastPrice floatValue]];
NSDictionary *dict=@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),NSStrikethroughColorAttributeName:[UIColor grayColor]};
NSMutableAttributedString *aStr=[[NSMutableAttributedString alloc]initWithString:str attributes:dict];
_myLastPriceLabel.attributedText=aStr;
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
return CGRectMake(0, 0, CGRectGetWidth(contentRect)*RATIO, CGRectGetHeight(contentRect));
}
//改变image的位置
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(CGRectGetWidth(contentRect)*RATIO, 0, CGRectGetWidth(contentRect)*(1-RATIO), CGRectGetHeight(contentRect));
}
设置导航栏标题
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24],NSForegroundColorAttributeName:[UIColor magentaColor]}];
把图片存进相册
UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
重绘系统cell图片大小
CGSize itemSize = CGSizeMake(60, 60)
UIGraphicsBeginImageContext(itemSize)
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height)
[cell.imageView.image drawInRect:imageRect]
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
设置cell选中后得背景颜色
cell.selectedBackgroundView=bgView;
隐藏cell分界线
Tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
cell自适应会用重绘
[cell setNeedsDisplay];
[cell layoutIfNeeded];
tableView.estimatedRowHeight=CGRectGetMaxY(cell.MyCol.frame)+10;
tableView.rowHeight=UITableViewAutomaticDimension;
检测键盘弹出
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector() name:UIKeyboardWillShowNotification object:nil];