第二天

*******图片的放大,和缩小 (去掉自动的布局)
-(IBAction ) zoomFrame:(UIbutton *) button{
CGRect frame= self.iconButton.frame;
// CGRect frame= self.iconButton.bounds; 用bounds的放大
if(button.tag){ //1
//放大
frame.size.width+=;
frame.size.height+=;
}else { frame.size.width-=;
frame.size.height-=;
}
self.iconButton.bounds=frame;
}
***首尾式动画

     设置动画执行的时长
*/
// 1> 准备开始一个动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0]; //时间 // 重新设置bounds
self.iconButton.bounds = frame; // 3> 提交动画
[UIView commitAnimations];
***位移行变
- (IBAction)move1:(UIButton *)button
{
// self.delta -= 20.0;
// // CGAffineTransformMakeTranslation的位移形变是相对按钮"初始"位置来变化的
// self.iconButton.transform = CGAffineTransformMakeTranslation(0, self.delta);
// CGAffineTransformTranslate 的位移形变是对按钮的上次形变的累加 CGFloat dx = , dy = ;
if (button.tag == kMovingDirTop || button.tag == kMovingDirBottom) {
dy = (button.tag == kMovingDirTop) ? -kMovingDelta : kMovingDelta;
}
if (button.tag == kMovingDirLeft || button.tag == kMovingDirRight) {
dx = (button.tag == kMovingDirLeft) ? -kMovingDelta : kMovingDelta;
} self.iconButton.transform = CGAffineTransformTranslate(self.iconButton.transform, dx, dy); NSLog(@"%@", NSStringFromCGAffineTransform(self.iconButton.transform));
}
***放大,和缩小
/** 放大缩小 */
- (IBAction)zoom:(UIButton *)button
{
CGFloat scale = (button.tag) ? 1.2 : 0.8; self.iconButton.transform = CGAffineTransformScale(self.iconButton.transform, scale, scale); NSLog(@"%@", NSStringFromCGAffineTransform(self.iconButton.transform));
}
***旋转
/** 旋转 */
- (IBAction)rotate:(UIButton *)button
{
// 在OC的开发中,关于角度统一都使用弧度值,逆时针是负值,顺时针是正值
// 180° = M_PI
CGFloat angle = (button.tag) ? -M_PI_4 : M_PI_4; [UIView beginAnimations:nil context:nil]; //行变
self.iconButton.transform = CGAffineTransformRotate(self.iconButton.transform, angle);
[UIView commitAnimations]; //
NSLog(@"%@", NSStringFromCGAffineTransform(self.iconButton.transform));
NSLog(@"%@", NSStringFromCGRect(self.iconButton.frame));
}
***代码创建按钮
/** 加载完成被调用 */
- (void)viewDidLoad
{
// 千万不要忘记调用父类的实现方法
[super viewDidLoad]; // 用代码创建按钮
// 使用alloc init方法实例化的按钮,就是custom类型的,按钮的类型一旦指定,不能修改
// 如果创建其他类型的按钮
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn1.center = CGPointMake(, );
[self.view addSubview:btn1]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
self.iconButton = btn; btn.backgroundColor = [UIColor redColor];//背景颜色 // 设置背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted]; // 设置按钮文字
[btn setTitle:@"点我啊" forState:UIControlStateNormal];
[btn setTitle:@"摸我" forState:UIControlStateHighlighted]; // 设置文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; // 文字垂直对齐方式
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom; // 将按钮添加到视图
[self.view addSubview:btn];
}
******图片查看器
#import "HMViewController.h" /**
用纯代码开发的过程 1. 确定界面元素,要有什么内容
2. 用代码来搭建界面
3. 编写代码
*/
@interface HMViewController ()
/**
@proerty
1. 创建了getter & setter方法
2. 生成一个带_的成员变量,直接读取成员变量不会经过getter方法&setter方法
*/
@property (nonatomic, strong) UILabel *noLabel;
@property (nonatomic, strong) UIImageView *iconImage;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton; /** 当前显示的照片索引 */
@property (nonatomic, assign) int index;
@end @implementation HMViewController /** 在viewDidLoad创建界面 */
- (void)viewDidLoad
{
[super viewDidLoad]; // 1. 序号
_noLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
// _noLabel.text = @"1/5";
_noLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_noLabel]; // 2. 图像
CGFloat imageW = ;
CGFloat imageH = ;
CGFloat imageX = (self.view.bounds.size.width - imageW) * 0.5;
CGFloat imageY = CGRectGetMaxY(self.noLabel.frame) + ; _iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
// _iconImage.image = [UIImage imageNamed:@"biaoqingdi"];
[self.view addSubview:_iconImage]; // 3. 描述文字
CGFloat descY = CGRectGetMaxY(self.iconImage.frame);
_descLabel = [[UILabel alloc] initWithFrame:CGRectMake(, descY, self.view.bounds.size.width, )];
// _descLabel.text = @"神马表情";
_descLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_descLabel]; // 4. 左边的按钮
_leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
CGFloat centerY = self.iconImage.center.y;
CGFloat centerX = self.iconImage.frame.origin.x * 0.5;
_leftButton.center = CGPointMake(centerX, centerY); [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
[_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
[self.view addSubview:_leftButton]; _leftButton.tag = -; [_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; // 5. 右边的按钮
_rightButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
_rightButton.center = CGPointMake(self.view.bounds.size.width - centerX, centerY); [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
[_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
[self.view addSubview:_rightButton]; _rightButton.tag = ; [_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; // 显示照片信息
[self showPhotoInfo];
} /**
重构的目的:让相同的代码只出现一次
*/
- (void)showPhotoInfo
{
// 设置序号
self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + , ]; // 设置图像和描述
switch (self.index) {
case :
self.iconImage.image = [UIImage imageNamed:@"biaoqingdi"];
self.descLabel.text = @"表情";
break;
case :
self.iconImage.image = [UIImage imageNamed:@"bingli"];
self.descLabel.text = @"病例";
break;
case :
self.iconImage.image = [UIImage imageNamed:@"chiniupa"];
self.descLabel.text = @"吃牛扒";
break;
case :
self.iconImage.image = [UIImage imageNamed:@"danteng"];
self.descLabel.text = @"蛋疼";
break;
case :
self.iconImage.image = [UIImage imageNamed:@"wangba"];
self.descLabel.text = @"王八";
break;
} // 控制按钮状态
// if (self.index == 4) {
// self.rightButton.enabled = NO;
// } else {
// self.rightButton.enabled = YES;
// } self.rightButton.enabled = (self.index != ); //可以点击 和不可以点击
self.leftButton.enabled = (self.index != );
} // 在OC中,很多方法的第一个参数,都是触发该方法的对象!
- (void)clickButton:(UIButton *)button
{
// 根据按钮调整当前显示图片的索引?
self.index += button.tag; // +1或者-1 [self showPhotoInfo];
} ///** 上一张照片 */
//- (void)prePhoto
//{
// NSLog(@"%s", __func__);
// self.index--;
//
// [self showPhotoInfo];
//}
//
///** 下一张照片 */
//- (void)nextPhoto
//{
// NSLog(@"%s", __func__);
// self.index++;
//
// [self showPhotoInfo];
//} @end
******* 懒加载(延迟加载),通过getter实现
/**
效果:让对象在最需要的时候才创建!
*/
- (NSArray *)imageList
{
NSLog(@"读取图像信息");
if (_imageList == nil) {
NSLog(@"实例化数组"); NSDictionary *dict1 = @{@"name": @"biaoqingdi", @"desc": @"表情1"};
NSDictionary *dict2 = @{@"name": @"bingli", @"desc": @"病例1"};
NSDictionary *dict3 = @{@"name": @"chiniupa", @"desc": @"吃牛扒1"};
NSDictionary *dict4 = @{@"name": @"danteng", @"desc": @"蛋疼1"};
NSDictionary *dict5 = @{@"name": @"wangba", @"desc": @"网吧1"}; _imageList = @[dict1, dict2, dict3, dict4, dict5];
}
return _imageList;
}
*****plist
/**
懒加载(延迟加载),通过getter实现 效果:让对象在最需要的时候才创建!
*/
- (NSArray *)imageList
{
NSLog(@"读取图像信息");
if (_imageList == nil) {
NSLog(@"实例化数组"); // "包" Bundle [NSBundle mainBundle]编译安装之后对应的程序包
NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"]; //读取plist的图片
NSLog(@"%@", path); // 在OC中ContentsOfFile,通常需要完整的路径
_imageList = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@", _imageList);
}
return _imageList;
}
/**
重构的目的:让相同的代码只出现一次
*/
- (void)showPhotoInfo
{
// 设置序号
self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + , ]; // 效率不高,每次都会生成数组
// 如何解决?使用属性记录字典数组
// NSDictionary *dict1 = @{@"name": @"biaoqingdi", @"desc": @"表情1"};
// NSDictionary *dict2 = @{@"name": @"bingli", @"desc": @"病例1"};
// NSDictionary *dict3 = @{@"name": @"chiniupa", @"desc": @"吃牛扒1"};
// NSDictionary *dict4 = @{@"name": @"danteng", @"desc": @"蛋疼1"};
// NSDictionary *dict5 = @{@"name": @"wangba", @"desc": @"网吧1"};
// NSArray *array = @[dict1, dict2, dict3, dict4, dict5]; // 设置图像和描述
self.iconImage.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];
self.descLabel.text = self.imageList[self.index][@"desc"]; self.rightButton.enabled = (self.index != );
self.leftButton.enabled = (self.index != );
}
**********Tom实现
#import "HMViewController.h" @interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tom; @end @implementation HMViewController /**
重构-抽取代码 方法:
1> 将重复代码复制到新的方法中
2> 根据需要调整参数 关于图像的实例化 imageNamed:系统推荐使用的,但是图像实例化之后的释放由系统负责
如果要自己释放图片,不能使用imageNamed方法! 而需要使用imageWithContentsOfFile 提示:如果放在Images.xcassets中的图片,不能使用imageWithContentsOfFile
Images.xcassets中不要 存放大的,不常用的图片
*/
- (void)tomAnimationWithName:(NSString *)name count:(NSInteger)count
{
// 如果正在动画,直接退出
if ([self.tom isAnimating]) return; // 动画图片的数组
NSMutableArray *arrayM = [NSMutableArray array]; // 添加动画播放的图片
for (int i = ; i < count; i++) {
// 图像名称
NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
// UIImage *image = [UIImage imageNamed:imageName];//内存不会被释放
// ContentsOfFile需要全路径
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path]; [arrayM addObject:image];
} // 设置动画数组
self.tom.animationImages = arrayM;
// 重复1次
self.tom.animationRepeatCount = ;
// 动画时长
self.tom.animationDuration = self.tom.animationImages.count * 0.075; // 开始动画
[self.tom startAnimating]; // 动画"结束"之后,清理动画数组
// self.tom.animationImages = nil;
// performSelector定义在NSObject分类中
// [self performSelector:@selector(cleanup) withObject:nil afterDelay:self.tom.animationDuration];
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; //清理内存,动画数组设置nil
} - (IBAction)tomAction:(UIButton *)sender
{
// currentTitle 可以取出按钮当前的标题文字
[self tomAnimationWithName:sender.currentTitle count:sender.tag];
} //- (void)cleanup
//{
// NSLog(@"%s", __func__);
//// self.tom.animationImages = nil;
// [self.tom setAnimationImages:nil];
//} - (IBAction)knockout
{
[self tomAnimationWithName:@"knockout" count:];
} - (IBAction)eatBird
{
[self tomAnimationWithName:@"eat" count:];
} @end
04-25 04:20