工作中小技巧总结(一)

工作中小技巧总结(一)

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转换

//将json对象装换成json字符串
+(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;
    }
}
//将json字符串转换成json对象
+(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) {//如果weather没加载
                    [_weatherView download];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"无线网络");
                if (!_weatherView.isLoad) {//如果weather没加载
                    [_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) {//如果weather没加载
                    [_weatherView download];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"无线网络");
                if (!_weatherView.isLoad) {//如果weather没加载
                    [_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;

重写button上图片和文字的位置

-(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标识为需要重绘
[cell layoutIfNeeded];//cell如果需要重绘就重新绘制
tableView.estimatedRowHeight=CGRectGetMaxY(cell.MyCol.frame)+10;//预估算高度
tableView.rowHeight=UITableViewAutomaticDimension;//自适应高度

检测键盘弹出

[[NSNotificationCenter defaultCenter]addObserver:self   selector:@selector()  name:UIKeyboardWillShowNotification  object:nil];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值