iOS UIView方法使用详情UIView : UIResponder

本文详细介绍了iOS开发中UIView的创建过程、关键属性与方法,包括如何使用UIView及其子类实现界面布局,探讨了frame与bound的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  view(视图):代表屏幕上的一个矩形区域

  iOS中用UIView来表示视图,不同的控件代表不同种类的view

  如何创建view?

 //添加一小块视图到屏幕上

   //1.申请空间,并初始化大小

   UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    //2. 设置view相关属性

    view.backgroundColor = [UIColor whiteColor];

    //3. 添加viewwindow,_window发送消息;

    //将一个view添加到_window上时,view的引用计数增加1

    [_windowaddSubview:view];

    //4.释放

    [view release];


    看一下view的相关属性

UIView的父类是UIResponder 

1.@property(nonatomic)CGRect            frame;  位置和尺寸(以父控制器的左上角为原点)

2.@property(nonatomic)CGRect            bounds;  位置和尺寸 (以自己的左上角为原点)      

3.@property(nonatomic)CGPoint           center;      中心点(以父视图控件的左上角为原点)

4.@property(nonatomic)CGAffineTransform        transform;    形变熟性 (缩放,旋转)

5.@property(nonatomic)CGFloat           alpha;         // animatable. default is 1.0 透明度(0~1)

6.@property(nonatomic,getter=isOpaque) BOOL              opaque;     不透明度(0~1)              

   // 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   

7.@property(nonatomic,readonlyUIView       *superview;            父视图控件

8.@property(nonatomic,readonly,copyNSArray *subviews;            子视图控件

9.@property(nonatomic,getter=isHidden) BOOL              hidden;                     // default is NO. doesn't check  superviews 设置是否要隐藏

10.@property(nonatomic,copy)            UIColor          *backgroundColor UI_APPEARANCE_SELECTOR// default is nil.  设置背景颜色

11.@property(nonatomic)                 UIViewContentMode contentMode;                // default is UIViewContentModeScaleToFill   内容显示的模式 拉伸自适应

12.@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  能否跟用户进行交互(YES能交互)

13.@property(nonatomic)                                 NSInteger tag;                //  标识(父控制器可以根据这个标识找到对应的子控件,同一个父控件中的子控件不要一样)default is 0

14.@property(nonatomic,readonlyUIWindow     *window; //自身的window窗口

15.@property(nonatomic,retain)UIColor *tintColorNS_AVAILABLE_IOS(7_0);  // tintcolor 就是控件的颜色,因为很多控件没有backgroundcolor 这个属性,注意很多控件即使你使用的tintcolor 但是不一定生效的,和backgroudcolor是一样的 他只适用与部分控件

16.@property(nonatomic,copy)NSArray *gestureRecognizersNS_AVAILABLE_IOS(3_2);  手势识别


手势相关详情及用法请看链接:



     UIView提供了大量管理视图的方法 ,常见的有以下方法
1. - (void)addSubview:(UIView *)view;
添加一个视图到一个视图里面的方法

2. - (void)removeFromSuperview;

把视图移除(自己)

3. - (UIView *)viewWithTag:(NSInteger)tag;    // recursive search. includes self
根据tag值,检索视图

4.- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

插入视图,并指定索引

5.- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

交换两个位置索引的视图

6.- (void)bringSubviewToFront:(UIView *)view;

将一个视图移到前面

7.- (void)sendSubviewToBack:(UIView *)view;

将一个视图推到背后

8.- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

插入视图在某个视图之上

9.- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

插入视图在某个视图之下


  给UIView设置标记和检索视图
 myview.tag = 1001;
 [self.view viewWithTag:1001];
 (UIView *)[self.view.window viewWithTag:1001];

  几何结构
/* Points. */

struct CGPoint {
  CGFloat x;
  CGFloat y;
};
typedef struct CGPoint CGPoint;

/* Sizes. */

struct CGSize {
  CGFloat width;
  CGFloat height;
};
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">struct</span> CGRect {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #3495af">CGPoint</span> origin; </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #3495af">CGSize</span> size;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">};</p>
typedef struct CGSize CGSize;
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">创建位置信息 CGPoint的方法</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(4, 51, 255);">CG_INLINE<span style="color: #000000"> </span><span style="color: #3495af">CGPoint</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">CGPointMake(<span style="color: #3495af">CGFloat</span> x, <span style="color: #3495af">CGFloat</span> y)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #3495af">CGPoint</span> p; p.<span style="color: #3495af">x</span> = x; p.<span style="color: #3495af">y</span> = y; <span style="color: #0433ff">return</span> p;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">创建大小 CGSize<span style="font-family: Menlo; font-size: 18px; white-space: pre; background-color: rgb(240, 240, 240);">的方法</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(4, 51, 255);">CG_INLINE<span style="color: #000000"> </span><span style="color: #3495af">CGSize</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">CGSizeMake(<span style="color: #3495af">CGFloat</span> width, <span style="color: #3495af">CGFloat</span> height)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #3495af">CGSize</span> size; size.<span style="color: #3495af">width</span> = width; size.<span style="color: #3495af">height</span> = height; <span style="color: #0433ff">return</span> size;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">创建CGRect<span style="font-family: Menlo; font-size: 18px; white-space: pre; background-color: rgb(240, 240, 240);">的方法</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(4, 51, 255);">CG_INLINE<span style="color: #000000"> </span><span style="color: #3495af">CGRect</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">CGRectMake(<span style="color: #3495af">CGFloat</span> x, <span style="color: #3495af">CGFloat</span> y, <span style="color: #3495af">CGFloat</span> width, <span style="color: #3495af">CGFloat</span> height)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #3495af">CGRect</span> rect;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  rect.<span style="color: #3495af">origin</span>.<span style="color: #3495af">x</span> = x; rect.<span style="color: #3495af">origin</span>.<span style="color: #3495af">y</span> = y;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  rect.<span style="color: #3495af">size</span>.<span style="color: #3495af">width</span> = width; rect.<span style="color: #3495af">size</span>.<span style="color: #3495af">height</span> = height;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">  <span style="color: #0433ff">return</span> rect;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> 判断两个矩形是否相交</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">bool</span> CGRectIntersectsRect(<span style="color: #3495af">CGRect</span> rect1, <span style="color: #3495af">CGRect</span> rect2)</p>
<div> 初始为 0的</div><div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">const</span> <span style="color: #3495af">CGPoint</span> CGPointZero</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">const</span> <span style="color: #3495af">CGSize</span> CGSizeZero</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">const</span> <span style="color: #3495af">CGRect</span> CGRectZero</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> 对一个CGRect进行修改 以这个的中心来修改 正数表示更小(缩小),负数表示更大(放大)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #3495af">CGRect</span> CGRectInset(<span style="color: #3495af">CGRect</span> rect, <span style="color: #3495af">CGFloat</span> dx, <span style="color: #3495af">CGFloat</span> dy)</p>
</div>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="color:#3333ff;">转换类型</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">将string转换成CGPoint 如@“{3.0,2.5}” {x,y}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">NSString</span> *NSStringFromCGPoint(<span style="color: #3495af">CGPoint</span> point);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="font-family: Menlo; font-size: 18px; white-space: pre; background-color: rgb(240, 240, 240);">将String转换成CGSize 如@“{3.0,2.5}” {w,h}</span>
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">NSString</span> *NSStringFromCGSize(<span style="color: #3495af">CGSize</span> size);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="font-family: Menlo; font-size: 18px; white-space: pre; background-color: rgb(240, 240, 240);">将String转换成CGRect 如@“{{3,2},{4,5}}” {{x,y},{w,h}}</span>
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">NSString</span> *NSStringFromCGRect(<span style="color: #3495af">CGRect</span> rect);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">同理</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">CGPoint</span> CGPointFromString(<span style="color: #3495af">NSString</span> *string);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">CGSize</span> CGSizeFromString(<span style="color: #3495af">NSString</span> *string);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="color: #0433ff">UIKIT_EXTERN</span> <span style="color: #3495af">CGRect</span> CGRectFromString(<span style="color: #3495af">NSString</span> *string);</p>
<span style="color:#3333ff;">直接设置视图的中心</span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">testview.<span style="color: #3495af">center</span> = <span style="color: #3495af">CGPointMake</span>(100, 200);</p>



 Frame和Bound的区别

    视图的大小和位置用两种方式表示。一种方式是Frame(框架),即以其父视图为起点,得出它自己的信息。另一种方式是Bound(界限),即以它自己为起点,得到其位置。

      其实,系统内部存放的是图的中心点位置和大小信息。Frame方式的信息是按照中心点位置计算出来的。当我们创建一个视图的时,我们往往采用Frame方式。当我们旋转一个视图或者处理视图事件时,我们大多采用Bound方式。







1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值