UIView的常用方法及相关属性

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为动画执行完毕以后执行的代码块
options为动画执行的选项。可以参考这里
delay为动画开始执行前等待的时间
如何实现连续的动画?
可以在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    一个定义的需要绘制的矩形 
延伸:
子类重写这个方法如果他们确实要绘制他们自定义的视图。如果子类是其他视图的容器那么它不需要重写这个方法。默认的实现不做任何事情。如果你自定义的视图是一个UIView子类,你不需要去调用它的父类实现。注意如果它的父类实现绘制并且不透明属性为YES那么每一个子类都需要填充矩形。 
当这个方法被调用,接收者可以假定他的帧在坐标上已经转换,边界矩形已经应用;所有他要做的就是绘制自定义的方法。使用UIGraphicsGetCurrentContext方法去获取当前图形内容用来绘制,坐标原点在左上角。不要保留图片内容当他可以被drawRect:这个方法调用。
12.常用属性
@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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值