当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS开发小技巧总结

iOS开发小技巧总结

2018年09月14日  | 移动技术网移动技术  | 我要评论

ios开发小技巧总结

tip 1 : 给uiimage添加毛玻璃效果

func blurimage(value:nsnumber) -> uiimage {

let context = cicontext(options:[kcicontextusesoftwarerenderer:true])

let ciimage = coreimage.ciimage(image:self)

let blurfilter = cifilter(name:"cigassianblur")

blurfilter?.setvalue(ciimage, forkey:kciinputimagekey)

blurfilter?.setvalue(value, forkey:"inputradius")

let imageref = context.createcgimage((blurfilter?.outputimage)!, fromrect:(ciimage?.extent)!)

let newimage = uiimage(cgimage:imageref)

return newimage

}

value : value代表毛玻璃效果的程度

核心内容:let blurfilter = cifilter(name:"cigassianblur") 使用滤镜工具

tip 2 : 图片压缩

func imagecompress(targetwidth:cgfloat) -> uiimage {

let targetheight = targetwidth/width*height

uigraphicsbeginimagecontext(cgsizemake(targetwidth,targetheight))

self.drawinrect(cgrectmake(0,0,targetwidth,targetheight))

let newimage:uiimage = uigraphicsgetimagefromcurrentimagecontext()

uigrapicsendimagecontext()

return newimage

}

这里是按原uiimage比例做压缩所以:let targetheight = targetwidth/width*height

tip 3 : svn & git 用法总结

一:svn

a. 项目经理:1.创建项目—checkin

2.设置项目人员

b.工作人员:1.checkout 获取项目的完整副本,此工作只需要做"一次"

2. 工作写代码....

3.阶段性工作完成后,commit(提交) 将自己修改的文件,上传到服务器

每天下班前一定要做一次能够编译的提交!

4.定期update项目,从服务器将最新的内容更新到本地,每天上班第一件事情一定要做的事情!

二. git

a.项目经理:1.创建项目push

2.设置项目人员

b. 工作人员:1.pull从服务器下拉最新的本版

2.commit是将修改过的代码提交至本地的代码库

3.每天下班前push自己当前的代码到服务器

4.每天上班前从服务器pull最新的内容

三. m / a 文件更新

对于文件夹svn支持并不好,需要在工具下选定文件夹commit

对应文件选定文件commits

由于过去在公司多半时间是做独立开发,最多人的时候也是两个人做开发,所以协作工具用的少,但是不断的关注着两种代码仓库管理工具,总结了一些tip,还有待实践验证,吐槽吧......

tip 4: uinavigationcontroller下的坐标系

ios 9 前:

navigationbar 如果设置了背景图片,那么可视化坐标会从navgationbar下面开始

self.navigationcontroller?.navigationbar.setbackgroundimage(uiimage(named:"nav"), forbarmetrics:uibarmetrics.default)

ios 9 后 : 坐标从状态栏下面开始

tip 5 : c字符串转成nsstring & nsstring转成c字符串

const char *cstring = "cstring";

c字符串转成nsstring : nsstring *str = [nsstring stringwithutf8string:cstring];

nsstring * str = @"nsstring";

nsstring转成c字符串 : const char *cstring = [str utf8string];

tip 6 : oc & swift 代码块(语法糖)

objective-c :

uilabel *label1 = ({

uilabel *label = [uilabelnew];

[self.view addsubview:label];

label.frame=cgrectmake(100,100,100,45);

label.backgroundcolor= [uicolor redcolor];

label.text=@"大家好1";

label;

});

uilabel*label2 = ({

uilabel*label = [uilabel new];

[self.view addsubview:label];

label.frame=cgrectmake(100,160,100,45);

label.backgroundcolor= [uicolor redcolor];

label.text=@"大家好2";

label;

});

uilabel*label3 = ({

uilabel*label = [uilabel new];

label.frame=cgrectmake(100,250,100,45);

label.backgroundcolor= [uicolor redcolor];

label.text=@"大家好3";

label;

});

[self.viewaddsubview:label3];

({

uilabel *label = [uilabel new];

[self.view addsubview:label];

label.frame=cgrectmake(100,310,100,45);

label.backgroundcolor= [uicolor redcolor];

label.text=@"大家好4";

});

swift:

letlabel1:uilabel= {

let label =uilabel()

self.view.addsubview(label)

label.frame=cgrectmake(100,100,100,45)

label.backgroundcolor=uicolor.redcolor()

label.text="大家好"

return label

}()

let label2:uilabel= {

let label =uilabel()

label.frame=cgrectmake(100,200,100,45)

label.backgroundcolor=uicolor.redcolor()

label.text="大家好"

return label

}()

self.view.addsubview(label2)

使用语法糖的好处就是拷贝代码时只需做少许的修改就可以达到目的,如上面的栗子,想要创建多个label,只要赋值粘贴,改一处,也就是对象名称就可以轻松完成!

tip 7 : 数据持久化方式归纳总结

数据缓存方式选择:

1: fmdata(增删改查) --数据需要:增删改查

2: coredata --数据需要:增删改查

3:序列化(nsuserdefault) --状态、偏好设置、默认选项

4:单独.plist --列表数据,城市信息等

5:单独.json文件 --页面列表数据

6: realm框架(增删改查) --数据需要:增删改查

7: fastcoder 某“强哥”推荐,哈哈哈!

tip 8 : 清理profiles证书文件

~/library/mobiledevice/provisioning profiles

由于平时会负责多个项目的上线管理或是开发工作,因此mac中有很多签名文件,有时候都分不清东西南北,一不做,二不休,前往这个目录下,将文件删个精光,调试的时候用到证书再update一下当前项目的证书即可

tip 9 : 拿到当前屏幕所看到的viewcontroller

objective-c版本:

- (uiviewcontroller *)getapprootviewcontroller

{

uiviewcontroller *approotvc = [uiapplication sharedapplication].keywindow.rootviewcontroller;

uiviewcontroller *topvc = approotvc;

while (topvc.presentedviewcontroller) {

topvc = topvc.presentedviewcontroller;

}

return topvc;

swift版本:

func getapprootviewcontroller()->uiviewcontroller?{

vartopvc=uiapplication.sharedapplication().keywindow?.rootviewcontroller

while topvc?.presentedviewcontroller!=nil{

topvc=topvc?.presentedviewcontroller

}

return topvc?

}

tip 10 : 制作pch文件

步骤:

1、新建ios->other->pch file

2、targets->buildsetting->prefix header->设置$(srcroot)/文件在工程中的路径

3、pch能像以前一样正常使用

如:$(srcroot)/firstproject/prefixheader.pch

firstproject : pch路径中的最后一个拓展名

prefixheader.pch: 是pch文件名

简介:/users/ly/desktop/firstproject/firstproject

tip 11 : 设置uinavigationcontroller title

当 uiviewcontroller作为uinavigationcontroller的根视图控制器的时候,将这个nav(root)赋给 tabbarcontroller时,

这样写:

root.title = @“title”;

那么 :self.naviagtionitem.title 会显示 title

同时 :nav.tabbaritem.title 也会显示 title

tip 12 : 判断uiscrollview是横向滚动还是竖向滚动

- (bool)gesturerecognizershouldbegin:(uigesturerecognizer*)gesturerecognizer {

if([gesturerecognizeriskindofclass:[uipangesturerecognizerclass]]) {

cgpointvelocity = [(uipangesturerecognizer*)gesturerecognizervelocityinview:gesturerecognizer.view];

boolishorizontalpanning =fabsf(velocity.x) >fabsf(velocity.y);

return ishorizontalpanning;

}

returnyes;

}

tip 13 : 监听 backbarbuttonitem 返回事件

github上搜: uiviewcontroller+backbuttonhandler 开源项目

tip 14 : 让一个view透明度失效

self.view.backgroundcolor=uicolor(colorliteralred:255.0/255, green:255.0/255, blue:255.0/255, alpha:0.3)

let subview = uiview()

subview.tintcolor=uicolor.whitecolor()//不透明了

self.view.addsubview(subview)

tip 15 : 从ipod库中读出音乐文件

// 从ipod库中读出音乐文件

mpmediaquery*everything=[[mpmediaqueryalloc]init];

//读取条件

mpmediapropertypredicate*albumnamepredicate=[mpmediapropertypredicatepredicatewithvalue:[nsnumbernumberwithint:mpmediatypemusic]forproperty:mpmediaitempropertymediatype];

[everythingaddfilterpredicate:albumnamepredicate];

nslog(@"loggingitemsfromagenericquery...");

nsarray*itemsfromgenericquery=[everythingitems];

for(mpmediaitem*songinitemsfromgenericquery){

nsstring*songtitle=[songvalueforproperty:mpmediaitempropertytitle];

nslog(@"%@",songtitle);

}

tip 16 : 广告标示符(adid) & adfv标示符的那些事

1.如何识别一个应用安装在同一个设备上呢?

2.如何识别一个企业的应用安装在同一个设备上呢?

苹果给我们提供了advertisingidentifier 来解决问题1;

只要是同一台设备,那么advertisingidentifier就是一样的

但是如果在设置-隐私-广告那里关掉这个权限或是还原设备的话,就没办法了哭死去吧

苹果给我们提供了identifierforvendor 来作为一个企业的app标示符

比如: com.game.yoyo

com.game.xoxo

只要在同一台设备上,那么identifierforvendor 是一样的

如果:com.game.yoyo

com.buyer.yoyo

不管是不是同一个应用identifierforvendor 都是不一样的

上代码:

广告id:

#import

//每个设备有唯一一个,如果重置广告或设置-隐私-关闭广告就会关闭更换

nsstring*adid = [[[asidentifiermanagersharedmanager]advertisingidentifier]uuidstring];

企业id:

nsstring*idfv = [[[uidevicecurrentdevice]identifierforvendor]uuidstring];

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网