UIView

基本属性

 


 

基本方法

- (void)bringSubviewToFront:(UIView *)view; //将Subview往前移动一个图层(与它的前一个图层对调位置)

- (void)sendSubviewToBack:(UIView *)view; //将Subview往后移动一个图层(与它的后一个图层对调位置)

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;//交换两个图层

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;//将指定的子视图移动到指定siblingSubview子视图的前面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;//将指定的子视图移动到指定siblingSubview子视图的后面

- (CGSize)sizeThatFits:(CGSize)size;     // return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (void)sizeToFit;                       // calls sizeThatFits: with current view bounds and changes bounds size.

 


其他

  • 苹果将控件的共同属性都抽取到父类UIView中
  • 所有的控件最终都继承自UIView

 


 

hitTest:withEvent:

  • 当你手指触摸屏幕后会发生以下事情:触摸事件被封装成一个UIEvent事件,去当前iOS操作系统的active app队列中取当前活跃的APP,把event传给它--->event传给UIApplication--->传给UIWindow的root view controller(rootVC)--->调用rootVC.view的所有subviews的hitTest:event:方法。哪个view的hitTest:event方法返回非nil值,则触摸事件就交给该view处理。
  • 判断point1是否在rect1内:bool CGRectContainsPoint(CGRect rect1, CGPoint point1)。
  • hitTest:withEvent:方法忽略隐藏(hidden=YES)的视图,禁止用户操作(userInteractionEnabled=YES)的视图,以及alpha级别小于0.01(alpha<0.01)的视图
  • 如果一个子视图的区域超过父视图的bound区域(父视图的clipsToBounds属性为NO,这样超过父视图bound区域的子视图内容也会显示),那么正常情况下对子视图在父视图之外区域的触摸操作不会被识别,因为父视图的pointInside:withEvent:方法会返回NO,这样就不会继续向下遍历子视图了。
例子:
  1. 在此例子中button,scrollview同为topView的子视图,但scrollview覆盖在button之上,这样在在button上的触摸操作返回的hit-test view为scrollview,button无法响应,可以修改topView的hitTest:withEvent:方法如下:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *result = [super hitTest:point withEvent:event];
    CGPoint buttonPoint = [underButton convertPoint:point fromView:self];
    if ([underButton pointInside:buttonPoint withEvent:event]) {
        return underButton;
    }
    return result;
}

 


 

坐标体系转换

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;//将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;//将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;//将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;//将rect从view中转换到当前视图中,返回在当前视图中的rect

 


UIView动画

UIView动画可以设置的动画属性有:
1、大小变化(frame)
2、拉伸变化(bounds)
3、中心位置(center)
4、旋转(transform)
5、透明度(alpha)
6、背景颜色(backgroundColor)
7、拉伸内容(contentStretch)

 

posted @ 2016-07-04 16:28  潜意识  阅读(135)  评论(0编辑  收藏  举报