---------------
放大缩小
步骤:
1、导入素材 拖button图片控件 设置控件属性为custom 取消自动布局
2、连线方法
3、同时修改frame或者bounds的宽度和高度
4、加入收尾动画 (这个不常用)
- (IBAction)zoom:(UIButton *)button { CGRect bounds = self.image.bounds; if (button.tag) { //放大 //取出bounds或frame //修改bounds bounds.size.width += 20; bounds.size.height +=20; }else{ //缩小 bounds.size.width -=20; bounds.size.height -=20; } [UIView beginAnimations:nil context:nil]; // 设置动画时长 [UIView setAnimationDuration:1.0]; // 设置bouds self.image.bounds = bounds; [UIView commitAnimations]; }
---------
button图像的transform位移形变
步骤
1、改变button图片的transform
self.image.transform = CGAffineTransformTranslate(self.image.transform, 0, -20); 追加的方式改变transform
2、打印tranform
NSLog(@"%@",NSStringFromCGAffineTransform(self.image.transform));
-------
button图像的transform放大
CGFloat scale = (button.tag)?1.2f : 0.8; CGAffineTransform transform = CGAffineTransformScale(self.image.transform, scale, scale); self.image.transform = transform;
-------
button图像的transform旋转
//旋转角度 CGFloat angle = (button.tag)?-M_PI_4:M_PI_4; self.image.transform = CGAffineTransformRotate(self.image.transform, angle);
------------
代码创建按钮
步骤:
1、根据stroyboard的上下集成各个属性 找到相应的.h类文件对应的属性名称
2、代码创建custom类型button(alloc init创建的是custom类型的,最纯洁的代码创建的) 并交给控制器button属性 (storyboard中是将storyboard中的button连线 就是赋值给控制器button属性 连线是为了后期代码修改) 创建系统自带的button 这里暂时不连线
3、设置button属性
4、添加自定义到控制器视图上 self.view addSubviews上
- (void)viewDidLoad { [super viewDidLoad]; //创建系统自带的button UIButton * btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; btn.center = CGPointMake(50, 50); [self.view addSubview:btn]; //代码创建custom button UIButton *image = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; //连线交给控制器属性Image self.image = image; image.backgroundColor = [UIColor redColor]; //设置背景图片 [image setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal]; [image setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted]; //设置标题文字 [image setTitle:@"点我啊" forState:UIControlStateNormal]; [image setTitle:@"我是abc" forState:UIControlStateHighlighted]; //设置文字颜色 [image setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [image setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; //设置标题文字对齐方式 image.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom; [self.view addSubview:image]; }
---------
系统头文件修改后处理
xcode->preferences->locations->derived data 进入 删除 derived data换成目录里面的所有文件
---------
button点击切换图片界面
步骤
1、导入png图片到image.assets (图片抽取放到一个folder中 选择图片->new folder from selection 变成蓝色的文件夹)
2、图片切换代码搭建
- (void)viewDidLoad { [super viewDidLoad]; // number _number = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)]; _number.text = @"1/5"; _number.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_number]; // image CGFloat imageW = 200; CGFloat imageH = 200; CGFloat imageX = (self.view.bounds.size.width - imageW) * 0.5; CGFloat imageY = CGRectGetMaxY(_number.frame) + 20; _image = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)]; // _image.backgroundColor = [UIColor redColor]; _image.image = [UIImage imageNamed:@"biaoqingdi"]; [self.view addSubview:_image]; // descriptionLabel CGFloat y = CGRectGetMaxY(_image.frame); _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, 100)]; _descriptionLabel.text = @"神马表情"; _descriptionLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_descriptionLabel]; // 左边按钮 _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _leftButton.center = CGPointMake(_image.frame.origin.x * 0.5, _image.center.y); [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:_leftButton]; // 右边的按钮 _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _rightButton.center = CGPointMake(self.view.bounds.size.width - _image.frame.origin.x * 0.5 , _image.center.y); [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:_rightButton]; }
-------
界面图片切换数据处理
1、界面搭建
2、控件懒加载(保证只加载一次)到界面 ,@property数组数据懒加载 数组数据最好用plist
(不用plist前的土办法:数组包字典 数据
NSDictionary *dict1 = @{@"name":@"biaoqingdi",@"desc":@"表情111111"};
NSDictionary *dict2 = @{@"name":@"wangba",@"desc":@"王八22222222"};
_imageList = @[dict1,dict2,dict3];
)
3、viewDidLoad中设置控件赋值 顺便执行了控件懒加载
// // ViewController.m // 03-图片查看器 // // Created by imac on 16/2/21. // Copyright (c) 2016年 china. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic,strong) UILabel *number; @property (nonatomic,strong) UIImageView *image; @property (nonatomic,strong) UILabel *descriptionLabel; @property (nonatomic,strong) UIButton *leftButton; @property (nonatomic,strong) UIButton *rightButton; @property (nonatomic,assign) int index; @property (nonatomic,strong) NSArray *imageList; @end @implementation ViewController - (NSArray *)imageList { if (_imageList == nil) { // 图片name和description文字 // 获得plist array数据 NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"]; _imageList = [NSArray arrayWithContentsOfFile:path]; } // NSLog(@"%@",_imageList); return _imageList; } - (UILabel *)number { if (_number == nil) { _number = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)]; // _number.text = @"1/5"; _number.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_number]; } return _number; } - (UIImageView *)image { if (_image == nil) { CGFloat imageW = 200; CGFloat imageH = 200; CGFloat imageX = (self.view.bounds.size.width - imageW) * 0.5; CGFloat imageY = CGRectGetMaxY(_number.frame) + 20; _image = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)]; // _image.backgroundColor = [UIColor redColor]; // _image.image = [UIImage imageNamed:@"biaoqingdi"]; [self.view addSubview:_image]; } return _image; } - (UILabel *)descriptionLabel { if (_descriptionLabel == nil) { CGFloat y = CGRectGetMaxY(_image.frame); _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, 100)]; // _descriptionLabel.text = @"神马表情"; _descriptionLabel.textAlignment = NSTextAlignmentCenter; _descriptionLabel.numberOfLines = 0; [self.view addSubview:_descriptionLabel]; } return _descriptionLabel; } - (UIButton *)leftButton { if (_leftButton == nil) { _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _leftButton.center = CGPointMake(_image.frame.origin.x * 0.5, _image.center.y); [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; _leftButton.tag = -1; [self.view addSubview:_leftButton]; [_leftButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside]; } return _leftButton; } - (UIButton *)rightButton { if (_rightButton == nil) { _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _rightButton.center = CGPointMake(self.view.bounds.size.width - _image.frame.origin.x * 0.5 , _image.center.y); [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; _rightButton.tag = 1; [self.view addSubview:_rightButton]; // 监听按钮点击事件 [_rightButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside]; } return _rightButton; } - (void)viewDidLoad { [super viewDidLoad]; [self imageList]; //显示界面 [self show]; } - (void) next:(UIButton *)button { NSLog(@"%s ---- %d",__func__,self.index); self.index += button.tag; //重新显示界面 [self show]; } - (void)show { // number self.number.text = [NSString stringWithFormat:@"%d/%d",self.index+1,5]; // image self.image.image = [UIImage imageNamed:self.imageList[self.index][@"name"]]; // descriptionLabel self.descriptionLabel.text = self.imageList[self.index][@"desc"]; // leftButton self.leftButton.enabled = (self.index != 0); // rightButton self.rightButton.enabled = (self.index != 4); } @end
------
按钮添加监听事件
步骤 添加监听方法next 如果next: 则相当于 next:_rightButton 默认自身是第一个参数
[_rightButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
- (void) next { NSLog(@"%s",__func__); self.index++; }
------
取出包(mainBubdle)中的plist
(包中包含真正的应用程序 包含exec文件 资源等)
1、包路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"];
2、打印包路径 , finder中找到该包位置
NSLog(@"%@",paht);
直接abc.plist-》show in finder 查看的是该程序自己存放的位置 对应的plist 不是系统中的
--------
Strong weak变量
(强人牵狗 弱人只能看狗)
默认对象都是强引用的如 Person *p1 = [[Person alloc] init]; 弱引用 __weak Person *p1
Person对象必须要有强引用指向 否则立即会被回收,不能存在
Person *p = [[Person alloc] init] //强人p牵狗
__weak Person p2 = p;//弱人看狗
---------
TOM猫ImageView动画和清理数组
步骤:
xcode6.4取消 use Size Classes
viewController下拖入一个View
1、素材导入 button文件夹拖到image.assets中 里面都是png图片(3个勾选 create Folder),animations文件夹中都是 jpg的 拖到supporting files中 (3个勾选 create Group)
2、吃鸟图片连线方法 viewDidLoad方法删除
3、使用UIImageView的属性搜索animation相关
isAnimation则退出 animationImages存放图片数组 startAnimation
(
ImageNamed方法系统来管理的 在动画中不能用这个 否则内存占用越来越大
NSString *path = [NSBundle mainBundle] pathForResources:@"abc.jpg" ofType:nil];
使用[UIImage imageWithContentsOfFile:path];
image.assets中优先使用imageNamed这里的图片常驻内存, supporting Files中图片优先使用 imageWithContentsOfFile 这里都是临时使用的图片
)
4、等动画结束后 ,清空UIImageView的动画数组
[self performSelector:@selector(cleanup) withObject:nil afterDelay:self.tom.animationDuration];
- (void) cleanup
{
self.tom.animationImages = nil;
}
(或者用 [self performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; 一个方法即可 )
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *tom; @end @implementation ViewController - (IBAction)eat { // 如果正在动画,直接退出 if ([self.tom isAnimating]) return; // 动画图片的数组 NSMutableArray *arrayM = [NSMutableArray array]; // 添加动画播放的图片 for (int i = 0; i < 25; i++) { // 图像名称 NSString *imageName = [NSString stringWithFormat:@"angry_%02d.jpg", 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 = 1; // 动画时长 self.tom.animationDuration = self.tom.animationImages.count * 0.075; // 开始动画 [self.tom startAnimating]; [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; } @end
其他不需要了解的
【
方法2(不常用):@property (nonatomic,assign) CGFloat y;
self.y -=20;
self.image.transform = CGAffineTransformMakeTranslation(0, self.y); 相对于第一次的transform基础上增加y
旋转后 应当修改bounds center才是正解 用frame有时候会出问题
#pragma mark - 操作1
生成的带下划线 _noLabel不经过getter 和 setter方法
双击image.assets 弹出窗口 方便查看
_noLable.numberOfLine = 0;显示不够 自动换行
- (void) dealloc{NSLog(@"被释放了");} 判断对象是否被释放
其他项目中的代码直接拖进我的代码中
@property (nonatomic,strong) Person *p;
@property (nonatomic,copy) NSString *str;
@property (nonatomic,assign) int age;
拖线用weak
Button有一个currentTitle属性 因为标题分2种状态
mac拖图片到xcode中 拖到image.assets 3个勾选 create Folder,拖到supportingFiles中 3个勾选 create Group
】