iOS(学习4) UIWindow 和 UIView

iOS应用中使用UIWindow、UIView来实现内容显示。

UIWindow:

UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示、分发事件给View。UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。UIWindow初始化在appDeleDgate里面的 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions方法。

@implementation AppDelegate  


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

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];  
    //self.window.backgroundColor = [UIColor grayColor];  
    //使用对象的主窗口显示到屏幕的最前端  
    [self.window makeKeyAndVisible];  

    // ViewController 显示最前端  
    self.window.rootViewController = [[ViewController alloc] init];  

    //切换根视图控制器,让OneViewController作为根视图控制器  
    //self.window.rootViewController = [[OneViewController alloc] init];  

    //带 .xib 文件的初始化  
    //self.window.rootViewController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:[NSBundle mainBundle]];  

    return YES;  
}  

UIVIew

UIView类继承自UIResponder,负责在屏幕上 定义一个矩形区域,视图用于展示界面及响应用户界面交互。每个视图对象都要负责渲染视图区域的内容,并响应该区域中发生的操作事件。
UIView为所有控件父类

这里做一个简单动画效果

#import "ViewController.h"  

@interface ViewController ()  
{  
    BOOL flag;  

}  
@end  



@implementation ViewController  

- (void)viewDidLoad {  
    [super viewDidLoad];  

    // 当前视图颜色  
    self.view.backgroundColor = [UIColor grayColor];  

    UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(40, 20, 60, 60)];  
    //每一个View都有一个tag的属性  
    oneView.tag = 1000;  
    //设置圆角  
    oneView.layer.cornerRadius = 5;  
    //告诉layer将位于它之下的layer都遮盖住  
    oneView.layer.masksToBounds = YES;  
    //边框 颜色及 粗细  
    oneView.layer.borderColor = [UIColor greenColor].CGColor;  
    oneView.layer.borderWidth = 1.0;  


    flag = YES;  
    oneView.backgroundColor = flag?[UIColor redColor]:[UIColor yellowColor];  
    //每个视图都有添加子视图的方法:addSubview  
    [self.view addSubview:oneView];  

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
    button.frame = CGRectMake(60, 150, 40, 30);  
    [button setTitle:@"点击" forState:UIControlStateNormal];  
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];  
    [self.view addSubview:button];  

}  

- (void)buttonAction:(UIButton *)sender {  

    UIView *view = [self.view viewWithTag:1000];  
    flag = !flag;  
     view.backgroundColor = flag?[UIColor redColor]:[UIColor yellowColor];  
    if (!flag) {  
        [UIView animateWithDuration:2.0 animations:^{  
            view.frame = CGRectMake(200, 20, 160, 160);  
        }];  
    } else {  
        [UIView animateWithDuration:2.0 animations:^{  
            view.frame = CGRectMake(40, 20, 60, 60);  
        }];  
    }  
}  

- (void)didReceiveMemoryWarning {  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  

@end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,触摸事件处理是非常重要的,因为用户与应用程序的交互主要是通过触摸屏幕来完成的。iOS提供了一整套触摸事件处理机制,包括手势识别、触摸事件响应等。下面我们来介绍一下iOS中常用的触摸事件处理方法。 1. 触摸事件响应方法 在iOS中,触摸事件主要由以下4个方法来响应: - touchesBegan:withEvent: //手指开始接触屏幕 - touchesMoved:withEvent: //手指在屏幕上移动 - touchesEnded:withEvent: //手指离开屏幕 - touchesCancelled:withEvent: //触摸事件取消 这些方法都在UIView类中定义,所以如果你的应用程序需要响应触摸事件,你需要重写UIView的这些方法。 2. 手势识别器 在iOS中,手势识别器是一种非常方便的触摸事件处理方式。手势识别器可以识别出一些常用的手势,比如轻击、长按、拖动、捏合等等,并且可以通过回调方法来处理这些手势。 手势识别器有很多种,比如UITapGestureRecognizer、UILongPressGestureRecognizer、UIPanGestureRecognizer、UIPinchGestureRecognizer等等。使用手势识别器可以让你的应用程序更加灵活,同时也可以提高用户体验。 3. 响应者链 在iOS中,事件的传递是通过响应者链来完成的。当用户发生了触摸事件时,事件会从最上层的UIWindow开始传递,直到找到第一个响应该事件的对象。如果没有任何对象响应该事件,事件就会被丢弃。 因此,在处理触摸事件时,你需要注意响应者链的结构,确保事件能够正确地传递到你想要处理事件的对象。 总之,在iOS开发中,触摸事件处理是一个非常重要的部分,需要我们认真对待。通过合理地使用触摸事件处理方法和手势识别器,我们可以为用户提供更加灵活、友好的应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值