框架

方便开发者开发出强大的功能,apple提供了各种各样的框架

  • UIKit:创建和管理应用程序的用户界面
  • QuartzCore:提供动画特效以及通过硬件进行渲染的能力
  • CoreGrephics:提供2D绘制的基于C的API
  • CoreLocation:使用GPS和WIFI获取位置信息
  • MapKit:为应用程序提供内嵌地图的接口
  • AVFoundation:音频、视频处理

常用代码存储

  • 右侧{}可用于存储代码,设置快捷键,备注,名称
@property (nonatomic,copy) NSString *<#name#> 
<#   #> 是高亮状态,方便修改

类前缀

选中项目,右边有ClassPerfix即为类前缀,以后创建的类都会加上前缀

UIKit坐标系

UIKit中,原点(0,0)在左上角

IBAction与IBOutlet

IBAction全称“Interface Builder Action”,“界面构建动作”,其实是动作监听,如按下,抬起,长按,从返回值相当于void
IBOutlet全称“Interface Builder Outlet”,翻译过来就是“界面构建输出口”,“插座变量”
只有声明是 IBActionIBOutlet才能跟storyboard进行连线

UILabel

UILabel *lbl = [[UILabel alloc] init];
//需要限制宽度
lbl.numberOfLines = 0;

UILabel富文本

    NSString *time = @"6天24小时60分";
    NSString *str = [[NSString alloc]initWithFormat:@"请同意   您需在%@内处理",time];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexStr:@"#EA4E3D"]} range:NSMakeRange(0,3)];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexStr:@"#EA4E3D"]} range:NSMakeRange(9,9)];
    [self.tipLabel1 setAttributedText:string];

UIView

UIViewUIViewController

  • UIView负责数据的展示、采集用户输入、监听用户事件
  • UIViewController处理每个UIView的创建、销毁、触发事件后的事件处理

UIView绑定

点击辅助控制器,按住shift点击view,拉到controller.m文件里面,自动生成代码

UIView的常见方法

//添加子控件
-(void)addSubview:(UIView *)view;

//从父控件移除 
-(void)removeFromSuperview;

//根据tag识别对应的控件(一般是子控件)
-(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;
  • center
CGRect originBounds = self.btnIcon.bounds;
originBounds.size.width += 10;
originBounds.size.height += 10;
self.btnIcon.bounds = originBounds;
  • bounds

多个控件由一个方法控制

  • 修改控件属性tag
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:UIControlStateNormal];

//加载图片
UIImage *imgNormal = [UIImage imageNamed:@"testPictuce"];
//设置默认状态背景图片
[button setBackgroundImage:imgNormal forState:UIControlStateNormal];
//设置按钮frame
button.frame = CGRectMake(50,100,100,100);
//注册事件
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

//添加进view
[self.view addSubview:button];

UIButton

  • normal(普通状态)
    默认情况Default
    对应的枚举常量:UIControlStateNormal
  • highlighted(高亮状态)
    按钮按下未松开
    对应的枚举常量:UIControlStateHighlighted
  • disabled(失败状态,不可用状态)
    enabled属性为NO,则处于disable状态,代表按钮不可被点击
    对应的枚举常量:UIControlStateDisabled

UIButton和UIImageView的区别

  • UIButton能监听点击事件,UIImageView默认情况不能
  • UIButton可以在不同状态显示不同的图片
  • UIButton既可以显示文字,又能显示图片(能显示两张图片,backgroundImageImage

不可点击又不处于disable状态,可以关闭在设置里面的用户交互,View->Interaction->User Interaction Enabled关闭

取消点击高亮状态可以在设置Drawing选择

设置按钮的背景图片

  • 设置不同状态下的背景图片
    为了保证高亮状态正常显示,按钮的type设置为custom

内边距可以在设置insert

隐藏按钮

sender.hidden =YES;

获取按钮的文字

获取按钮的文字
NSString *text = sender.currentTitle;

获取指定状态下的文字
NSString *text = [sender titleForState:UIContrlStateNormal];

UIScrollView

用来实现滚动和缩放
UITextView继承UIScrollView

告诉UIScrollView里面图片的实际大小
self.scrollView.contentSize = self.imgView.frame.size; 

UIScrollView无法滚动

  • 没有设置contentSize
  • scrollEnable = NO
  • 没有接受到触摸事件:userInteractionEnable = NO
  • 没有取消autolayoutxcode6.1下不需要取消了)

UIScrollView常用属性:contentOffset 偏移

@property(nonatomic) CGPoint contentOffset;
UIScrollView左上角的偏移,就是内容滚动到什么位置
CGPoint point = self.scrollView.contentOffset;

直接修改
self.scrollView.contenOffset = CGPointMake(0,-50);

也可以通过下面自带动画的方法修改
[self.scrollView setContentOffset:point animated:YES];

UIScrollView常用属性:contentInset

@property(nonatomic) UIEdgeInsets contentInset;
内容的内边距

//上左下右,逆时针
self.scrollView.contentInset = UIEdgeInsetsMake(100,50,30,5);

UIScrollView其他属性:contentInset

@property(nonatomic) BOOL bounces;
是否有弹簧效果

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
是否能滚动

隐藏滚动指示器,左右显示条

 self.scrollView.showsHorizontalScrollIndcator = NO;
 self.scrollView.showsVerticalScrollIndcator = NO;

通过代理监听UIScrollView滚动

  • 设置好UIScrollView的代理对象
  • 当用户开始拖拽时,调用scrollViewWillBeginDragging方法
  • 滚动到某个位置时,调用scrollViewDidScroll方法
  • 用户停止拖拽时,调用scrollViewDIdEndDragging: willDecelerate:方法

步骤

//1.设置代理对象 将控制器作为代理对象
self.scrollView.delegate = self;
//1.或是通过拖线 view -> delegate -> 拖到view controller 设置代理

//2.控制器遵守代理协议,可以在实现文件加上
@interface ViewController() <UIScrollViewDelegate>

//3.添加方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"用户开始拖拽");
}

通过代理监听UIScrollView缩放

  • 设置缩放viewForZoomingInScrollView
  • 即将开始缩放scrollViewWillBeginZooming
  • 正在缩放scrollViewDidZoom
  • 缩放完毕scrollViewDidEndZooming
@property (weak,nonatomic)IBOutlet UIImageView *imgView;
@property (weak,nonatomic)IBOutlet UIScrollView *scrollView;

self.scrollView.maximumZoomScale = 3.5;//最大缩放3.5倍
self.scrollView.minimumZoomScale = 0.5;//最小缩放0.5倍

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
  return self.imgView;//返回缩放的子控件
}

UIScollView实现图片横向滚动

//创建多个UIImageView添加到ScrollView中
for(int i=0;i<5;i++){
UIImageView *imgView = [[UIImageView alloc] init];
//设置图片
NSString *imgName = [NSString stringWithFormat:@"img_@02d",i+1];
imgView.image = [UIImage imageNamed:imgName];

//计算x值
CGFloat imgX = i*imgW;
imgView.frame = CGRectMake(imgX,imgY,imgW,imgH);

[self.scrollView addSubview:imgView];
}

//设置UIScrollView的contenSize(实际大小)
CGFloat maxW = self.scrollView.frame.size.width*5;
self.scrollView.contentSize = CGSizeMake(maxW,0);

//实现分页效果
//取决于UIScrollView的宽度,而不是图片
self.scrollView.pagingEnabled = YES;

//隐藏水平滚动指示器
self.scrollView.showHorizontalScrollIndicator = NO;

图片自动滚动

  • 通过定时器NSTime,获取偏移再设置偏离contentOffset,或者获取页码再设置偏离也可以
  • 注意拖拽时,通过监听拖拽开始结束,应该停止或开始计时器,不然容易产生bug

UIPageControl(页面小白点)

UIScollView没关系,需要自己创建关联

@property (weak,nonatomic)IBOutlet UIPageControl *pageControl;

self.pageControl.numberOfPages = 5;//页数
self.pageControl.currentPage = 0;//当前页
[self.scrollView bringSubviewToFront:self.pageControl];//设置最上方

根据UIScollView的监听设置self.pageControl.currentPage

transform属性

利用transform属性可以修改控件的位置、缩放、旋转
transform是结构体(不能直接修改)
CGFloat angle是弧度不是角度,360°=2π,180°=π

//创建transform属性:位置、缩放、旋转
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,CGFloat ty);
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx,CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle);
//在某个transform的基础叠加
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,CGFloat tx,CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t,CGFloat sx,CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,CGFloat angle);
//清空transform属性
view.transform = CGAffineTransformIdentity;
//3D旋转 可以绕x,y,z轴旋转
CGAffineTransform3DMakeRotation(M_PI_4,0,1,0);//角度 x,y,z. make函数不叠加,转到这个角度就停止了
CGAffineTransform3DRotate(M_PI_4,0,1,0);//角度 x,y,z. 继续叠加,每次旋转M_PI_4 45度
  • 例子
-(IBAction)move){
//直接赋值,平移到基于原点(0,0)平移到-50的位置
self.btnIcon.transform =  CGAffineTransformMakeTranslation(0,-50);
}
-(IBAction)move){
//基于现在的值平移向上50
self.btnIcon.transform =  CGAffineTransformTranslate(self.btnIcon.transform,0,-50);
}
M_PT_2是90self.btnIcon.transform = CGAffineTransformMakeRotation(M_PT_2);

文件路径、plist文件

  • 放置Supporing Files文件夹下
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"];

UIImage的图片处理方式

拉伸图片

UIImage *imgNormal = [UIImage imageNamed:@"testPictuce"];

//用平铺的方式拉伸图片 stretchableImageWithLeftCapWidth是距离左边的距离 topCapHeight是距离上边的距离,可以使图片拉大而不变形
imgNormal = [imgNormal stretchableImageWithLeftCapWidth: imgNormal.size.width*0.5 topCapHeight:imgNormal.size.height*0.5];

//图片模式
imgNormal.contentMode = [UIViewContentModeCenter];//图片居中
//图片超出部分不截取
imgNormal.clipToBounds = NO;

UIImage截取图片

CGImageRef *imageRef = CGImageCreatWithImageInRect(image.CGImage,CGRectMake(x,y,w,h));
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];

textInput输入框

textInput默认最左端有个UIView

UIView *myLeftView = [[UIView alloc] init];
myLeftView.frame = CGRectMake(0,0,5,1);

//把myLeftView设置给文本框
self.txtInput.leftView = myLeftView;
//设置显示模式
self.txtInput.leftViewMode = UITextFieldViewModeAlways;

UIImageView

  UIImage *mainImage = [UIImage imageNamed:@"background"];
  UIImageView *mainImageView = [[UIImageView alloc]init];
  mainImageView.image = mainImage;
  mainImageView.contentMode = UIViewContentModeScaleAspectFill;
  mainImageView.clipsToBounds = YES;
  [self.view addSubview:mainImageView];

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];
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐