UIKit 框架之UIView二
下面这些都是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屏蔽了最上面按钮的响应,而它下面的可以响应
UIKit 框架之UIView二的更多相关文章
- UIKit 框架之UIView一
- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View’s Visual Appearance //配置视觉展示 @pro ...
- UIKit 框架之UITableView二
// // ViewController.m // UITableView // // Created by City--Online on 15/5/21. // Copyright (c) 201 ...
- UIKit框架使用总结--看看你掌握了多少
一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...
- UIKit框架
在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...
- iOS学习32之UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...
- iOS开发UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- 79、iOS 的Cocoa框架、Foundation框架以及UIKit框架
Cocoa框架是iOS应用程序的基础 1. Cocoa是什么? Cocoa是 OS X和ios 操作系统的程序的运行环境. 是什么因素使一个程序成为Cocoa程序呢?不是编程语言,因为在Cocoa开发 ...
- UIKit 框架之Bar、Controller
UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarIt ...
随机推荐
- mysql 唯一索引UNIQUE使用方法详解
创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复.唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值.如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的 ...
- MISL Learning
https://www.codeguru.com/csharp/.net/net_general/il/article.php/c4635/MSIL-Tutorial.htm http://etuto ...
- 自定义延时关闭弹窗,替代MesssageBox
1,新建一个窗体MessageForm,在里面加一个label控件和timer 2,代码如下: public partial class MessageForm : Form { int t; str ...
- Nanui 教程
彩票自动投注软件定制-联灬系-\加/Q;2943075966 黑/科/技问/世.详情直接添加咨询.信/誉/文本 最近接到一个项目 是关于构建一套 电脑端会员管理系统 但考虑到个人比较喜欢写Web ...
- Android------TabLayout的使用
https://www.jianshu.com/p/2b2bb6be83a8 主要放在 -------> Design库中的TabLayout的使用. margin和padding的区别 外边距 ...
- 阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return false的区别
1.event.stopPropagation()方法 这是阻止事件的冒泡方法,不让事件向documen上蔓延,但是默认事件任然会执行,当你掉用这个方法的时候,如果点击一个连接,这个连接仍然会被打开, ...
- [POI2015]LOG(树状数组)
今天考试考了这题,所以来贡献\([POI2015]LOG\)的第一篇题解.代码略丑,调了快三个小时才调出来\(AC\)代码. 对于这种小清新数据结构题,所以我觉得树状数组才是这道题的正确打开方式. 首 ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- 玄武短信接口和移动MAS短信接口的API封装
直接上代码,关键点: 133行的敏感词过滤 176行的6位扩展码写入 using System; using System.Collections.Generic; using System.Linq ...
- cStringIO 实现指定大小的字符串缓存
StringIO经常被用来作为字符串的缓存,以下实现无论写入多少字符串,总能返回一个指定大小的缓存 from cStringIO import StringIO class CustomStringI ...