UIView-iOS初学

UIView

初始化UIView

//Typical code ...
- (void)setup { ... }
- (void)awakeFromNib { [self setup]; } - (id)initWithFrame:(CGRect)aRect
{
    self = [super initWithFrame:aRect];
    [self setup];
    return self;
}



View 结构体

CGFloat
CGPoint:包含X、Y坐标
CGSize:包含宽度、高度
CGRect:包含以上两个CG。
CGRect aRect = CGRectMake(45.0, 75.5, 300, 500);

绘制的是“点”,不是像素。
@property CGFloat contentScaleFactor; //返回一个点是多少像素



View B’s bounds = ((0,0),(200,250))
View B’s frame = ((140,65),(320,320)) 
View B’s center = (300,225)


创建UIView

示例:创建一个label

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



永远不要调用drawRect:

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

实现drawRect

Core Graphics 基础流程 : 
上下文(绘制位置) 
CGContextRef context = UIGraphicsGetCurrentContext();

创建路径
设置字体和颜色等
填充描边

UIBezierPath 封装了上面所有东西。不用担心上下文
示例画一个三角形:
//初始化
UIBezierPath *path = [[UIBezierPath alloc] init];
//先找到第一个点,然后画两条线
[path moveToPoint:CGPointMake(75, 10)];
[path addLineToPoint:CGPointMake(160, 150)];
[path addLineToPoint:CGPointMake(10, 150]);
Close the path (connects the last point back to the first) [path closePath]; // 关闭曲线,画第三条线
画完以上这些,还不能显示,需要[path fill] [path stroke] 才真正显示

其他函数:
path.lineWidth = 2.0; // line width in points (not pixels)
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:(CGRect)bounds cornerRadius:(CGFloat)radius];//圆角矩形
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:(CGRect)bounds];//椭圆形

透明度

UIView 默认是白色的
//set @property BOOL opaque to NO
@property CGFloat alpha//透明度


subView是一个NSArray,[0]在最下面。



压入 取出

- (void)drawGreenCircle:(CGContextRef)ctxt {
    CGContextSaveGState(ctxt);
    [[UIColor greenColor] setFill];
// draw my circle
    CGContextRestoreGState(ctxt);
}
- (void)drawRect:(CGRect)aRect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor redColor] setFill];
// do some stuff
    [self drawGreenCircle:context];
// do more stuff and expect fill color to be red
}

绘制文本

//在p点写text文本
NSAttributedString *text = ...;
[text drawAtPoint:(CGPoint)p];
//文本大小
CGSize textSize = [text size];

绘制图像

创建图像
//1
UIImage *image = [UIImage imageNamed:@“foo.jpg”]; // you did this in Matchismo 

//2 Or create one from a named file or from raw data
UIImage *image = [[UIImage alloc] initWithContentsOfFile:(NSString *)fullPath];
UIImage *image = [[UIImage alloc] initWithData:(NSData *)imageData];

//3 Or you can even create one by drawing with CGContext functions
UIGraphicsBeginImageContext(CGSize);// draw with CGContext functions
UIImage *myImage = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();

显示图像
UIImage *image = ...;
[image drawAtPoint:(CGPoint)p];  // p是图片左上角坐标
[image drawInRect:(CGRect)r];  // r是图片大小
[image drawAsPatternInRect:(CGRect)patRect;  // 平铺图像,重复

Bounds 改变

当添加导航栏或旋转时,bounds被压缩或改变。默认情况下视图会拉伸。但是我们希望重绘。
@property (nonatomic) UIViewContentMode contentMode;
UIViewContentMode{Left,Right,Top,Right,BottomLeft,BottomRight,TopLeft,TopRight}
UIViewContentModeScale{ToFill,AspectFill,AspectFit} // bit stretching/shrinking
UIViewContentModeRedraw//calls drawRect: to redraw

属性

@property (nonatomic,strong) UITextField        *usernameTextField;
self.usernameTextField = usernameTextField;
设置成属性,再把新建的控件赋值给属性。之后可以在其他消息中使用这个属性。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值