第二天

*******图片的放大,和缩小 (去掉自动的布局)
-(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

IOS第二天的更多相关文章

  1. ios第二天{函数}

    ////  main.m//  DAY3-1.6作业:工程敲4遍/*  作业:限时代码3分钟     提示用户从键盘输入一个整数(100以内) .如果输入的数,不是7的倍数,且不含7(个位和十位都不含 ...

  2. UE4 Windows平台部署游戏到IOS 第二部分

    点击加号后会出来如下截图 勾选上红色单选框处(因为这个我已经申请过了所以是灰色),然后continue到后面会出现下图 选择一个之前我提到申请证书会用的的那个.csr后缀文件夹,完成以后就可以下载证书 ...

  3. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  4. IOS第二天多线程-05NSOperationQueue 暂停,和恢复队列任务

    *********** #import "HMViewController.h" @interface HMViewController () <UITableViewDel ...

  5. IOS第二天多线程-04简化单例模式

    ******HMSingleton-ARC.h // .h文件 #define HMSingletonH(name) + (instancetype)shared##name; // .m文件 #de ...

  6. IOS第二天多线程-03对列组合并图片

    ********* // 2D绘图 Quartz2D // 合并图片 -- 水印 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) ...

  7. IOS第二天多线程-02一次性代码

    ********** #import "HMViewController.h" #import "HMImageDownloader.h" @interface ...

  8. IOS第二天多线程-01-延时执行

    **********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implement ...

  9. 起底多线程同步锁(iOS)

    iOS/MacOS为多线程.共享内存(变量)提供了多种的同步解决方案(即同步锁),对于这些方案的比较,大都讨论了锁的用法以及锁操作的开销,然后就开销表现排个序.春哥以为,最优方案的选用还是看应用场景, ...

随机推荐

  1. java&Protocol Buffers

    ps: Protocol Buffers简称PB PB 安装配置 下载 PB: 在 PB 官网,下载最新版(或者其他版本)PB,这里为了与 Java 项目中的 PB Maven 依赖版本一致,使用 P ...

  2. Zepto tap 穿透bug

    当两个层重叠在一起时,使用Zepto的tap事件时,点击上面的一层时会触发下面一层的事件,特别是底层如果是input框时,必“穿透”,“google”说原因是“tap事件实际上是在冒泡到body上时才 ...

  3. String equals()方法使用以及子串加密

    String equals()方法的实现方法: 名称 说明 String.Equals (Object) 确定此 String 实例是否与指定的对象(也必须是 String)具有相同的值. Strin ...

  4. 由case 和 break 引发的思考

    我在使用case 语句的时候,发现结果不对劲,原来我是忽略了break这个语句的添加 然后,我要反思的是,这样基本的语句,我这样的错误也能犯,证明我自己之前还没有真正掌握这个语句的使用. 所以,真正地 ...

  5. C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)

    Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, ...

  6. oracle增量备份

    在进行数据库维护的过程中经常会遇到数据库备份的问题.先介绍一种常用的数据备份操作系统执行计划+批处理命令:在win的系统中存在 任务计划程序 选项:新建任务选中你写好的程序,设定好时间,就可以按照设定 ...

  7. iOS学习06C语言结构体

    1.结构体的概述 在C语言中,结构体(struct)指的是一种数据结构,是C语言中构造类型的其中之一. 在实际应用中,我们通常需要由不同类型的数据来构成一个整体,比如学生这个整体可以由姓名.年龄.身高 ...

  8. C# 格式化字符串,日期,字符串操作汇总

    时间格式化 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6-6或更多的该怎么办呢 我们要用到:D ...

  9. django 视图开发与url配置

    可识别的视图需满足一下两个条件: 1.第一个参数的类型:HttpRequest 2.返回HttpResponse实例 在新建app的views当中写下以下内容 from django.shortcut ...

  10. Java 读取配置文件 Properties

    String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new Buff ...