第一部分Demo




简单计算器的实现 通过递归栈改变






第二部分,Views




view的内容




1、View是屏幕上一个矩形的空间

2、View处理两件事:画出矩形控件,并处理其中的事件


3、view组织架构:View是层结构的,View只有superView,可以有多个subView。子view的顺序和数组中的位置有关系,数字越大,越显示在后面。


4、UIWindow ,iOS中,UIWindow没有那么重要了,交给view,viewcontroller处理。





views 组织架构

可以在通过工具来管理view的组织架构,也可以通过代码:

-(void)addSubView:(UIView *)aView;

-(void)removeFromSuperview;

需要注意点的是:

通过父view添加子view



通过子view自己移除自己






view的坐标系统

单位:

CGFloat ,是个float/double数字,在obj-c里就要用这个单位

CGPoint,是个C结构体,x,y。CGPoint p = CGPointMake(33.2.22.3); 表示位置。

CGSize, 是个结构体,表示大小,width,height。

CGRect :由一个 CGPoint 和一个CGSize组成

用来描述view的主要四种类型。




坐标:


左上角是坐标的原点。坐标单位是点不是像素。


不用关心一个的点有都少像素,系统会自动适配。如果有需要获取一个点是多少像素,


通过这个属性:@property CGFloat contentScaleFactor。




view有3个属性和它的位置大小有关:


@property CGRect bounds; 自己内部的绘制空间,自定义的。




下面两个属性是父类用来定位你的view的属性。


@property CGRect center ;
@property CGRect frame ;


为什么bounds和frame 不一样呢?


因为view是可以伸缩,可以旋转的,看下图:



斯坦福大学公开课 iOS应用开发教程学习笔记(第四课)  Views 视图_ios



view的bounds只有200*250,但是view B的frame大多了是320*320。


设置center会改变frame,二者是关联的。




创建views


继承UIView。




通过alloc  init 创建view .


eg:


CGRect labelRect = CGRectMake(20, 20, 50, 30);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.text = @”Hello!”;
[self.view addSubview:label];




每个controller都有一个顶级的view,其他view都放在这个view上。


什么时候需要自定义view呢?


当需要特殊的图形,或控制触摸事件的时候,通常不要继承内置的控件,比如UIButton,没有优化过继承和重载,只优化了被拖出来使用。




drawRect


怎么绘图呢?


覆盖一个方法:-(void)drawRect:(CGRect)aRect;




红色警告:决不能自己调用drawRect:。系统调用这个方法。如果你需要重绘怎么办?


发送这两个消息:


- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)aRect;


绘制开销很大




写代码的信条就是代码行最优雅化,最少bug,最容易写,最容易读。




如何利用drawRect呢?


调用核心图形框架framework,它是C的接口,不是面向对象的。


Core Graphics framework的基本概念是,你创建一个环境,然后创建一些轨迹,比如直线和弧线,再设置他们都字体颜色样式等并描边或填充到轨迹里。这就是绘图的过程。




绘制图片和文字是主要的,文字和轨迹是一回事,它有很多精细的弧线组成。


绘制图片就是赋值二进制码。




context(环境) ,决定了你要在哪绘制。


每次drawRect是的环境都是不一样的,所以不要缓存context。




获取环境的代码:


CGContextRef context = UIGraphicsGetCurrentContext();  


几乎所有的drawRect都把这个放在第一行。


CGContextBeginPath(context);//开始
CGContextMoveToPoint(context, 75, 10);//移动 增加线 
CGContextAddLineToPoint(context, 160, 150);
CGContextAddLineToPoint(context, 10, 150);
CGContextClosePath(Path);//不是必需的


[[UIColor greenColor] setFill];
[[UIColor redColor] setStroke];//至今什么都没画
CGContextDrawPath(context,kCGPathFillStroke); //kCGPathFillStroke is a constant




上面代码:绘制的过程,先开始一个轨迹,移动轨迹的点,添加线,填充绿色,描边是红色,


颜色不需要指定context,默认就是当前的context。


调用 CGContextDrawpath在屏幕上画出来。

可以定义一个轨迹保存,CGPath,在其他环境重用。




颜色


UIColor


UIColor *red=[UIColor redColor];
UIColor *custom = [[UIColor alloc] initWithRed:(CGFloat)red blue:(CGFloat)blue green:(CGFloat)green alpha:(CGFloat) alpha];
[red setFill];//填充
[custom set];//填充 描边




透明:


@property CGFloat alpha  


@property BOOL opaque  


alpha  1,不透明, 0 完全透明。


 


隐藏view。


@property (nonatomic) BOOL hidden;  


myView.hidden = YES;




画文字


用UILabel显示文字


用UIFont设置字体 大小


UIFont *myFont = [UIFont systemFontOfSize:12.0];
UIFont *theFont = [UIFont fontWithName:@“Helvetica” size:36.0];
NSArray *availableFonts = [UIFont familyNames];




用NSString 来画文字


NSString *text = ...;
[text drawAtPoint:(CGPoint)p withFont:theFont]; // NSString instance method
CGSize  textSize = [text sizeWithFont:myFont];


因为UIKit,添加了绘制字符串的方法,所以NSString也能实现UI上的功能,用了categories .




画图像


UIImageView 


名称:


UIImage *image = [UIImage imageNamed:@“foo.jpg”];




二进制,网络:


UIImage *image = [[UIImage alloc] initWithContentsOfFile:(NSString *) fullPath];  
UIImage *image = [[UIImage alloc] initWithData:(NSData *)imageData];



通过CGContext函数


UIGraphicsBeginImageContext(CGSize); // draw with CGContext functions
UIImage *myImage = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();




在context环境里画出来


[image drawAtPoint:(CGPoint)p];  
[image drawInRect:(CGRect)r];  
[image drawAsPatternInRect:(CGRect)patRect; 
//drawAtPoint 会按原大小画出来
//drawInRect 会缩放。
//drawAsPatternInRect 会重复绘制来甜蜜指定的区域。




同样可以使用UIImage来表示PNG/JPG


NSData *jpgData = UIImageJPEGRepresentation((UIImage *)myImage,(CGFloat)quality);
NSData *pngData = UIImagePNGRepresentation((UIImage *)myImage);