在UIView中添加或删除子视图
1 2
| - (void)addSubview:(UIView *)view;
- (void)removeFromSuperview; |
插入视图的z-index控制
1 2 3 4
| - (void)insertSubview:(UIView *)view atIndex:(int)index;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)view;
- (void)exchangeSubviewAtIndex:(int)index withSubviewAtIndex:(int)otherIndex; |
使用代码创建图片的4个步骤
1. 创建一个有尺寸的 CGGraphicsContext
2. 绘制
3. 将上下文(context)截图成位图
4. 清除
例如:
1 2 3 4 5 6 7 8 9 10
| - (UIImage *)polygonImageOfSize:(CGSize)size {
UIImage *result = nil;
UIGraphicsBeginImageContext (size);
// call your drawing code...
result = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();
return result;
} |
隐藏视图theView.hidden = YES;
和UIView相关的坐标数据结构:CGPoint, CGSize, CGRect
frame和bounds的区别
用代码创建View
1 2
| CGRect frame = CGRectMake(0, 0, 200, 150);
UIView *myView = [[UIView alloc] initWithFrame:frame]; |
如
1 2 3 4 5
| CGRect frame = CGRectMake(20, 45, 140, 21);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[window addSubview:label];
[label setText:@”Number of sides:”];
[label release]; // label now retained by window |
自定义绘制视图
1
| - (void)drawRect:(CGRect)rect; |
自定义视图事件
1 2 3 4
| - (void)touchesBegan :(NSSet *)touches withEvent :(UIEvent *)event;
- (void)touchesMoved :(NSSet *)touches withEvent :(UIEvent *)event;
- (void)touchesEnded :(NSSet *)touches withEvent :(UIEvent *)event;
- (void)touchesCancelled :(NSSet *)touches withEvent :(UIEvent *)event; |
不要直接调用drawRect,使用
1
| - (void)setNeedsDisplay; |
UIKit提供的绘图方法
1 2
| UIRectFill(CGRect rect);
UIRectFrame(CGRect rect); |
在drawRect中获得图像上下文(context)
1
| (CGContextRef)UIGraphicsGetCurrentContext(void); |
获取并设置画笔颜色
1 2 3
| UIColor *redColor = [UIColor redColor];
[redColor set];
// drawing will be done in red |
获取并设置字体
1 2
| UIFont *font = [UIFont systemFontOfSize:14.0];
[myLabel setFont:font]; |
例子:
1 2 3 4 5 6 7 8 9 10
| - (void)drawRect:(CGRect)rect {
CGRect bounds = [self bounds];
[[UIColor grayColor] set];
UIRectFill (bounds);
CGRect square = CGRectMake (10, 10, 50, 100);
[[UIColor redColor] set];
UIRectFill (square);
[[UIColor blackColor] set];
UIRectFrame (square);
} |
绘制Path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| - (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] set];
UIRectFill ([self bounds]);
CGContextBeginPath (context);
CGContextMoveToPoint (context, 75, 10);
CGContextAddLineToPoint (context, 10, 150);
CGContextAddLineToPoint (context, 160, 150);
CGContextClosePath (context);
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath (context, kCGPathFillStroke);
} |
获取图片对象的三种方法
从application bundle中获得image
从磁盘读取图片
1
| -[UIImage initWithContentsOfFile :(NSString *)path ] |
从内存数据中创建图片
1
| Use -[UIImage initWithData :(NSData *)data ] |
通过上下文创建图片有以下四步
1. 创建一个带尺寸的UIGraphicsBeginImageContext
2. 绘图
3. 截图,返回UIImage对象
4. 清理
1 2 3 4 5 6 7 8 9 10
| - (UIImage *)polygonImageOfSize:(CGSize)size {
UIImage *result = nil;
UIGraphicsBeginImageContext (size);
// call your drawing code...
result = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();
return result;
} |
图片绘图方法
1 2 3 4 5
| - [UIImage drawAtPoint :(CGPoint )point ]
- [UIImage drawInRect :(CGRect )rect ]
- [UIImage drawAsPatternInRect :(CGRect )rect ]
// 绘制字符串
- [NSString drawAtPoint :(CGPoint )point withFont :(UIFont *)font ] |
动画
老方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| - (void)showAdvancedOptions {
// assume polygonView and optionsView
[UIView beginAnimations:@”advancedAnimations” context:nil];
[UIView setAnimationDuration:0.3];
// make optionsView visible (alpha is currently 0.0)
optionsView.alpha = 1.0;
// move the polygonView down
CGRect polygonFrame = polygonView.frame;
polygonFrame.origin.y += 200;
polygonView.frame = polygonFrame;
[UIView commitAnimations];
} |
UIView 可以把动画事件代理给某对象
1
| [UIView setAnimationDelegate:myController]; |
对象可实现此2方法对动画开始或结束事件进行处理
1 2
| - (void)animationWillStart :(NSString *)animationID context :(void *)context;
- (void)animationDidStop :(NSString *)animationID finished :(NSNumber *)finished context :(void *)context; |
也可以通过自定义的selector指定处理事件的action
1 2
| [UIView setAnimationWillStartSelector:@selector(animationWillStart)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)]; |
关于UIView变形的一些函数
1 2 3 4
| CGAffineTransform Functions (just a small example set)
CGAffineTransformScale (transform, xScale, yScale)
CGAffineTransformRotate (transform, angle)
CGAffineTransformTranslate (transform, xDelta, yDelta) |
使用NSUserDefaults读写状态值
获取对象方法
读写方法
1 2 3 4
| - (int)integerForKey :(NSString *)key;
- (void)setInteger :(int)value forKey :(NSString *)key;
- (id)objectForKey :(NSString *)key;
- (void)setObject :(id)value forKey :(NSString *)key; |