欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 基础工具类总结

程序员文章站 2022-05-16 21:10:52
ios 基础工具类总结。 uitableview的group样式下顶部空白处理 //分组列表头部空白处理 uiview *view = [[uiview alloc] initwithframe:c...

ios 基础工具类总结。

uitableview的group样式下顶部空白处理

//分组列表头部空白处理

uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, 0, 0.1)];

self.tableview.tableheaderview = view;

uitableview的plain样式下,取消区头停滞效果

- (void)scrollviewdidscroll:(uiscrollview *)scrollview

{

cgfloat sectionheaderheight = sectionhead.height;

if (scrollview.contentoffset.y<=sectionheaderheight&&scrollview;.contentoffset.y>=0)

{

scrollview.contentinset = uiedgeinsetsmake(-scrollview.contentoffset.y, 0, 0, 0);

}

else if(scrollview.contentoffset.y>=sectionheaderheight)

{

scrollview.contentinset = uiedgeinsetsmake(-sectionheaderheight, 0, 0, 0);

}

}

获取某个view所在的控制器

- (uiviewcontroller *)viewcontroller

{

uiviewcontroller *viewcontroller = nil;

uiresponder *next = self.nextresponder;

while (next)

{

if ([next iskindofclass:[uiviewcontroller class]])

{

viewcontroller = (uiviewcontroller *)next;

break;

}

next = next.nextresponder;

}

return viewcontroller;

}

两种方法删除nsuserdefaults所有记录

//方法一

nsstring *appdomain = [[nsbundle mainbundle] bundleidentifier];

[[nsuserdefaults standarduserdefaults] removepersistentdomainforname:appdomain];

//方法二

- (void)resetdefaults

{

nsuserdefaults * defs = [nsuserdefaults standarduserdefaults];

nsdictionary * dict = [defs dictionaryrepresentation];

for (id key in dict)

{

[defs removeobjectforkey:key];

}

[defs synchronize];

}

打印所有已注册的字体名称

#pragma mark - 打印系统所有已注册的字体名称

void enumeratefonts()

{

for(nsstring *familyname in [uifont familynames])

{

nslog(@"%@",familyname);

nsarray *fontnames = [uifont fontnamesforfamilyname:familyname];

for(nsstring *fontname in fontnames)

{

nslog(@"\t|- %@",fontname);

}

}

}

取图片某一像素点的颜色 在uiimage的分类中

- (uicolor *)coloratpixel:(cgpoint)point

{

if (!cgrectcontainspoint(cgrectmake(0.0f, 0.0f, self.size.width, self.size.height), point))

{

return nil;

}

cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();

int bytesperpixel = 4;

int bytesperrow = bytesperpixel * 1;

nsuinteger bitspercomponent = 8;

unsigned char pixeldata[4] = {0, 0, 0, 0};

cgcontextref context = cgbitmapcontextcreate(pixeldata,

1,

1,

bitspercomponent,

bytesperrow,

colorspace,

kcgimagealphapremultipliedlast | kcgbitmapbyteorder32big);

cgcolorspacerelease(colorspace);

cgcontextsetblendmode(context, kcgblendmodecopy);

cgcontexttranslatectm(context, -point.x, point.y - self.size.height);

cgcontextdrawimage(context, cgrectmake(0.0f, 0.0f, self.size.width, self.size.height), self.cgimage);

cgcontextrelease(context);

cgfloat red = (cgfloat)pixeldata[0] / 255.0f;

cgfloat green = (cgfloat)pixeldata[1] / 255.0f;

cgfloat blue = (cgfloat)pixeldata[2] / 255.0f;

cgfloat alpha = (cgfloat)pixeldata[3] / 255.0f;

return [uicolor colorwithred:red green:green blue:blue alpha:alpha];

}

字符串反转

第一种:

- (nsstring *)reversewordsinstring:(nsstring *)str

{

nsmutablestring *newstring = [[nsmutablestring alloc] initwithcapacity:str.length];

for (nsinteger i = str.length - 1; i >= 0 ; i --)

{

unichar ch = [str characteratindex:i];

[newstring appendformat:@"%c", ch];

}

return newstring;

}

//第二种:

- (nsstring*)reversewordsinstring:(nsstring*)str

{

nsmutablestring *reverstring = [nsmutablestring stringwithcapacity:str.length];

[str enumeratesubstringsinrange:nsmakerange(0, str.length) options:nsstringenumerationreverse | nsstringenumerationbycomposedcharactersequences usingblock:^(nsstring *substring, nsrange substringrange, nsrange enclosingrange, bool *stop) {

[reverstring appendstring:substring];

}];

return reverstring;

}

禁止锁屏,

默认情况下,当设备一段时间没有触控动作时,ios会锁住屏幕。但有一些应用是不需要锁屏的,比如视频播放器。

[uiapplication sharedapplication].idletimerdisabled = yes;

[[uiapplication sharedapplication] setidletimerdisabled:yes];

模态推出透明界面

uiviewcontroller * = [[uiviewcontroller alloc] init];

uinavigationcontroller *na = [[uinavigationcontroller alloc] initwithrootviewcontroller:vc];

if ([[[uidevice currentdevice] systemversion] floatvalue] >= 8.0)

{

na.modalpresentationstyle = uimodalpresentationovercurrentcontext;

}

else

{

self.modalpresentationstyle=uimodalpresentationcurrentcontext;

}

[self presentviewcontroller:na animated:yes completion:nil];

xcode调试不显示内存占用

editscheme 里面有个选项叫叫做enable zoombie objects 取消选中

显示隐藏文件

//显示

defaults write com.apple.finder appleshowallfiles -bool true

killall finder

//隐藏

defaults write com.apple.finder appleshowallfiles -bool false

killall finder