IOS-碎片记录

  • 自定义字体时设置字体大小时不能在.号后加任何

  • 多用继承/分类/扩展。不仅仅是对于UIViewcontroller 还有对于一些控件, 这对于整体的架构,以及后期的维护很重要。但是写继承的时候要注意分块儿,不然随着代码的迭代继承会成为你的累赘

  • 继承/多态/封装。越看越重要
    都能够让代码解耦、模块化,清晰起来 ,对于书写代码的思路也有很好的提升。
    所以一开始拿到需求的时候,要先对整体有一个总的思路(框架),不仅仅是Nav/Tab 还有对于一些控件/UIView。

  • webview内设置属性可以用js代码(h5),可以让webView看起来很酷

  • 字典可以是多个数组的集合,字典取value指以及数组取value值十分重要

  • 滑动控制器的contentSize

scrollView.contentSize = CGSizeMake(self.view.frame.size.width,CGRectGetMaxY(view.lastLabel.frame)+ 40); 
或者可以用布局写scrollView距底为0
  • 弹框动画
-(void)animationAlert:(UIView *)view{
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration             = 0.4;
    popAnimation.values               = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                                          [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                                          [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                                          [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes             = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions      = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:popAnimation forKey:nil];
}
  • 把数据源绑定到UIButton上
    1、可以通过自定义Btn(继承),在此上新增属性作为参数
1

//写一个wxzBtn继承自UIButton,为了可以重写数据。因为UIBtton本身不能
@interface wxzBtn : UIButton
//Btn所绑定的东西
@property (nonatomic,strong) NSString * titlestring;
@end

@interface Mycell : UITableViewCell
@property (nonatomic,strong) wxzBtn * confirmBtn;
@end

2
在创建 confirmBtn按钮时是根据类wxzBtn创建
self.confirmBtn = [[wxzBtn alloc] init];

3
在按钮点击之前重写titlestring的值
cell.confirmBtn.titlestring = @"1111";
[cell.confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
4
- (void)confirmBtnClick:(wxzBtn *)btn{
    NSLog(@"%@",btn.titlestring);
}
  • 知道返回数据的Json格式特别重要,因为在tableview等一些控制器里有section和row 这里需要对解析格式特别熟练 比如数组A里有一个字典---字典里有字符串,数组B---数组B里又有字符串
  • 加载本地的gif图片
- (void)viewDidLoad {
    [super viewDidLoad];
    _imageIV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    [self.view addSubview:_imageIV];
    _imageIV.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"party_sel_00000" ofType:@"png"]];
    _imageIV.backgroundColor = [UIColor redColor];
    [self configMainImageView];
}
- (void)configMainImageView {
    self.imageIV.animationImages = [self initialImageArray];
    self.imageIV.animationDuration = 3.f;
    self.imageIV.animationRepeatCount = 1;
    [self.imageIV startAnimating];
    [self performSelector:@selector(clearAinimationImageMemory) withObject:nil afterDelay:3.0f];
}
- (NSArray *)initialImageArray {
    
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 26; i++) {
        NSString * imageName ;
        if (i >= 10) {
            imageName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"party_sel_000%d@2x",i]ofType:@"png"];
        }else{
            imageName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"party_sel_0000%d@2x",i]ofType:@"png"];
        }
        UIImage *image = [UIImage imageWithContentsOfFile:imageName];
        
        [imageArray addObject:image];
    }
    return imageArray;
}
- (void)clearAinimationImageMemory {
    [self.imageIV stopAnimating];
    self.imageIV.animationImages = nil;
}
  • self.userInteractionEnabled = NO;
    设置了当前self不能和用户交互,相当于显示了一张图片

  • VIew里的跳转
    1、获取当前界面(VC)的Nav 然后Push
    2、获取当前窗口的可见控制器Push

  • btn的img和title的内边距设置
    在设置内边距的时候直接设置img和title的位置就可以了。

  • 销毁某个View时
    正确的做法
    1、[view removeFromSuperview ];
    2、view = nil;
    原因如下:

    _View = nil;
    [_View removeFromSuperview];
  
    //理解内存管理, 一个变量的何时销毁。
    //_View 在视图上的时候是有两个引用,一个父视图,一个——_View变量,先把——_View设置nil,就不能执行这句话了
    [nil removeFromSuperview];
  • KVC真的很好用! 不管是在什么情况下都可以取到某些特定的值

  • 利用贝塞尔曲线切view的边角

  //创建
   UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 70)];
   myLabel.text = @"Hello,world";
   myLabel.font = [UIFont systemFontOfSize:20.0];
   myLabel.textAlignment = NSTextAlignmentCenter;
   [self.view addSubview:myLabel];

   CGFloat radius = 21.0f;
   //利用贝塞尔曲线为label的边距设置
   UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:myLabel.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)];
   CAShapeLayer * mask  = [[CAShapeLayer alloc] init];
   mask.lineWidth = 5;
   // kCGLineCapButt:无端点
     kCGLineCapRound:圆形端点
     kCGLineCapSquare:方形端点(样式上和kCGLineCapButt是一样的,但是比kCGLineCapButt长一点)
   //
   mask.lineCap = kCALineCapSquare;
   // 带边框则两个颜色不要设置成一样即可
   mask.strokeColor = [UIColor redColor].CGColor;
   mask.fillColor = [UIColor yellowColor].CGColor;
   mask.path = path.CGPath;
   [myLabel.layer addSublayer:mask];

  • 加载帧动画
-  (void)viewDidLoad {  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 77/2, 118/2)];
    animatedImageView.image = [UIImage imageNamed:@"personaldata_reward"];
    //图片控件添加到视图上面去  
    [self.view addSubview:animatedImageView];  
      
    //创建一个可变数组  
    NSMutableArray *animationImages=[NSMutableArray array];  
    for(int I=1;I<=12;I++){  
        //通过for 循环,把我所有的 图片存到数组里面  
        NSString *imageName=[NSString stringWithFormat:@"personaldata_reward%d",I];  
        UIImage *image=[UIImage imageNamed:imageName];  
        [animationImages addObject:image];  
    }  
      
    // 设置图片的序列帧 图片数组  
    animatedImageView.animationImages=ary;  
    //动画重复次数  0代表无限选婚
    animatedImageView.animationRepeatCount=1;  
    //动画执行时间,多长时间执行完动画  
    animatedImageView.animationDuration=3.0;  
    //开始动画  
    if (! animatedImageView.isAnimating) {
         [animatedImageView startAnimating];
    }
    //添加一个按钮,把帧动画添加到按钮里
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(HCSystemWidth-50, HCSystemHeight/2+HCScaleHeight(100), 77/2, 118/2)];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [btn addSubview:animatedImageView];
    [btn bringSubviewToFront:animatedImageView];
    [self.view addSubview:btn];
}  

离开界面的时候的

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

推荐阅读更多精彩内容