iOS UIView方法使用详情UIView : UIResponder

  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 , readonly UIWindow      *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方式。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值