• UIView


    UIView

    大部分可视化操作是由视图对象UIView类打实例完成的。

    一个视图对象定义啦屏幕上的一个矩形区域,同时处理该区域打绘制和触屏事件。

    一个视图也可以作为其他视图的父视图,同时决定着这些子视图打位置和大小。

    UIView类做了大量打工作去管理内部视图关系,但是也可以定制默认打行为。

    UIView的主要行为分为如下三个方面:

    1)         绘制和动画。我们可以使用UIKit,Core Graphics,OpenGL ES等技术绘制视图,通过改变视图打属性实现一些动画效果。例如,alpha可以改变透明度,transform可以进行缩放,旋转和移动。

    2)         布局和子视图的管理。一个视图可以包含若干子视图,可以动态添加和删除子视图,可以定义子视图相对于父视图的位置。

    3)         事件处理。UIView继承UIResponder,可以实现UIResponder中的事件方法可以调用addGestureRecognizer: 方法为视图添加手势处理。

    UIView的创建

    大部分应用程序都是使用系统定义好的视图,例如,按钮,输入框等。但有时候我们也需要自己定义视图来实现一些高级功能,例如,在游戏开发中,自定义一个UIView。

    l   使用Xcode创建一个空项目

    l   创建一个类继承UIView,覆盖initWithFrame 和 drawRect方法

    l   

    l  - (id)initWithFrame:(CGRect)frame

    l  {

    l      self = [super initWithFrame:frame];

    l      if (self) {

    l          // Initialization code

    l      }

    l      return self;

    l  }

    l   

     

    l  - (void)drawRect:(CGRect)rect{

    l      CGContextRef context = UIGraphicsGetCurrentContext();

    l      CGContextSetFillColor(context, [UIColor redColor].CGColor);

    l      CGRect r = CGRectMake(20, 20, 200, 200);

    l      CGContextAddRect(context, r);

    l      CGContextDrawPath(context, kCGPathFill);

    l  }

    l    

    l   在代理的didFinishLaunchingWithOptions方法中,实例化MyView并添加到UIWindow中

    l  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    l  {

    l      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    l      

    l      

    l      CGRect frame = CGRectMake(0, 0, 300, 300);

    l      MyView *myView = [[MyView alloc] initWithFrame:frame];

    l      

    l      

    l      // Override point for customization after application launch.

    l      self.window.backgroundColor = [UIColor whiteColor];

    l      [self.window addSubview:myView];

    l      [self.window makeKeyAndVisible];

    l      return YES;

    l   }

    l    

    UIView的核心属性

    视图对象通过frame, bounds, center 属性声明来跟踪自己的大小和位置。

    l   frame属性包含一个矩形,即边框矩形,用于指定视图相对于其父视图坐标系统的位置和大小。

    l   bounds 属性也包含一个矩形,即边界矩形,负责定义视图相对于本地坐标系统的位置和大小。虽然边界矩形的原点通常被设置为(0,0),但这并不是必需的。

    l   center属性包含边框矩形的中心点。

    在viewDidLoad方法中创建两个按钮:

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        self.myBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        CGRect frame = CGRectMake(20, 20, 100, 30);

        self.myBtn.frame=frame;

        [self.myBtn setTitle:@"Test" forState:UIControlStateNormal];

        [self.view addSubview:self.myBtn];

       

        self.changeBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

        CGRect frame2=CGRectMake(20, 100, 100, 30);

        self.changeBtn.frame=frame2;

        [self.changeBtn setTitle:@"Change" forState:UIControlStateNormal];

        [self.view addSubview:self.changeBtn];

        [self.changeBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];

       

       

       

    }

     

    - (IBAction)change:(id)sender{

        CGPoint center = CGPointMake(160, 240);

        self.myBtn.center = center;

        [self.myBtn setNeedsLayout];

        CGRect bounds = CGRectMake(20, 20, 100, 60);

        self.myBtn.bounds=bounds;

    }

  • 相关阅读:
    Win10系统接口自动化测试持续集成
    postman结合newman生成测试报告
    关于sass(scss)、less、postcss、stylus的简介与区别
    python 对pdf的response进行操作(如:去掉首页)
    存数据的格式
    json文本格式化
    python处理时间
    @SuppressWarning
    【源码笔记】构建Spring源码环境
    手写一个模拟的ReentrantLock
  • 原文地址:https://www.cnblogs.com/haiwei_sun/p/3517648.html
Copyright © 2020-2023  润新知