1.向UIVIew 中加入子视图
- (void)addSubview:(UIView *)view;
//注释:添加一个子视图到接收者并让它在最上面显示出来;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
//注释:向指定的子视图之下,插入该视图;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
//注释:向指定的子视图之上,插入该视图。
2.将视图带到到最上面,或最下面
//*最上面
-(void)bringSubviewToFront:(UIView *)view;
//*最下面
-(void)sendSubviewToBack:(UIView *)view;
3.两个视图之间的交换
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
//注释:类比数组理解
4.将子视图从父视图中移除掉
-(void)removeFromSuperview;
5.UIView中的坐标转换
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button
// 在controllerA中实现:
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc为btn在controllerA中的rect
或当已知btn时:
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
6.系统自动调用(留给子类去实现)
- (void)didAddSubview:(UIView *)subview;
//注释:当加入视图完成后
- (void)willRemoveSubview:(UIView *)subview;
//注释:当视图移除完成后
- (void)willMoveToSuperview:(UIView *)newSuperview;
//注释:在删除视图之后调用
- (void)didMoveToSuperview;
//注释:
- (void)willMoveToWindow:(UIWindow *)newWindow;
//注释:当视图移动到新的WINDOW后调用
- (void)didMoveToWindow;
//注释:
7.判断是不是view的子控件或者子控件的子控件(是否为view的子类)
- (BOOL)isDescendantOfView:(UIView *)view;
8.通过tag获得对应的子控件(也可以或者子控件的子控件)
- (UIView *)viewWithTag:(NSInteger)tag;
9./**系统自动调用(留给子类去实现)**/
- (void)layoutSubviews;
//注释:控件的frame发生改变的时候就会调用,一般在这里重写布局子控件的位置和尺寸
重写了这个写方法后,一定调用[super layoutSubviews];
10.动画
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
//注释:
* duration为动画持续的时间。
* animations为动画效果的代码块。
下面是可以设置动画效果的属性:
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
举例:例如一个视图淡出屏幕,另外一个视图出现的代码:
[UIView animateWithDuration:1.0 animations:^{
firstView.alpha = 0.0;
secondView.alpha = 1.0;
}];
completion为动画执行完毕以后执行的代码块
[UIView animateWithDuration:2.0 animations:^{
oldImageView.alpha = 0.0;
newImageView.alpha = 1.0;
//imageView.center = CGPointMake(500.0, 512.0);
}completion:^(BOOL finished){
[UIView animateWithDuration:4.0 animations:^{
newImageView.center = CGPointMake(500.0, 512.0);
}];
}];
11.在接收者视图中绘制矩形
- (void)drawRect:(CGRect)rect
注释:参数 rect 一个定义的需要绘制的矩形
@property(nonatomic) CGAffineTransform transform; // default is CGAffineTransformIdentity
//注释:形变属性(平移\缩放\旋转)
@property(nonatomic,getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled;
//注释:YES:支持多点触摸 默认是 NO
@property(nonatomic,readonly,retain) CALayer *layer
//注释:图层(可以用来设置圆角效果\阴影效果)
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
//注释:YES:能够跟用户进行交互 默认是YES
@property(nonatomic,readonly) UIView *superview;
//注释:父控件
@property(nonatomic,readonly,copy) NSArray *subviews;
//注释:子控件(新添加的控件默认都在subviews数组的后面, 新添加的控件默认都显示在最上面\最顶部)
@property(nonatomic,readonly) UIWindow *window;
//注释:获得当前控件所在的window(window常为一个,但可以不止一个)
@property(nonatomic) BOOL clipsToBounds;
//注释:YES : 超出控件边框范围的内容都剪掉
@property(nonatomic,copy) UIColor *backgroundColor;
//注释:背景色 默认:nil
@property(nonatomic) CGFloat alpha;
//注意:透明度(0.0~1.0) 默认:1.0
@property(nonatomic,getter=isOpaque) BOOL opaque;
//注释:YES:不透明 NO:透明 默认:YES
@property(nonatomic,getter=isHidden) BOOL hidden;
//注释:YES : 隐藏 NO : 显示
@property(nonatomic) UIViewContentMode contentMode;
//注释:内容模式 默认是UIViewContentModeScaleToFill