沃梦达 / IT编程 / 移动开发 / 正文

iOS开发中Subview的事件响应以及获取subview的方法

这篇文章主要介绍了iOS开发中Subview的事件响应以及获取subview的方法,代码基于传统的Objective-C,需要的朋友可以参考下

Subview的事件响应
在view的层级里面,默认情况下subview是可以显示到其父view的frame区域以外的,通过设置clipToBounds属性为YES,可以限制subview的显示区域。但是touch在各个UIView中传递的时候,区域时限制在view的frame内,此处包含两个信息:1、在当前view的frame以外所做的操作是不会传递到该view中的,这一点很容易理解。2、如果touch事件是发生在当前view的frame以外,该view所有的subview将也不会再收到该消息。这一点通常容易被我们忽略,很多奇怪的问题就是这个引起的。
 
  下面请看一个小例子,定制view的代码如下:
 

代码如下:

SvTestClipSubviewEvent.h


//

//  SvTestClipSubviewEvent.h

//  SvUIViewSample

//

//  Created by maple on 3/19/12.

//  Copyright (c) 2012 smileEvday. All rights reserved.

//

//  默认的情况下,subView可以超出父view的frame,即可以显示到父View的外边

//  但是消息的接受返回却是由于父View的大小限制,即出了父View的subView将不能收到消息

//  在程序中一定要注意当前程序view的最底层是充满整个window的可用区域的,

//  否则将会导致某些区域明明有按钮但是却点不中的问题

#import <UIKit/UIKit.h>

@interface SvTestClipSubviewEvent : UIView

@end

代码如下:

//
//  SvTestClipSubviewEvent.m
//  SvUIViewSample
//
//  Created by maple on 3/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import "SvTestClipSubviewEvent.h"

@interface SvTestClipSubviewEvent()

- (void)btnAction:(UIButton*)btn;

@end

@implementation SvTestClipSubviewEvent

- (id)initWithFrame:(CGRect)frame
{   
    self = [super initWithFrame:frame];   
    if (self) {       
        // Initialization code       
        self.backgroundColor = [UIColor redColor];               
        UIButton *testOutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];                    testOutBtn.frame = CGRectMake(-80, -50, 70, 30);      
        [testOutBtn addTarget:self action:@selecto (btnAction: forControlEvents:UIControlEventTouchUpInside];       
        [testOutBtn setTitle:@"I'm out" forState:UIControlStateNormal];       
        [self addSubview:testOutBtn];                
        UIButton *testInBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];                    testInBtn.frame = CGRectMake(20, 30, 70, 30);       
        [testInBtn setTitle:@"I'm in" forState:UIControlStateNormal];       
        [testInBtn addTarget:self action:@selector(btnAction: forControlEvents:UIControlEventTouchUpInside];       
        [self addSubview:testInBtn];   
    }   
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{   
    // Drawing code
}
*/

- (void)btnAction:(UIButton*)sender
{   
    NSLog(@"HI, you tap button %@", [sender titleForState:UIControlStateNormal]);
}
@end


  在程序的ViewController中添加如下测试代码:
 

代码如下:

SvTestClipSubviewEvent *testClipSubView = [[SvTestClipSubviewEvent alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
[self.view addSubview:testClipSubView];
[testClipSubView release];

  运行可以看到如下界面:

代码如下:


//
//  SvTestViewTag.h
//  SvUIViewSample
//
//  Created by maple on 3/18/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//
//  view根据Tag获取subView的时候执行的是深度优先遍历的算法
//  返回第一个Tag和请求tag相等的子View
//  从subViews中查找,最下层的优先找到

#import <UIKit/UIKit.h>

@interface SvTestViewWithTag : UIView

@end

 
  其次我还在subview1中添加了tag均为13的subview13,同时向view中添加了tag也为13的subview2,运行程序点击“Show Tag 13”按钮,屏幕上将会显示“SubView13”,而非“SubView2”。这可以验证viewWithTag在搜索的时候遵循深度优先遍历的原则,即会首先查找最下层的view并递归查询其subview。
 
  综上两点我们可以看出来viewWithTag获取subview的基本原则,即遵循深度优先,下层优先两个原则。

本文标题为:iOS开发中Subview的事件响应以及获取subview的方法