1. IOS 认识
1.info.plist:
Bundle name: 应用名称
Bundle Identifirer: 应用唯一标识
// 获取 Info.plist 路径
NSString* filepath= [[NSBundle mainBundle] pathForResource:@"Info.plist" ofType:nil];
// 获取配置文件,保存字典中
NSDictionary* dict= [NSDictionary dictionaryWithContentsOfFile:filepath];
// 获取当前版本号
NSLog(@"dict=%@",dict[@"CFBundleShortVersionString
2. UiApplication的常用属性:
设置应用程序图标
设置状态栏
打开网页、发短信、发邮件、打电话
3. UIApllication的 delegate 代理:
1. 应用程序生命周期
2. 系统事件
3. 内存警告
https://www.jianshu.com/p/5b7c2e72a5f6
2. IOS 基础控件 UIKit.framework
UiKit坐标系: [选中控件,属性面板第6个]
1. 控件坐标是相对坐标: 现对于父控件的位置,每一个子控件
2. 左上角是原点
1. 按下 option拖,复制控件
2. xcode11 拖线找不到控制器: https://blog.csdn.net/qq_20255275/article/details/102784747
Touch up inside 事件,点击事件,拖入头文件中
属性连线 :Referencing Outlets,拖入匿名分类中即可
3.删除方法的时候,把连线的方法也删除
UIKit.frework
1. 按下 option拖,复制控件
2. xcode11 拖线找不到控制器: https://blog.csdn.net/qq_20255275/article/details/102784747
Touch up inside 事件,点击事件,拖入头文件中
属性连线 :Referencing Outlets,拖入匿名分类中即可
3.删除方法的时候,把连线的方法也删除
UiKit坐标系: [选中控件,属性面板第6个]
1. 控件坐标是相对坐标: 现对于父控件的位置,每一个子控件
2. 左上角是原点
1. 案例1 求和, 如何拖控件、点击方法
2. 案例2 设置随机颜色
3. 动态生成View
4. 案例4:动画
5. 案例5: 按钮创建
6. 基本属性控制api : 扩大、旋转、缩放、平移
7 : 删除指定tag 控件 、 删除父控件下所有子控件
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
// 方法拖入头文件即可,点击事件
- (IBAction)jisuang:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
// 私有属性 1
@property (weak, nonatomic) IBOutlet UITextField *text1;
// 私有属性 2
@property (weak, nonatomic) IBOutlet UITextField *text2;
// 求和
@property (weak, nonatomic) IBOutlet UILabel *sumText;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)jisuang:(UIButton* )sender {
NSDate* now = [NSDate new];
NSDateFormatter* nsformater= [NSDateFormatter new];
nsformater.dateFormat=@"yyyy-MM-dd HH:mm:ss";
NSString* str= [nsformater stringFromDate:now];
// 获取控件1 控件2的 文本值
int text1= [self.text1.text intValue];
int text2= [self.text2.text intValue];
int sum =text1 + text2;
// 1. 案例1 求和
[self.sumText setText:[NSString stringWithFormat:@"%d",sum]];
// 取消控制器View的编辑状态
// 隐藏键盘
[self.view endEditing:YES];
// 2. 案例2 设置随机颜色
// 获取 父元素
UIView* father= sender.superview;
// 生成随机数
float randomR= arc4random_uniform(255)/255.0;
float randomG= arc4random_uniform(255)/255.0;
float randomB= arc4random_uniform(255)/255.0;
// 随机颜色,这里参数是[0,1]
UIColor* randomColor= [UIColor colorWithRed:randomR green:randomG blue:randomB alpha:1];
father.backgroundColor= randomColor;
NSLog(@"hel---------%@",str);
// 3. 动态生成View
UIView* addView= [UIView new];
// 设置控件位置以及尺寸
addView.frame= CGRectMake(0, 0, 30, 30);
addView.backgroundColor=[UIColor redColor];
// 添加控件
[father addSubview:addView];
// 案例4:动画
// 不能直接修改frame的 值
// addView.frame.origin.x=230;
CGRect oldFrame = addView.frame;
oldFrame.origin.x= 100;
oldFrame.origin.y= 100;
// 动画方式1: 头尾式动画, ios13 已经废弃
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:10];
// [UIView setAnimationDelay:2];
// // 赋值回去
// addView.frame=oldFrame;
// [UIView commitAnimations];
// 动画方式2:
[UIView animateWithDuration:3 animations:^{
// 动画要改变的值
addView.frame= oldFrame;
}];
// 案例5: 按钮创建
UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(30, 30, 50, 50);
/**
UIButton控件状态:
default
Highighted: 点击
Selected: 选择
Disabled: 禁用状态
*/
// 枚举 提示按下 esc 键
[button setTitle:@"点我" forState:UIControlStateNormal];
UIImage* image= [UIImage imageNamed:@"btn_01.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[father addSubview:button];
// 使用代码添加点击事件
[button addTarget:self action:@selector(doSomeThings) forControlEvents:UIControlEventTouchUpInside
];
}
// 案例6: 基本属性控制api 扩大、旋转、缩放、平移
- (IBAction)startAnimation:(id)sender {
// CGRect oldFrame = self.donghuaImage.frame;
// // 中心点扩大
// oldFrame.size.height= oldFrame.size.height+20;
// oldFrame.size.width = oldFrame.size.height+20;
// oldFrame.origin.x = oldFrame.origin.x -10 ;
// oldFrame.origin.y = oldFrame.origin.y -10 ;
// self.donghuaImage.frame= oldFrame;
//使用 transfrom 属性来设置按钮 旋转 , 旋转必须是弧度
// self.donghuaImage.transform= CGAffineTransformMakeRotation(M_PI_4);
// 累加 旋转 , 在原始值的基础上累加 self.donghuaImage.transform
self.donghuaImage.transform= CGAffineTransformRotate(self.donghuaImage.transform, M_PI_4);
// CGAffineTransformMake(<#CGFloat a#>, <#CGFloat b#>, <#CGFloat c#>, <#CGFloat d#>, <#CGFloat tx#>, <#CGFloat ty#>) // 参数最多,定制最强
// CGAffineTransformMakeRotation(<#CGFloat angle#>) // 旋转
// CGAffineTransformMakeScale(CGFloat sx, <#CGFloat sy#>) 缩放
// CGAffineTransformTranslate(<#CGAffineTransform t#>, <#CGFloat tx#>, <#CGFloat ty#>) // 平移
}
//案例7 : 删除指定tag 控件 、 删除父控件下所有子控件
- (IBAction)clickBtn:(id)sender {
// 删除指定tag 控件
UIButton* btn10= [self.whiteView viewWithTag:10];
// [btn10 removeFromSuperview];
// 删除所有子控件
for (UIView* view in self.whiteView.subviews) {
// if( [view isKindOfClass: [UIButton class]]){
// continue;
// }
[view removeFromSuperview];
}
}
-(void)doSomeThings{
NSLog(@"点击按钮");
}
@end
效果:
3. 帧动画
UIimage imageName 与 imageWithContentsOfFile 区别,播放帧动画
#import "HomeController.h"
@interface HomeController ()
@property (weak, nonatomic) IBOutlet UIImageView *animation;
@end
@implementation HomeController
/**
播放帧动画:
1. UIimage imageName:图片名, 这种方式加载图片 会在内存中常驻 ,一般用于背景图片,小箭头 icon等等, 第一次加载以后,后面使用直接读取缓存中
对于需要释放的图片 使用 imageWithContentsOfFile 加载图片,只有当没有任何一种对象对他进行 强引用的时候,才会释放
2. 项目中, 如果是帧动画,需要播放完以后释放, 使用 imageWithContentsOfFile
不做缓存
*/
// 帧动画 播放方式1:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// if([self.animation isAnimating]){
// return;
// };
//
// NSMutableArray * arry=[NSMutableArray array];
// for (int i=0; i< 26 ; i++) {
// // 1. 拼接图片名字
// NSString* imageNameStr= [NSString stringWithFormat:@"angry_%02d",i];
//
// NSLog(@"lujing==%@",imageNameStr);
//
// // 2. 加载图片
// UIImage* image= [UIImage imageNamed:imageNameStr];
//
// [arry addObject:image];
// }
//
// // 把加载好的图片 设置给UIIMageView
// self.animation.animationImages= arry;
// // 开始播放动画
//
// // 设置播放动画的细节
// self.animation.animationDuration=2;
// self.animation.animationRepeatCount=2;
//
// [self.animation startAnimating];
}
// 帧动画 播放方式 2
- (IBAction)clickAnimation:(id)sender {
if([self.animation isAnimating]){
return;
};
NSMutableArray * arry=[NSMutableArray array];
for (int i=1 ; i<40 ; i++) {
// 1. 拼接图片名字
NSBundle* mainBund=[NSBundle mainBundle];
// 获取 项目 下 沙盒路径
// 可以把图片拷贝到项目根目录下,项目根目录下可以有文件夹,查看沙盒的时候文件夹会去掉
NSString* imageNameStr= [NSString stringWithFormat:@"gun%03d.png",i];
NSString* imagePath =[ mainBund pathForResource:imageNameStr ofType: nil];
NSLog(@"lujing==%@",imagePath);
// 2. 加载图片a
UIImage* image= [UIImage imageWithContentsOfFile:imagePath];
[arry addObject:image];
}
self.animation.animationImages= arry;
self.animation.animationDuration=40*0.1;
self.animation.animationRepeatCount=2;
[self.animation startAnimating];
// 播放完成以后释放 引用,才可以释放内存
[self performSelector:@selector(cleanImage) withObject:nil afterDelay:40*0.1*2];
}
// 清除动画引用
-(void)cleanImage{
self.animation.animationImages=nil;
}
@end