下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客
一、自定义一个View
// // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } //下面两个方法主要作用是事件传递、响应者链 //当在一个view上添加一个屏蔽罩,但又不影响对下面view的操作,也就是可以透过屏蔽罩对下面的view进行操作 -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; if (hitView == self) { return nil; } else { return hitView; } } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return NO; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // UITouch *touch=[touches anyObject]; NSLog(@"bbbbb"); return; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
二、
// // ViewController.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "ViewController.h" #import "MyView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *view1=[[UIView alloc]init]; view1.frame=CGRectMake(100, 100, 200, 200); //允许用户交互 view1.userInteractionEnabled=YES; //tag值 view1.tag=10001; //layer view1.layer.backgroundColor=[UIColor redColor].CGColor; //UIViewGeometry //bounds center NSLog(@"bounds=%@\ncenter=%@",NSStringFromCGRect(view1.bounds),NSStringFromCGPoint( view1.center)); //设置view是否响应多点触摸,默认为NO view1.multipleTouchEnabled=YES; ////设置touch是否排它,默认为NO view1.exclusiveTouch=YES; //是否隐藏 view1.hidden=NO; //将像素point从view中转换到当前视图中,返回在当前视图中的像素值 CGPoint point=[view1 convertPoint:CGPointMake(200, 200) fromView:self.view]; NSLog(@"convertPoint fromView=%@",NSStringFromCGPoint(point)); // 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 CGPoint point1=[view1 convertPoint:CGPointMake(200, 200) toView:self.view]; NSLog(@"convertPoint toView=%@",NSStringFromCGPoint(point1)); // 将rect从view中转换到当前视图中,返回在当前视图中的rect CGRect rect=CGRectMake(50, 50, 200, 200); CGRect rect1= [view1 convertRect:rect fromView:self.view]; //将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect CGRect rect2=[view1 convertRect:rect toView:self.view]; NSLog(@"convertRect fromView=%@",NSStringFromCGRect(rect1)); NSLog(@"convertRect toView=%@",NSStringFromCGRect(rect2)); //子视图自适应 如果设置成NO,那么subView的autoresizingMask属性失效。 view1.autoresizesSubviews=YES; // typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { // UIViewAutoresizingNone = 0, 不调整 // UIViewAutoresizingFlexibleLeftMargin = 1 << 0,自动调整与superView左边的距离,也就是说,与superView右边的距离不变。 // UIViewAutoresizingFlexibleWidth = 1 << 1,自动调整与superView的右边距离,也就是说,与superView左边的距离不变。 // UIViewAutoresizingFlexibleRightMargin = 1 << 2, // UIViewAutoresizingFlexibleTopMargin = 1 << 3, // UIViewAutoresizingFlexibleHeight = 1 << 4, // UIViewAutoresizingFlexibleBottomMargin = 1 << 5 // }; //多个用| view1.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin; //调整view的尺寸去适应其内容 [view1 sizeToFit]; //传递view的尺寸,返回建议的子view尺寸 CGSize size=[view1 sizeThatFits:CGSizeMake(100, 150)]; NSLog(@"sizeThatFits=%@",NSStringFromCGSize(size)); //UIViewHierarchy //UIViewRendering //子视图超出边境裁剪 view1.clipsToBounds=YES; //透明度 view1.alpha=0.5; // opaque属性提示绘制系统如何处理view。如果opaque设置为YES,绘图系统会将 // view看为完全不透明,这样绘图系统就可以优化一些绘制操作以提升性能。如果设置 // 为NO,那么绘图系统结合其它内容来处理view。默认情况下,这个属性是YES。) // 如果屏幕是静止的,那么这个opaque属性的设置与否不是一个大问题。但是,如果 // view是嵌入到scroll view中的,或者是复杂动画的一部分,不将设置这个属性的话 // 肯定会影响程序的性能! // alpha支持animation, hidden和opaque不支持 // hidden开销小,alpha=0透明开销大,如果效果一样,用hidden好一点. // hideen的时候view是不接收事件的,但alpha为0接收 // 当把View设置为透明的背景时,一般把opaque设置为NO,可以减少开销,对内存也好. // 告诉系统渲染器view是否不透明,设置YES可以加快渲染,默认为YES,如果设置了alpha值,应该设置为NO view1.opaque=YES; // 是否清除缓冲区中不可见内容,默认为YES,如果在一个滚动操作频繁的视图中,应该设为NO,以避免重新渲染,提升性能 view1.clearsContextBeforeDrawing=YES; //view自适应变化的方式 填充方式 左对齐、右对齐、拉伸填充等 view1.contentMode=UIViewContentModeScaleToFill; MyView *myView=[[MyView alloc]initWithFrame:view1.frame]; myView.backgroundColor=[UIColor blackColor]; [self.view addSubview:view1]; //将自定义的MyView 放在View上面时,点击触发响应者链 [self.view addSubview:myView]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // UITouch *touch=[touches anyObject]; NSLog(@"aaaaa"); return; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
三、点击视图
2015-05-17 18:20:17.356 UIView[676:60b] bounds={{0, 0}, {200, 200}} center={200, 200} 2015-05-17 18:20:17.358 UIView[676:60b] convertPoint fromView={100, 100} 2015-05-17 18:20:17.358 UIView[676:60b] convertPoint toView={300, 300} 2015-05-17 18:20:17.359 UIView[676:60b] convertRect fromView={{-50, -50}, {200, 200}} 2015-05-17 18:20:17.360 UIView[676:60b] convertRect toView={{150, 150}, {200, 200}} 2015-05-17 18:20:17.360 UIView[676:60b] sizeThatFits={200, 200} 2015-05-17 18:20:20.049 UIView[676:60b] aaaaa
四、上面输出可以看到输出"aaaaa"而不是"bbbb", 因为在MyView中重写了-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event屏蔽了最上面按钮的响应,而它下面的可以响应