iOS 各种小知识点.小demo

电影票样式的层次感

https://github.com/PageGuo/NewPagedFlowView

滚动

image.png

设置图片上的按钮可以点击

imgView.userInteractionEnabled  = YES;
 /*** 原生post请求

    NSString *path = @"http://v.juhe.cn/joke/randJoke.php";
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式为POST
    [request setHTTPMethod:@"POST"];
    //请求内容写在 请求体内
    [request setHTTPBody:[@"key=8d0bde7167db666ac160191217840b5b&type=pic" dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@",dic);
        
    }];
     [task resume];
    ***/

文字图片自适应

https://github.com/gsdios/SDAutoLayout

手机电池白色

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
    
}

button传递多个参数

objc_setAssociatedObject(aBt, "firstObject", @"参数1", OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        objc_setAssociatedObject(aBt, "secondObject", @"参数2", OBJC_ASSOCIATION_RETAIN_NONATOMIC);

使用

objc_getAssociatedObject(sender, "firstObject");
objc_getAssociatedObject(sender, "secondObject")

生成图标

https://icon.wuruihong.com/

手机通讯录

https://github.com/hackxhj/BeautyAddressBook

身份证识别

https://github.com/zhongfenglee/IDCardRecognition

NSUserDefaults

[[NSUserDefaults standardUserDefaults]setObject:tempDic forKey:@"param"];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"param"]);

大次数循环内存暴涨问题 典型的例题如下

for (int i = 0; i < 100000; i++) {
    
    NSString *string = @"Abc";
    
    string = [string lowercaseString];
    
    string = [string stringByAppendingString:@"xyz"];
    
    NSLog(@"%@", string);
    
}

该循环内产生大量的临时对象,直至循环结束才释放,可能导致内存泄漏,解决方法为在循环中创建自己的autoReleasePool,及时释放占用内存大的临时变量,减少内存占用峰值。

for (int i = 0; i < 100000; i++) {
        
        @autoreleasepool {
            
            NSString *string = @"Abc";
            
            string = [string lowercaseString];
            
            string = [string stringByAppendingString:@"xyz"];
            
            NSLog(@"%@", string);
            
        }
        
    }

播放语音 mp3

//从budle路径下读 这个文件名是你的歌曲名字,mp3是你的音频格式
    NSString *string = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp3"];
    //把音频文件转换成url格式
    NSURL *url = [NSURL fileURLWithPath:string];
    //初始化音频类 并且添加播放文件
    avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    //设置代理
    avAudioPlayer.delegate = self;

    //设置初始音量大小
     //avAudioPlayer.volume = 1;

    //设置音乐播放次数  -1为一直循环
    avAudioPlayer.numberOfLoops = -1;


    [avAudioPlayer play];

view添加翻页动画动画

CATransition *animation = [CATransition animation];
        animation.duration = 0.7f;
        animation.type = @"pageCurl";
        
        
        animation.subtype = kCATransitionFromLeft;
        
        animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
        [self.sxssScrollView.layer addAnimation:animation forKey:@"animation"];

最近做项目发现app 首页push一个界面,返回的时候,tabBar上的图标和文字出现一个从上往下的神奇动画, 经过测试发现,如果使用系统OS12.1 UINavigationController + UITabBarController,在popViewControllerAnimated 会遇到tabbar布局错乱的问题:
这个问题是 iOS 12.1 Beta 2 引入的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发,出现这个现象的直接原因是 tabBar 内的按钮 UITabBarButton 被设置了错误的 frame,frame.size 变为 (0, 0) 导致的。
所以最简单的解决方案就是:

[UITabBar appearance].translucent = NO;

button设置文字对齐方式

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

首先,这里使用button.titleLabel.textAlignment = NSTextAlignmentLeft; 这行代码是没有效果的,这只是让标签中的文本左对齐,但并没有改变标签在按钮中的对齐方式。所以我们使用button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 这行代码,把按钮的内容(控件)的对齐方式修改为水平左对齐,但是这们会紧紧靠着左边,不好看,所以我们还可以修改属性:button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了。

基础知识

http://www.cocoachina.com/ios/20180531/23592.html?utm_source=tuicool&utm_medium=referral

富文本

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 30)];
    
    testLabel.textAlignment = NSTextAlignmentCenter;
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"猴年大吉,新春快乐!"];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:26.0]
     
                          range:NSMakeRange(2, 2)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor redColor]
     
                          range:NSMakeRange(2, 2)];
    
    [AttributedStr addAttribute:NSBackgroundColorAttributeName
     
                          value:[UIColor redColor]
     
                          range:NSMakeRange(7, 2)];
    
    testLabel.attributedText = AttributedStr;
    
    [self.view addSubview:testLabel];

文字加粗

[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗并且倾斜

[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

渐变色

UIView *colorView = [[UIView alloc] init];
[colorView setFrame:CGRectMake(20, 160, 
        self.view.frame.size.width - 40, self.view.frame.size.height - 320)];
[self.view addSubview:colorView];
    
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = colorView.bounds;
gradient.colors = [NSArray arrayWithObjects:
                   (id)[UIColor colorWithRed:0 green:143/255.0 blue:234/255.0 alpha:1.0].CGColor, 
                   (id)[UIColor colorWithRed:0 green:173/255.0 blue:234/255.0 alpha:1.0].CGColor, 
                   (id)[UIColor whiteColor].CGColor, nil];
[colorView.layer addSublayer:gradient];

设置提示框取消 确定颜色

[cancel setValue:getColor(@"141414") forKey:@"titleTextColor"];

dismissViewControllerAnimated到根视图

如果是在Controller中  则代码如下
-(void)backClick{
    
    UIViewController *vc =  self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
    
}

如果实在视图(View)中 则代码如下

-(void)backClick{
    
    UIViewController *vc =  [self viewController];
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
    
    
    
}
/** 获取当前view的controller */
- (UIViewController *)viewController
{
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponser = [next nextResponder];
        
        if ([nextResponser isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponser;
        }
    }
    return nil;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • //联系人:石虎QQ:1224614774昵称:嗡嘛呢叭咪哄 QQ群:807236138 群称:iOS 技术交流学...
    石虎132阅读 7,369评论 0 9
  • 我不知道该写下些什么,这似乎是毫无意义和纪念性的一件事,可是,我就是想熬着半夜的时光,写下一些东西。 我哭了,为了...
    微木头阅读 221评论 0 1
  • 有位老太太问爱因斯坦“听说您在研究相对论,您能告诉我什么是相对论吗?”爱因斯坦没办法向一位老人讲清楚相对论的内容,...
    用思考喂饱生活阅读 124评论 0 0