UIView头文件分析
UIView应该是所有iOS开发者非常常用的类之一了,看看头文件这个类都有啥
- (id)initWithFrame:(CGRect)frame;
UIView的初始化方法,初始化时传入坐标和宽高
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.
注释:默认是yes,如果设置为NO,这个视图就会忽略用户事件并且被移出事件队列
@property(nonatomic) NSInteger tag; // default is 0
UIView对象的tag值,可以通过这个查找UIView对象,默认为0
// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.
@property(nonatomic) CGRect frame;
控件的大小和位置.注释:可动画的属性.如果UIView使用了形变属性(操控了Layer),就不要用frame去设置大小和位置,因为此时这个控件的显示位置和实际位置不一致.要使用bounds属性和center属性代替(所以bounds和center就不介绍了,bounds默认坐标点是0,0.是相对自身的位置)
@property(nonatomic) CGAffineTransform transform; // default is CGAffineTransformIdentity. animatable
形变属性,默认是CGAffineTransformIdentity无形变 可动画属性
@property(nonatomic) CGFloat contentScaleFactor
// Higher scale factors indicate that each point in the view is represented by more than one pixel in the underlying layer. For example, if the scale factor is 2.0 and the view frame size is 50 x 50 points, the size of the bitmap used to present that content is 100 x 100 pixels.
注释:这个属性就是表示一个点表示几个像素,非retina屏幕一个点表示一个像素,retina屏幕一个点表示两个像素,具体看文档
@property(nonatomic,getter=isExclusiveTouch) BOOL exclusiveTouch; // default is NO
exclusiveTouch的意思是UIView会独占整个Touch事件,具体的来说,就是当设置了exclusiveTouch的 UIView是事件的第一响应者,那么到你的所有手指离开前,其他的视图UIview是不会响应任何触摸事件的,对于多点触摸事件,这个属性就非常重要,值得注意的是:手势识别(GestureRecognizers)会忽略此属性。
列举用途:我们知道ios是没有GridView视图的,通常做法是在UITableView的cell上加载几个子视图,来模拟实现 GridView视图,但对于每一个子视图来说,就需要使用exclusiveTouch,否则当同时点击多个子视图,那么会触发每个子视图的事件。当然 还有我们常说的模态对话框。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
// UIView事件传递的函数,这个函数会调用pointInside:withEvent:来判断点击的点是否在自己身上,如果在自己身上则会遍历子控件的hittest方法,如果没在自己身上就返回nil,
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds
// 判断某一个点在不在自己身上,可以通过覆写这个方法,来拒绝响应事件,所以
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
转换坐标和rect,非常实用的方法
@property(nonatomic) BOOL autoresizesSubviews; // default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes
是否要自动调节子控件的位置和大小,默认为YES
@property(nonatomic) UIViewAutoresizing autoresizingMask; // simple resize. default is UIViewAutoresizingNone
自动调节自己的位置大小选项,默认是不自动调节
- (CGSize)sizeThatFits:(CGSize)size; // return 'best' size to fit given size. does not actually resize view. Default is return existing view size
根据传入的size返回一个最适应的size,并不改变view的size,默认返回一个存在的view的size,覆写以后可以返回指定的size,供sizeToFit调用
- (void)sizeToFit; // calls sizeThatFits: with current view bounds and changes bounds size.
调用sizeThatFits:调节自身的size到适当
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
看其名知其意,分别是父控件,子控件的数组,和所属的window
- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
分别是将自己从父控件移除,在指定的位置插入到父控件中,交换子控件的顺序
- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
分别是添加一个子控件,在摸一个子控件下面插入一个子控件,在某一个子控件前面插入一个子控件
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
分别是将一个子控件放到最前面,或者将一个子控件放到最后面
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;
见名知意,这些方法默认什么都没做,供开发者自己覆写,监听view的改变
- (BOOL)isDescendantOfView:(UIView *)view; // returns YES for self.
- (UIView *)viewWithTag:(NSInteger)tag; // recursive search. includes self
1.判断当前view是不是传入那个view的子view或者孙view或者子孙孙view 2.根据tag遍历自身及子view查找对应tag值的view
// Allows you to perform layout before the drawing cycle happens. -layoutIfNeeded forces layout early
- (void)setNeedsLayout;
- (void)layoutIfNeeded;
- (void)layoutSubviews; // override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
1.下一次事件循环的时候去主动调用layoutSubViews 2立即调用layoutSubViews
3.默认是你么也没实现,就是供子类去覆写的,下面是layoutSubviews的调用情况
2、addSubview会触发layoutSubviews。
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
4、滚动一个UIScrollView会触发layoutSubviews。
5、旋转Screen会触发父UIView上的layoutSubviews事件。
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
- (void)drawRect:(CGRect)rect;
- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)rect;
视图创建完毕后会调用DrawRect方法,默认是什么也不做,你可以在这里用CoreGraphics进行绘图
setNeedDisplay调用以后会在下一个循环调用DrawRect方法,二Set....InRect,则会只更新Rect指定区域的视图,如果你在(100,100)画了一个点,并且用dispatch_once包裹住了,证明这个点只会画一次,如果setNeedsDisplay导致全局更新,这个点会被更新掉,setNeedsDisplayInRect如果rect不包含这个点则不会消失
@property(nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.
超出本view边界的子view是否需要裁剪
@property(nonatomic,copy) UIColor *backgroundColor UI_APPEARANCE_SELECTOR; // default is nil. Can be useful with the appearance proxy on custom UIView subclasses.
背景色
@property(nonatomic) CGFloat alpha; // animatable. default is 1.0
透明度,默认为1(不透明)
@property(nonatomic,getter=isOpaque) BOOL opaque; // default is YES. opaque views must fill their entire bounds or the results are undefined. the active CGContext in drawRect: will not have been cleared and may have non-zeroed pixels
是否为不透明
@property(nonatomic) BOOL clearsContextBeforeDrawing; // default is YES. ignored for opaque views. for non-opaque views causes the active CGContext in drawRect: to be pre-filled with transparent pixels
清除上下文缓冲区用的,UIScrollView应该会设置为NO,应该来回清除会耗费性能
@property(nonatomic,getter=isHidden) BOOL hidden; // default is NO. doesn't check superviews
是否隐藏
@property(nonatomic) UIViewContentMode contentMode; // default is UIViewContentModeScaleToFill
// 内容模式,默认为自适应填充
/*
-tintColor always returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself).
If no non-default value is found, a system-defined color is returned.
If this view's -tintAdjustmentMode returns Dimmed, then the color that is returned for -tintColor will automatically be dimmed.
If your view subclass uses tintColor in its rendering, override -tintColorDidChange in order to refresh the rendering if the color changes.
*/
@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(7_0);
返回一个系统颜色
/*
-tintAdjustmentMode always returns either UIViewTintAdjustmentModeNormal or UIViewTintAdjustmentModeDimmed. The value returned is the first non-default value in the receiver's superview chain (starting with itself).
If no non-default value is found, UIViewTintAdjustmentModeNormal is returned.
When tintAdjustmentMode has a value of UIViewTintAdjustmentModeDimmed for a view, the color it returns from tintColor will be modified to give a dimmed appearance.
When the tintAdjustmentMode of a view changes (either the view's value changing or by one of its superview's values changing), -tintColorDidChange will be called to allow the view to refresh its rendering.
*/
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);
tintcolor调整的方式,没看太懂
/*
The -tintColorDidChange message is sent to appropriate subviews of a view when its tintColor is changed by client code or to subviews in the view hierarchy of a view whose tintColor is implicitly changed when its superview or tintAdjustmentMode changes.
*/
- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);
tintcolor改变的回调方法
_imageVIew.tintColor = [UIColor redColor]; //_imageVIew.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; _imageVIew.image = [_imageVIew.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

浙公网安备 33010602011771号