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;
}

你可能感兴趣的:(iOS 各种小知识点.小demo)