IOS OC 笔记汇总

为了方便开发者开发出强大的功能,苹果提供了各种各样的框架

UIKit:创建和管理应用程序的用户界面
UIKit基本使用
QuartzCore:提供动画特效以及通过硬件进行渲染的能力
CoreGraphics:提供2D绘制的基于C的API
CoreLocation:使用GPS和WIFI获取位置信息
MapKit:为应用程序提供内嵌地图的接口
AVFoundation:音频、视频处理

KVC: key valued coding 键值编码
KVC使用
KVO: key valued observer 键值观察

依赖管理工具

依赖管理工具流行程度
CocoaPods > Carthage > Swift Package Manager

CocoaPads 依赖管理工具
命令行操作:
pod init  			初始化项目
pod install			下载依赖
pod install --repo-update 	更新依赖 PS: 删除同级目录下的 “Pods” 目录与Podfile.lock文件

pod 'XXXX', '~> 0.0'  	下载指定依赖

IOS 属性

IOS 基础属性

导入依赖
导入第三方依赖
#import <Masonry.h>

导入本地依赖/类文件
#import "ViewController.h"

property
//	strong:会复制引用,该一个处,被引用出也会改变
@property (nonatomic,strong) NSString *str1;

//	copy:会开辟一块新的内存区域,而且copy是不可变拷贝
@property (nonatomic,copy) NSString *str2;

/**
1.多线程特性:表示属性是否具有多线程特性,它有两个值:nonatomic(默认值)和atomic;
2.读写特性:表示属性是否可读或者可写,它有两个值:readwrite(默认值)和readonly;
(readonly:只会)
3.内存管理特性:用来给ARC做指示,它有四个值:strong(默认值),weak,copy和unsafe_unretained;
*/
@property (nonatomic,readwrite,strong) NSString *name;

strong:强引用也就是引用,其存亡直接决定了所指对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示列表中,则此对象会被从内存中释放。 
weak:弱引用除了不决定对象的存亡外,其他与强引用相同。即使一个对象被持有无数个若引用,只要没有强引用指向他,那麽其还是会被清除。

 1> 控件用weak
 2> 属性对象用strong
 3> 非对象类型(int Double Float...)用assign
 4> 字符串NSString用copy

创建对象的2种方式
MyObject * my = [[MyObject alloc] init];

MyObject * my = [MyObject new];
NSString
//	创建 NSString 对象
NSString *字符 = @"字符...";
// NSString类型动态插入字符
NSLog(@"str1:%@ str2:%@",str1,str2);
NSDictionary
//	创建 NSDictionary 字典对象
NSDictionary *字典 = @{@"name": @"nameData",@"desc": @"descData", ......};

NSArray
//	创建数组对象
NSArray *数组 =@[*字典1,*字典2,*字典3, ...... ];
//	数组使用
数组[self.index][@"name"];
IBAction和IBOutlet
IBAction
从返回值角度上看,作用相当于void
只有返回值声明为IBAction的方法,才能跟storyboard中的控件进行连线

IBOutlet
只有声明为IBOutlet的属性,才能跟storyboard中的控件进行连线

IOS 基础方法

C语言
.h 头文件,用于存放函数声明
.c C语言源文件,用于实现头文件中声明的方法

OC语言
.h 头文件,头文件包含类,方法,属性声明
.m/.mm 类的实现文件,参与编译的文件,用来实现类中声明的方法 
创建 .h 文件
#import "父类.h"

@interface 类名称 : 父类 {
    int memberVar1; // 实体变量
    id  memberVar2;
}

+(返回类型) class_method; // 类方法

-(返回类型) instance_method1; // 实例方法
-(返回类型) instance_method2: (int) p1;
-(返回类型) instance_method3: (int) p1 andPar: (int) p2;
@end
创建 .m 文件
TODO

//	CG_INLINE 空间换时间
CG_INLINE void ReplaceMethod()

UIView的常见属性

@property(nonatomic,readonly) UIView *superview;
获得自己的父控件对象

@property(nonatomic,readonly,copy) NSArray *subviews;
获得自己的所有子控件对象

@property(nonatomic) NSInteger tag;
控件的ID(标识),父控件可以通过tag来找到对应的子控件
transform属性
利用transform属性可以修改控件的位移(位置)、缩放、旋转

创建一个transform属性
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,  CGFloat ty) ;
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
(注意:angle是弧度制,并不是角度制)

在某个transform的基础上进行叠加
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);

清空之前设置的transform属性
view.transform = CGAffineTransformIdentity;

UIView的常见方法

- (void)addSubview:(UIView *)view;
添加一个子控件view

- (void)removeFromSuperview;
从父控件中移除

- (UIView *)viewWithTag:(NSInteger)tag;
根据一个tag标识找出对应的控件(一般都是子控件)
获取手机页面宽度
(self.view.bounds.size.width - totalCol * viewW)
修改控件的位置和尺寸
@property(nonatomic) CGRect frame;
控件所在矩形框在父控件中的位置和尺寸(以父控件的左上角为坐标原点)

@property(nonatomic) CGRect bounds;
控件所在矩形框的位置和尺寸(以自己左上角为坐标原点,所以bounds的x、y一般为0)

@property(nonatomic) CGPoint center;
控件中点的位置(以父控件的左上角为坐标原点)


通过以下属性可以修改控件的位置
frame.origin
center

通过以下属性可以修改控件的尺寸
frame.size
bounds.size
通过路径获取数据

// Bundle-包,只读的
NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData(文件名称)" ofType:@"plist(文件类型)"];
NSLog(@"%@", path);

// File表示从文件的完整路径加载文件
_imageList = [NSArray arrayWithContentsOfFile:path];
序列帧动画
// 序列帧动画,所谓序列帧动画,就是让一组图片一张一张的顺序播放
// 1. 图片的数组
NSMutableArray *arrayM = [NSMutableArray array];

for (int i = 0; i < count; i++) {
	//	图片名称
    NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", img, i];
    //	获取图片路径
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];


    //	创建图片控件
    /**
	 [UIImage imageNamed:imageName]; 
	 图片使用完成后,内存不会直接被释放掉,释放时间由系统决定,适用于图片小,常用的图像处理
	*/
	// UIImage *image = [UIImage imageNamed:imageName];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
    [arrayM addObject:image];	//	把图片控件添加到数组
}
//	设置图片
[self.tom setAnimationImages:arrayM];

// 2. 设置动画时长
[self.tom setAnimationDuration:arrayM.count * 0.075];
//	设置播放次数
[self.tom setAnimationRepeatCount:1];

// 3. 开始动画
[self.tom startAnimating];

// 4. 动画完成之后,再清除动画数组内容
//	@selector 的 setAnimationImages: 方法理解为 设置空的图片
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
动画效果

动画效果 完成之后,将Label从视图中删除

    // 1. 首尾式动画,只能做动画,要处理完成后的操作不方便
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    label.alpha = 1.0;
    [UIView commitAnimations];
    
    // 2. block动画比首尾式动画简单,而且能够控制动画结束后的操作 在iOS中,基本都使用首尾式动画
    [UIView animateWithDuration:2.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        // 删除label
        [label removeFromSuperview];
    }];
关闭键盘 resignFirstResponder
当叫出键盘的那个控件(第一响应者)调用这个方法时,就能退出键盘
例子: [self.对象 resignFirstResponder];

强行关闭键盘 endEditing
只要调用这个方法的控件内部存在第一响应者,就能退出键盘
例子: 
[self.view endEditing:YES]; 	//	多线程也强制关闭
[self.view endEditing:NO]; 	
修改状态栏
- (UIStatusBarStyle)preferredStatusBarStyle
{
    // 修改状态栏的颜色(白色)
    return UIStatusBarStyleLightContent;
}
查看大图缩小动画效果

入门方式


/** 大图 */
- (IBAction)bigImage
{
    // 1. 增加蒙版(跟根视图一样大小)
    // 在设置子视图大小时,通常使用父视图的bounds属性,可以保证x,一定是0
    UIButton *cover = [[UIButton alloc] initWithFrame:self.view.bounds];
    //  背景色
    cover.backgroundColor = [UIColor blackColor];
    //  透明度
    cover.alpha = 0.5f;
    
    [self.view addSubview:cover];
    
    [cover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
    
    self.cover = cover;
    
    // 2. 将图片移动到视图的顶层
    [self.view bringSubviewToFront:self.iconView];
    
    // 3. 动画放大图片
    // 1> 计算目标位置
    CGFloat viewW = self.view.bounds.size.width;    //  手机页面宽度
    CGFloat imageW = viewW;
    CGFloat imageH = imageW;
    CGFloat imageY = (self.view.bounds.size.height - imageH) * 0.5; //  手机页面高度
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0f];
    self.iconView.frame = CGRectMake(0, imageY, imageW, imageH);
    [UIView commitAnimations];
}


- (void)smallImage
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    // 0. 拦截首尾式动画结束的方法
    // delegate:代理,提干活的,让视图控制器监听动画事件
    [UIView setAnimationDelegate:self];
    // 动画完成事件发生的时候,让代理(self)去调用removeCover方法
    [UIView setAnimationDidStopSelector:@selector(removeCover)];
    
    // 1. 动画变小
    self.iconView.frame = CGRectMake(85, 80, 150, 150);
}

- (void)removeCover
{
    NSLog(@"动画完成");
    // 2. 删除遮罩
    [self.cover removeFromSuperview];
}


进阶


/** 大图 */
- (IBAction)bigImage
{
    /** 当按钮的alpha < 0.001 的时候,按钮不响应点击事件 */
    // 1. 增加蒙版(跟根视图一样大小)
    if (self.cover.alpha == 0.0) {
        // 2. 将图片移动到视图的顶层
        [self.view bringSubviewToFront:self.iconView];
        
        // 3. 动画放大图片
        // 1> 计算目标位置
        CGFloat viewW = self.view.bounds.size.width;
        CGFloat imageW = viewW;
        CGFloat imageH = imageW;
        CGFloat imageY = (self.view.bounds.size.height - imageH) * 0.5;
        
        [UIView animateWithDuration:1.0f animations:^{
        	// 遮罩半透明,看不见了
            self.cover.alpha = 0.5;
            self.iconView.frame = CGRectMake(0, imageY, imageW, imageH);
        }];
    } else {
        // 图片已经是放大显示的了
        [UIView animateWithDuration:1.0 animations:^{
            // 1. 动画变小
            self.iconView.frame = CGRectMake(85, 80, 150, 150);
            // 2. 遮罩透明,看不见了
            self.cover.alpha = 0.0f;
        }];
    }
}
简易动画大致有2种方式:

头尾式

[UIView beginAnimations:nil context:nil];
/** 需要执行动画的代码 **/
[UIView commitAnimations];

Block式

[UIView animateWithDuration:0.5 animations:^{
    /** 需要执行动画的代码 **/
}];
int转String
[NSString stringWithFormat:@"%d", num1 + num2];
方法理论
在.m中声明的方法是私有方法,外界无法直接访问,保证了封装性


图片

IOS技能树

在这里插入图片描述
在这里插入图片描述

UI控件

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值