——————————————————————————————
//视图加载完毕后就会自动调用(系统自动调用),一般会在这个方法中进行初始化控件
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"视图加载完毕调用");
// 1.创建按钮对象
UIButton *head = [[UIButton alloc] init];
// 2.设置frame
head.frame = CGRectMake(30, 60, 120, 120);
// 3.添加
[self.view addSubview:head];
// 4.设置背景图片
UIImage *nomalImage = [UIImage imageNamed:@"btn_01"];
// 4.1设置普通状态的图片
[head setBackgroundImage:nomalImage forState:UIControlStateNormal];
// 4.2设置高亮状态的图片
UIImage *highImage = [UIImage imageNamed:@"btn_02"];
[head setBackgroundImage:highImage forState:UIControlStateHighlighted];
// 5.设置文字
[head setTitle:@"摸我吧" forState:UIControlStateNormal];
[head setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
// 6.设置文字颜色
[head setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[head setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
// 7.创建一个加号按钮
// 7.1创建加号按钮
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
[self.view addSubview:addBtn];
// 8.给加号添加点击事件
// addTarget:表示监听者:控制器来监听
// forControlEvents:监听到某个事件
// action:表示监听者监听到某个事件后调用action这个方法做些事情
// [addBtn addTarget:self action:@selector(addBtnClick)
// forControlEvents:UIControlEventTouchUpInside];
// 9.给头像添加点击事件
// [head addTarget:self action:@selector(headBtnClick)
// forControlEvents:UIControlEventTouchUpInside];
// 10.一个方法监听两个按钮点击
[head addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[addBtn addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
}
//隐藏状态栏
- ($BOOL)prefersStatusBarHidden {
return YES;
}
// 监听头像和加号按钮点击后调用的方法
- (void)btnClick:(UIButton *)btn {
// NSLog(@"监听到两个按钮的点击");
NSLog(@"%@", btn);
}
———————————字典———————————————————
- (void)viewDidLoad {
[super viewDidLoad];
// 加载数据
// NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
// dict1[icon] = @"biaoqingdi";
// dict1[desc] = @"在他面前,其他神马表情都弱爆了";
// NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
// dict2[icon] = @"wangba";
// dict2[desc] = @"哥们为什么选八号呢";
// NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
// dict3[icon] = @"bingli";
// dict3[desc] = @"这也忒狠了";
// NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
// dict4[icon] = @"chiniupa";
// dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";
// NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];
// dict5[icon] = @"danteng";
// dict5[desc] = @"亲,你能改下你的网名么?哈哈";
// NSMutableDictionary *dict6 = [NSMutableDictionary dictionary];
// dict6[icon] = @"danteng";
// dict6[desc] = @"亲,你能改下你的网名么?哈哈";
//
//
// _images = @[dict1,dict2,dict3,dict4,dict5,dict6];
//
[self changData];
// self.noLabel.text = [NSString stringWithFormat:@"1/5"];
// self.headView.image = [UIImage imageNamed:@"biaoqingdi"];
// self.descLabel.text = @"在他面前,其他神马表情都弱爆了!";
// self.previousBtn.enabled = NO;
}
//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据
//好处:提高性能
- (NSArray *)images{
if (_images ==nil) {
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
dict1[icon] = @"biaoqingdi";
dict1[desc] = @"在他面前,其他神马表情都弱爆了";
NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
dict2[icon] = @"wangba";
dict2[desc] = @"哥们为什么选八号呢";
NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
dict3[icon] = @"bingli";
dict3[desc] = @"这也忒狠了";
NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
dict4[icon] = @"chiniupa";
dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";
NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];
dict5[icon] = @"danteng";
dict5[desc] = @"亲,你能改下你的网名么?哈哈";
_images = @[dict1,dict2,dict3,dict4,dict5];
}
return _images;
}
//
- (IBAction)prvious {
// 1.索引值减一
self.index --;
// 2.根据索引值设置数据
[self changData];
}
//next按钮点击的方法
- (IBAction)next {
// 1.索引值加一
// self.index = self.index +1;
self.index ++;
// 2.根据索引值设置数据
[self changData];
}
//改变控件的数据
- (void)changData{
self.noLabel.text = [NSString stringWithFormat:@"%d/%ld",self.index + 1,self.images.count];
NSDictionary *dict = self.images[self.index];
self.headView.image = [UIImage imageNamed:dict[icon]];
self.descLabel.text = dict[desc];
// 3.设置按钮状态
// 3.1左边按钮设置状态
self.previousBtn.enabled = (self.index != 0);
// 3.2右边按钮设置状态
self.nextBtn.enabled = (self.index != (self.images.count - 1));
}
——————————plist————————————————————
- (void)viewDidLoad {
[super viewDidLoad];
[self changData];
// self.noLabel.text = [NSString stringWithFormat:@"1/5"];
// self.headView.image = [UIImage imageNamed:@"biaoqingdi"];
// self.descLabel.text = @"在他面前,其他神马表情都弱爆了!";
// self.previousBtn.enabled = NO;
}
//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据
//好处:提高性能
- (NSArray *)images {
if (_images == nil) {
// 1.获取全路径
NSString *path =
[[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"];
// arrayWithContentsOfFile:必须传入全路径(有路径必须传入全路径)
// 2.读取plist文件
_images = [NSArray arrayWithContentsOfFile:path];
}
return _images;
}
//
- (IBAction)prvious {
// 1.索引值减一
self.index--;
// 2.根据索引值设置数据
[self changData];
}
// next按钮点击的方法
- (IBAction)next {
// 1.索引值加一
// self.index = self.index +1;
self.index++;
// 2.根据索引值设置数据
[self changData];
}
//改变控件的数据
- (void)changData {
self.noLabel.text =
[NSString stringWithFormat:@"%d/%ld", self.index + 1, self.images.count];
NSDictionary *dict = self.images[self.index];
self.headView.image = [UIImage imageNamed:dict[icon]];
self.descLabel.text = dict[desc];
// 3.设置按钮状态
// 3.1左边按钮设置状态
self.previousBtn.enabled = (self.index != 0);
// 3.2右边按钮设置状态
self.nextBtn.enabled = (self.index != (self.images.count - 1));
}
—————————模型—————————————————————
- (void)viewDidLoad {
[super viewDidLoad];
[self changData];
}
//懒加载(延迟加载):思想:重写数据的get方法,当数据不存在的时候加载数据
//好处:提高性能
- (NSArray *)images {
if (_images == nil) {
// 1.获取全路径
// NSBundle:代表了一个资源包文件夹
// NSString *path =[[NSBundle mainBundle]pathForResource:@"images"
// ofType:@"plist"];
//
NSString *path =
[[NSBundle mainBundle] pathForResource:@"images.plist"
ofType:nil]; //第二种获取全路径的方法.
// arrayWithContentsOfFile:必须传入全路径(有路径必须传入全路径)
// 2.读取plist文件
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.字典转模型
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
ImageModel *imageModel = [[ImageModel alloc] init];
imageModel.icon = dict[@"icon"];
imageModel.desc = dict[@"desc"];
[tempArray addObject:imageModel];
}
_images = tempArray;
}
return _images;
}
//
- (IBAction)prvious {
// 1.索引值减一
self.index--;
// 2.根据索引值设置数据
[self changData];
}
// next按钮点击的方法
- (IBAction)next {
// 1.索引值加一
// self.index = self.index +1;
self.index++;
// 2.根据索引值设置数据
[self changData];
}
//改变控件的数据
- (void)changData {
self.noLabel.text =
[NSString stringWithFormat:@"%d/%ld", self.index + 1, self.images.count];
// NSDictionary *dict = self.images[self.index];
// self.headView.image = [UIImage imageNamed:dict[icon]];
// self.descLabel.text = dict[desc];
ImageModel *imageMode = self.images[self.index];
self.headView.image = [UIImage imageNamed:imageMode.icon];
self.descLabel.text = imageMode.desc;
// 3.设置按钮状态
// 3.1左边按钮设置状态
self.previousBtn.enabled = (self.index != 0);
// 3.2右边按钮设置状态
self.nextBtn.enabled = (self.index != (self.images.count - 1));
}
——————————————————————————————
oc strong ui weak/ strong
——————————序列帧动画————————————————————
}
//执行和牛奶的动画
- (IBAction)drink {
[self runAnimationWithName:@"drink" andCount:81];
}
- (IBAction)knock {
[self runAnimationWithName:@"knockout" andCount:81];
}
//执行动画:重构原则:把相同的代码放在一起,把不同的东西作为参数
- (void)runAnimationWithName:(NSString *)imageName andCount:(int)count {
// 0.判断动画是否在执行,然后确定是否继续执行程序
if (self.Tom.isAnimating) {
return;
}
// 1.获取播放动画的图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i++) {
// 1.1拼接图片的名称(2是表示去两位,0是表示占位符,不足两位补0)
NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", imageName, i];
// imageNamed:加载图片,图片加载完成后图片会驻留内存,好处:下次加载图片的时候会很快,不好处:占用内存空间
// imageWithContentsOfFile:加载出来的图片不会驻留内存,
// UIImage *image = [UIImage imageNamed:name];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[images addObject:image];
}
// 2.告诉tom播放哪些图片
self.Tom.animationImages = images;
// 2.1设置播放的次数
self.Tom.animationRepeatCount = 1;
// 2.2设置播放的时间
CGFloat delat = 0.08; //播放每一张图片的时间
self.Tom.animationDuration = delat * images.count;
// 3.播放动画
[self.Tom startAnimating];
// 4.清空图片的缓存
// performSelector:执行方法
// withObject:方法参数
// afterDelay:多长时间之后
// self 在afterDelay时间后执行performSelector这个方法
[self performSelector:@selector(clearCache)
withObject:nil
afterDelay:delat * images.count];
}
- (void)clearCache {
self.Tom.animationImages = nil;
}
———————————sound———————————————————
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
- (IBAction)drink;
@property(weak, nonatomic) IBOutlet UIImageView *Tom;
- (IBAction)knock;
@property(nonatomic, strong) AVAudioPlayer *play;
@end
@implementation ViewController
//执行和牛奶的动画
- (IBAction)drink {
[self runAnimationWithName:@"drink" andCount:81];
// [self playSound];$
[self performSelector:@selector(playSound) withObject:nil afterDelay:2.5];
}
- (IBAction)knock {
[self runAnimationWithName:@"knockout" andCount:81];
}
//执行动画:重构原则:把相同的代码放在一起,把不同的东西作为参数
- (void)runAnimationWithName:(NSString *)imageName andCount:(int)count {
// 0.判断动画是否在执行,然后确定是否继续执行程序
if (self.Tom.isAnimating) {
return;
}
// 1.获取播放动画的图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i++) {
// 1.1拼接图片的名称(2是表示去两位,0是表示占位符,不足两位补0)
NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", imageName, i];
// imageNamed:加载图片,图片加载完成后图片会驻留内存,好处:下次加载图片的时候会很快,不好处:占用内存空间
// imageWithContentsOfFile:加载出来的图片不会驻留内存,
// UIImage *image = [UIImage imageNamed:name];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[images addObject:image];
}
// 2.告诉tom播放哪些图片
self.Tom.animationImages = images;
// 2.1设置播放的次数
self.Tom.animationRepeatCount = 1;
// 2.2设置播放的时间
CGFloat delat = 0.08; //播放每一张图片的时间
self.Tom.animationDuration = delat * images.count;
// 3.播放动画
[self.Tom startAnimating];
// 4.清空图片的缓存
// performSelector:执行方法
// withObject:方法参数
// afterDelay:多长时间之后
// self 在afterDelay时间后执行performSelector这个方法
[self performSelector:@selector(clearCache)
withObject:nil
afterDelay:delat * images.count];
}
- (void)clearCache {
self.Tom.animationImages = nil;
}
- (void)playSound {
// 1.获取音效的路径
NSString *path =
[[NSBundle mainBundle] pathForResource:@"p_drink_milk" ofType:@"wav"];
// 2.把路径转化成url
NSURL *url = [NSURL fileURLWithPath:path];
// 2.创建一个播放器
NSError *error = nil;
// 3.创建一个播放器
AVAudioPlayer *play =
[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
self.play = play;
// 4.播放
[self.play play];
}
——————————模型————————————————————
- (id)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
}
return self;
}
+ (id)appWithDict:(NSDictionary *)dict{
return [[self alloc]initWithDict:dict];
}
#import "AppModel.h"
@interface ViewController ()
@property(nonatomic, strong) NSArray *apps;
@end
@implementation ViewController
//懒加载
- (NSArray *)apps {
if (_apps == nil) {
// 1.获取plist全路径
NSString *path =
[[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
// 2.读取plist文件
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.字典转模型
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// AppModel *appModel = [[AppModel alloc]init];
// appModel.icon = dict[@"icon"];
// appModel.name = dict[@"name"];
AppModel *appModel = [AppModel appWithDict:dict];
[tempArray addObject:appModel];
}
_apps = tempArray;
}
return _apps;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.设置格子的相关的数据
int totalColumn = 3;
CGFloat appViewW = 100;
CGFloat appViewH = 120;
CGFloat spaceXmargin = 15;
CGFloat viewW = self.view.frame.size.width;
CGFloat letfXmargin =
(viewW - totalColumn * appViewW - (totalColumn - 1) * spaceXmargin) * 0.5;
CGFloat topMargn = 50;
CGFloat spaceYmargin = 20;
// 2.创建格子
for (int i = 0; i < self.apps.count; i++) {
// 创建第一个格子
// 2.1创建一个格子
UIView *appView = [[UIView alloc] init];
int col = i % totalColumn;
int row = i / totalColumn;
CGFloat appViewX = letfXmargin + (appViewW + spaceXmargin) * col;
CGFloat appViewY = topMargn + (appViewH + spaceYmargin) * row;
// 2.2设置frame
appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
// 2.3添加
[self.view addSubview:appView];
// 2.4设置背景色
// appView.backgroundColor = [UIColor redColor];
//取出数据
AppModel *appModel = self.apps[i];
// 2.5添加一个图片
// 2.5.1 创建一个imageView
UIImageView *headView = [[UIImageView alloc] init];
CGFloat headViewY = 0;
CGFloat headViewW = 60;
CGFloat headViewX = (appViewW - headViewW) * 0.5;
CGFloat headViewH = 60;
// 2.5.2设置frame
headView.frame = CGRectMake(headViewX, headViewY, headViewW, headViewH);
// 2.5.3 添加
[appView addSubview:headView];
// headView.backgroundColor = [UIColor blueColor];
// 2.5.4设置图片数据
headView.image = [UIImage imageNamed:appModel.icon];
// 2.6添加label
// 2.6.1创建一个label
UILabel *nameLabel = [[UILabel alloc] init];
CGFloat nameLabelX = 0;
CGFloat nameLabelW = appViewW;
CGFloat nameLabelY = headViewY + headViewH;
CGFloat nameLabelH = 20;
// 2.6.2设置frame
nameLabel.frame =
CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
// 2.6.3 添加
[appView addSubview:nameLabel];
// nameLabel.backgroundColor = [UIColor greenColor];
// 2.6.4 设置文字数据
nameLabel.text = appModel.name;
// 2.6.5 设置文字的大小
nameLabel.font = [UIFont systemFontOfSize:14];
// 2.6.6 文字居中
nameLabel.textAlignment = NSTextAlignmentCenter;
// 2.7添加按钮
//
UIButton *downLoadBtn = [[UIButton alloc] init];
CGFloat downLoadBtnX = 10;
CGFloat downLoadBtnY = nameLabelY + nameLabelH + 10;
CGFloat downLoadBtnW = appViewW - 2 * downLoadBtnX;
CGFloat downLoadBtnH = 25;
downLoadBtn.frame =
CGRectMake(downLoadBtnX, downLoadBtnY, downLoadBtnW, downLoadBtnH);
[appView addSubview:downLoadBtn];
// downLoadBtn.backgroundColor = [UIColor grayColor];
// 2.7.1设置图片
[downLoadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"]
forState:UIControlStateNormal];
[downLoadBtn
setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"]
forState:UIControlStateHighlighted];
// 2.7.2设置按钮文字
[downLoadBtn setTitle:@"下载" forState:UIControlStateNormal];
// 按钮内部至少有两个内置的控件,一个ImageView,
// 用来显示图片,一个label,用来显示文字.
downLoadBtn.titleLabel.font = [UIFont systemFontOfSize:14];
}
}
———————————xib———————————————————
#import "AppModel.h"
@interface ViewController ()
@property(nonatomic, strong) NSArray *apps;
@end
@implementation ViewController
//懒加载
- (NSArray *)apps {
if (_apps == nil) {
// 1.获取plist全路径
NSString *path =
[[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
// 2.读取plist文件
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.字典转模型
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// AppModel *appModel = [[AppModel alloc]init];
// appModel.icon = dict[@"icon"];
// appModel.name = dict[@"name"];
AppModel *appModel = [AppModel appWithDict:dict];
[tempArray addObject:appModel];
}
_apps = tempArray;
}
return _apps;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.设置格子的相关的数据
int totalColumn = 3;
CGFloat appViewW = 100;
CGFloat appViewH = 120;
CGFloat spaceXmargin = 15;
CGFloat viewW = self.view.frame.size.width;
CGFloat letfXmargin =
(viewW - totalColumn * appViewW - (totalColumn - 1) * spaceXmargin) * 0.5;
CGFloat topMargn = 50;
CGFloat spaceYmargin = 20;
// 2.创建格子
for (int i = 0; i < self.apps.count; i++) {
// 创建第一个格子
// 2.1创建一个格子
UIView *appView = [[[NSBundle mainBundle] loadNibNamed:@"AppView"
owner:nil
options:nil] lastObject];
int col = i % totalColumn;
int row = i / totalColumn;
CGFloat appViewX = letfXmargin + (appViewW + spaceXmargin) * col;
CGFloat appViewY = topMargn + (appViewH + spaceYmargin) * row;
appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
[self.view addSubview:appView];
// 2.2给格子中的子控件设置数据,可以通过控件的父控件的一个方法根据tag值取得对应的控件(第一种去得子控件的方法)
AppModel *appModel = self.apps[i];
// 2.3通过tag值获取控件
// UIImageView *headView = (UIImageView *) [appView viewWithTag:10];
//
// headView.image = [UIImage imageNamed: appModel.icon];
//
// UILabel *nameLabel = (UILabel *)[appView viewWithTag:20];
// nameLabel.text = appModel.name;
// 2.4通过subView属性获取子控件
UIImageView *headView = (UIImageView *)appView.subviews[0];
headView.image = [UIImage imageNamed:appModel.icon];
UILabel *nameLabel = (UILabel *)appView.subviews[1];
nameLabel.text = appModel.name;
//
}
}
——————————————————————————————
——————————————————————————————
——————————————————————————————
——————————————————————————————
——————————————————————————————
——————————————————————————————