下面这些都是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二的更多相关文章

  1. UIKit 框架之UIView一

    - (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View’s Visual Appearance //配置视觉展示 @pro ...

  2. UIKit 框架之UITableView二

    // // ViewController.m // UITableView // // Created by City--Online on 15/5/21. // Copyright (c) 201 ...

  3. UIKit框架使用总结--看看你掌握了多少

    一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...

  4. UIKit框架

    在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...

  5. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  6. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  7. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  8. 79、iOS 的Cocoa框架、Foundation框架以及UIKit框架

    Cocoa框架是iOS应用程序的基础 1. Cocoa是什么? Cocoa是 OS X和ios 操作系统的程序的运行环境. 是什么因素使一个程序成为Cocoa程序呢?不是编程语言,因为在Cocoa开发 ...

  9. UIKit 框架之Bar、Controller

    UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarIt ...

随机推荐

  1. 锁——Java同步的基本思想

    翻译人员: 铁锚 翻译时间: 2013年11月13日 原文链接:  Monitors – The Basic Idea of Java synchronization 如果你上过操作系统课程,你就知道 ...

  2. shell 命令 --ps aux | grep

    ps aux | grep  要查询的进程名 查询当前进程,如 ps aux | grep python 确认过需要查询的进程,就可以进行 kill -9 进程号等操作了.

  3. B - Avoiding a disaster

    Description Percy likes to be punctual. So much so that he always keeps three watches with him, so t ...

  4. poj1573模拟

    Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...

  5. ios 百度地图,火星坐标,地球坐标互转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. STF环境搭建(ubuntu)

    一,环境搭建 1. linux 一些基础的工具要有: sudo apt-get update sudo apt-get install git sudo apt-get install lib32st ...

  7. VS动态修改App.config中遇到的坑(宿主进程问题)

    昨天遇到了很奇怪的一个bug,具体描述如下: 这个系统是c/s架构的针对多个工厂做的资材管理系统,由于有很多个工厂,每个工厂都有自己的服务器.所以需要动态的改变连接字符串去链接不同的服务器. 由于这个 ...

  8. 《jQuery基础教程(第四版)》学习笔记

    本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...

  9. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. K8s之spinnaker

    一.spinnaker概述 1.spinnaker是netflix开源的一款云发布CI/CD,其前身是Asgard,spinnaker支持各种部署目标,包括OpenStack.DC/OS.Kubern ...