框架
方便开发者开发出强大的功能,apple提供了各种各样的框架
- UIKit:创建和管理应用程序的用户界面
- QuartzCore:提供动画特效以及通过硬件进行渲染的能力
- CoreGrephics:提供2D绘制的基于C的API
- CoreLocation:使用GPS和WIFI获取位置信息
- MapKit:为应用程序提供内嵌地图的接口
- AVFoundation:音频、视频处理
类前缀
选中项目,右边有ClassPerfix即为类前缀,以后创建的类都会加上前缀
UIKit坐标系
UIKit中,原点(0,0)在左上角
IBAction与IBOutlet
IBAction全称“Interface Builder Action”,“界面构建动作”,其实是动作监听,如按下,抬起,长按,从返回值相当于void IBOutlet全称“Interface Builder Outlet”,翻译过来就是“界面构建输出口”,“插座变量” 只有声明是 IBAction与IBOutlet才能跟storyboard进行连线
Text把键盘叫回
[self.对象 resignFirstResponder];
- 每个控制器都会管理一个view,view代表当前控制的,让其停止编辑
[self.view endEditing:YES];
UIView
UIView与UIViewController
- UIView负责数据的展示、采集用户输入、监听用户事件
- UIViewController处理每个UIView的创建、销毁、触发事件后的事件处理
UIView绑定
点击辅助控制器,按住shift点击view,拉到controller.m文件里面,自动生成代码
UIView的常见方法
-(void)addSubview:(UIView *)view;
-(void)removeFromSuperview;
-(UIView *)viewWithTag:(NSInteger)tag;
子控件和父控件
self.view.subviews
self.view.superviews
控件的frame,center,bounds
frame包括控件的大小和坐标
CGRect originFrame = self.控件.frame;
originFrame.origin.y = 10;
self.控件.frame = originFrame;
CGRect originBounds = self.btnIcon.bounds;
originBounds.size.width += 10;
originBounds.size.height += 10;
self.btnIcon.bounds = originBounds;
多个控件由一个方法控制
switch(sender.tag){
case 10:
break;
case 20;
break;
}
- 根据tag获取控件:父控件可以根据tag找到子控件
UITextField *txt = [self.view viewWithTag:1000];
txt.text = @"txt";
动态添加控件
- viewDidLoad方法中写,控制器加载完view后执行的代码
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"button1" forState:UIControlStateNormall];
UIImage *imgNormal = [UIImage imageNamed:@"testPictuce"];
[button setBackgroundImage:imgNormal forState:UIControlStateNormal];
button.frame = CGRectMake(50,100,100,100);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton
- normal(普通状态)
默认情况Default 对应的枚举常量:UIControlStateNormal - highlighted(高亮状态)
按钮按下未松开 对应的枚举常量:UIControlStateHighlighted - disabled(失败状态,不可用状态)
enabled属性为NO,则处于disable状态,代表按钮不可被点击 对应的枚举常量:UIControlStateDisabled
UIButton和UIImageView的区别
- UIButton能监听点击事件,UIImageView默认情况不能
- UIButton可以在不同状态显示不同的图片
- UIButton既可以显示文字,又能显示图片(能显示两张图片,backgroundImage和Image)
设置按钮的背景图片
- 设置不同状态下的背景图片
为了保证高亮状态正常显示,按钮的type设置为custom
transform属性
利用transform属性可以修改控件的位置、缩放、旋转 transform是结构体(不能直接修改) CGFloat angle是弧度不是角度,360°=2π,180°=π
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,CGFloat ty);
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx,CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle);
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,CGFloat tx,CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t,CGFloat sx,CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,CGFloat angle);
view.transform = CGAffineTransformIdentity;
-(IBAction)move){
self.btnIcon.transform = CGAffineTransformMakeTranslation(0,-50);
}
-(IBAction)move){
self.btnIcon.transform = CGAffineTransformTranslate(self.btnIcon.transform,0,-50);
}
文件路径、plist文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"pictuce.plist" ofType:nil];
介绍:[NSBundle mainBundle]获取app安装到手机上的根目录,搜索pathForResource:@"pictuce.plist"文件,ofType:nil是后缀,找到文件路径返回给path
NSArray *array = [NSArray arrayWithContentsOfFile:path];
UIImage
png格式可以省略扩展名
UIImage *imgNormal = [UIImage imageNamed:@"testPictuce"];
动画
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDuration:2];
self.btnIcon.center=centerPoint;
[UIView commitAnimations];
[UIView animateWithDuration:0.5 animations:^{
self.btnIcon.center=centerPoint;
}];
UIImageView帧动画(两种加载方式)
UIImageView帧动画相关的属性和方法
@property(nonatomic,copy)NSArray *animationImages;
需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)
@property(nonatomic)NSTimeInterval animationDuration;
帧动画的持续时间
@property(nonatomic)NSInterger animationRepeatCount;
帧动画的执行次数(默认无限循环)
-(void)startAnimating;
开始执行帧动画
-(void)stopAnimating;
停止执行帧动画
-(BOOL)isAnimating;
是否正在执行帧动画
MSMutableArray *arrayM = [MSMutableArray array];
UIImage *imgCat = [UIImage imageNamed:@"drink01.jpg"];
[arrayM addObject:imgCat];
设置图片
self.imgViewCat.animationImages = arrayM;
设置持续时间
self.imgViewCat.animationDuration = self.imgViewCat.animationImages.count *0.1;
设置是否重复播放
self.imgViewCat.animationRepeatCount = 1;
开始动画
[self.imgViewCat startAnimating];
- 通过iamgeNamed:加载图片,加载好的图片会一直保存写在内存中,不会释放,这样下次如果再使用同样的图片的时候就不需要再重新加载了,因为内存里面已经有了
- 缺点就是:如果加载了大量的图片,那么这些图片会一直保留在内存中,导程序占用内存过大(这就叫缓存),没有强指针引用也不会销毁
- 解决方法:根据路径加载,该方法会销毁之前加载的图片,没有强指针引用就销毁
NSString *path = [[NSBundle mainBundle] pathForResource:@"cat.png" ofType:nil];
UIImage *imgCat = [UIImage imageWithContentsOfFile:path];
最后加上执行完毕清空图片缓存,调用延迟方法
将这个方法延迟运行:[self.imgViewCat setAnimationImages:nil];
[self.imgViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imgViewCat.animationImages.count*0.1];
|