iOS大杂烩

  1. 看扩展名是不是.gif image 有个属性:pathExtension 就可以判断
  self.gifView.hidden = ![model.image1.pathExtension.lowercaseString isEqualToString:@"gif"];
  1. 圆形进度条的使用

使用了第三方框架DACircularProgress ,具体用法如下:

[self.pictureView sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:_model.image0] andPlaceholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progreessView.hidden = NO;
        self.progreessView.progressLabel.text = [NSString stringWithFormat:@"%d%%",(int)(1.0  * receivedSize/expectedSize * 100)];
        [self.progreessView setProgress:1.0  * receivedSize/expectedSize animated:YES];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progreessView.hidden = YES;
    }];

-> 个人觉得这个框架还是挺好的,虽然里面有点小的bug,但是使用起来很方便,至于bug自己调调就好。

3.如果要在一个非控制器(例如UIView) 中,present出一个控制器,可以使用窗口的根控制器去present。

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicture animated:YES completion:nil];

4.为了减轻第三方框架的风险,最好的做法就是自定义,然后继承自该第三方框架。
5.图片是没办法直接添加点击事件的,需要用到手势去添加,首先打开用户交互,然后添加点击事件:

//打开图片的用户交互
    self.pictureView.userInteractionEnabled = YES;
    //添加图片的点击事件
    [self.pictureView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicture)]];
    [super awakeFromNib];

5.保存图片

- (IBAction)save {
    UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
}
  • 该方法必须含有这三个参数,可以点上面那个方法进去就会提醒示例方法。
  • 需要配置plist文件,



    否则就会报错:This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

6.单例模式: 重写系统的方法-alloc方法,具体操作如下:

//0.提供全局变量
static MKTool * _instance;
  + (instancetype)allocWithZone:(struct _NSZone *)zone{
//加互斥锁解决多线程访问安全
//    if (_instance) {
//        _instance = [super allocWithZone:zone];
//    }
//本身就是线程安全的
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

具体可以参考这篇:ios单例模式

7.pop和Core Animation 的区别:

  • Core Animation的动画只能添加到layer上
  • pop的动画能添加到任何对象
  • pop的底层并非基于Core Animation ,是基于CADisplaylink
  • Core Animation的动画仅仅是表象,并不会真正修改对象的frame\size等值
  • pop的动画修改对象的属性,真正修改可对象的属性

8.block用copy;block是属性或者参数的写法不一样:

  • 属性 :@property (nonatomic,copy) void(^myBlock)();
  • 参数:
-(void)cancelWithCompletionBlock:(void(^)())myBlock;

9.窗口级别:(窗口是不用addsubview的方式添加到父控件上的,只需要控制窗口是否隐藏就好了,----------------window_.hidden = NO;)

const UIWindowLevel UIWindowLevelNormal;>
const UIWindowLevel UIWindowLevelStatusBar;>
const UIWindowLevel UIWindowLevelAlert;

10.菊花的显示:

    UIActivityIndicatorView * load = [[UIActivityIndicatorView alloc]init];
    load.center = CGPointMake(120, 10);
    load.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [load startAnimating];

11.UIDynamic :可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象。重力、弹性碰撞等现象。

  • 使用步骤:
    1.创建物理仿真器
    2.创建相应的物理仿真行为(顺便添加仿真元素)
    3.将物理仿真行为添加到物理仿真器中--开始仿真

  • 注意:
    1.不是任何对象都能做物理仿真元素
    2.不是任何对象都能进行物理仿真

  • 那些对象才能做物理仿真元素
    1.任何遵守了UIDynamicItem协议的元素
    2.UIView默认遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真

实例如下:

@property (nonatomic,strong) UIDynamicAnimator * animator;

- (UIDynamicAnimator *)animator{
    if (!_animator) {
        _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }
    return _animator;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //1.创建物理仿真器(ReferenceView,参照视图,其实就是设置仿真范围)
   
    //2.创建物理仿真行为 - 重力行为
    UIGravityBehavior * gravity = [[UIGravityBehavior alloc]init];
    gravity.gravityDirection = CGVectorMake(1, 1);
    gravity.magnitude = 10;
    [gravity addItem:self.blueView];
    //3.添加物理仿真行为 到 物理仿真器中
    [self.animator addBehavior:gravity];
}

  • 比较好玩的吸附行为:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // 获得触摸点
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
   //创建吸附行为
    UISnapBehavior * snap = [[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:point];
    //防抖系数
    snap.damping = 1.0;
    //添加行为
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:snap];
}

你可能感兴趣的:(iOS大杂烩)