IOS第二天
第二天
*******图片的放大,和缩小 (去掉自动的布局)
-(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第二天的更多相关文章
- ios第二天{函数}
//// main.m// DAY3-1.6作业:工程敲4遍/* 作业:限时代码3分钟 提示用户从键盘输入一个整数(100以内) .如果输入的数,不是7的倍数,且不含7(个位和十位都不含 ...
- UE4 Windows平台部署游戏到IOS 第二部分
点击加号后会出来如下截图 勾选上红色单选框处(因为这个我已经申请过了所以是灰色),然后continue到后面会出现下图 选择一个之前我提到申请证书会用的的那个.csr后缀文件夹,完成以后就可以下载证书 ...
- IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...
- IOS第二天多线程-05NSOperationQueue 暂停,和恢复队列任务
*********** #import "HMViewController.h" @interface HMViewController () <UITableViewDel ...
- IOS第二天多线程-04简化单例模式
******HMSingleton-ARC.h // .h文件 #define HMSingletonH(name) + (instancetype)shared##name; // .m文件 #de ...
- IOS第二天多线程-03对列组合并图片
********* // 2D绘图 Quartz2D // 合并图片 -- 水印 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) ...
- IOS第二天多线程-02一次性代码
********** #import "HMViewController.h" #import "HMImageDownloader.h" @interface ...
- IOS第二天多线程-01-延时执行
**********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implement ...
- 起底多线程同步锁(iOS)
iOS/MacOS为多线程.共享内存(变量)提供了多种的同步解决方案(即同步锁),对于这些方案的比较,大都讨论了锁的用法以及锁操作的开销,然后就开销表现排个序.春哥以为,最优方案的选用还是看应用场景, ...
随机推荐
- Ajax实现点击省份显示相应城市
功能:不用级联效果,自己写ajax,从接口读取省份城市数据,实现点击省份显示相应城市.后端根据省份ID,给前端返回城市. 一.DOM结构(套用blade模板) <div class=" ...
- 5.15[没什么营养的一段日子]A*
五月份没有写过blog. 期中考刚过......漫漫文化课,无尽头. 马上要为联赛开坑了,激动啊. 刚听了孙柘的演讲..%%% 最近刷的题只有一道启发式合并,一道分层图,一道差分约束..然后不知不觉破 ...
- apache activemq 学习笔记
0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...
- 【转】Xcelsius2008 水晶易表问题 部分汇总
要使用 Xcelsius 2008,需要安装 Adobe Flash 吗? 若要正常运行 Xcelsius 2008,必须安装 Adobe Flash Player 版本 9.如果在安装过程中没有安装 ...
- 笔记本做wifi热点
你可以开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网.点开始 所有程序 命令提示符右键管理员身份运行命令提示符 运行命令:ne ...
- QtCreator下运行opencv出现realloc():pointer invalid
解决办法是将qmake换成4.8的,qmake5.2的支持opencv支持的不是很好
- ACM 寻找最大数
寻找最大数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...
- jquery自己手写表单验证
<script type="text/javascript" src="../jquery-1.8.3.js"></script> / ...
- 基于jquery的图片懒加载js
function lazyload(option){ var settings={ defObj:null, defHeight: }; settings=$.extend(settings,opti ...
- [WP8.1UI控件编程]Windows Phone自定义布局规则
3.2 自定义布局规则 上一节介绍了Windows Phone的系统布局面板和布局系统的相关原理,那么系统的布局面板并不一定会满足所有的你想要实现的布局规律,如果有一些特殊的布局规律,系统的布局面板是 ...