Subview Event Response Range

Source: Internet
Author: User

Subview Event Response

In the view level, the subview can be displayed outside the frame area of its parent view by default. By setting the cliptobounds attribute to yes, you can limit the display area of the subview. However, when touch is passed in each uiview, the area time limit is defined in the View frame, which contains two information: 1. operations performed outside the frame of the current view are not passed to the view, which is easy to understand. 2. If the touch event occurs outside the frame of the current view, all subviews of the view will no longer receive the message. This is often overlooked. Many strange problems are caused by this.

The following is a small example.CodeAs follows:

Svtestclipsubviewevent. h

// Svtestclipsubviewevent. h
// Svuiviewsample
// Created by MAPLE on 3/19/12.
// Copyright (c) 2012 smileevday. All rights reserved.
// By default, subview can go beyond the frame of the parent view, that is, it can be displayed outside the parent view.
// However, the size of the parent view is limited. That is, the subview of the parent view cannot receive the message.
// InProgramYou must note that the bottom layer of the current program view is filled with the available area of the entire window,
// Otherwise, a button is clearly displayed in some regions, but the button is not displayed.

# Import <Uikit/uikit. h>

@ Interface Svtestclipsubviewevent: uiview

@ End
Svtestclipsubviewevent. m

// 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: @ selector (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

Add the following test code to viewcontroller:

Svtestclipsubviewevent * testclipsubview = [[svtestclipsubviewevent alloc] initwithframe: cgrectmake (100,100,150,150)];
[Self. View addsubview: testclipsubview];
[Testclipsubview release];

The following page is displayed:

In this example, we set the background color of the custom view to red, create a uibutton "I'm out" located outside the custom view and another uibutton "I'm in" located in the Custom view ". When you run the program, we can find that the interface does not change at all when you click the "I'm out" button. Adding a breakpoint to the btnaction: function will not go in, this indicates that the button does not receive the message at all, and the "I'm in" button runs normally. After you click it, the console will output the message "... uiviewsample [664: f803] Hi, you tap button I'm in ". These indicate that the range of events received by the subview is limited by the frame of its superview, and it is impossible to receive touch events other than the frame of the superview.

if our program sometimes looks at some buttons but fails to normally receive touch messages, it may be that its display position is out of the frame range of superview. Therefore, the rootview of the program must always be filled with the entire window. Otherwise, some parts of the program may not be able to communicate with the corresponding user messages normally. We recommend that you set the autoresizingmask value to uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight, in this way, when the superview size changes, it is automatically scaled to the same size as the superview, to ensure that every moment is filled with its superview, this avoids issues such as abnormal user touch events caused by the display location going out of the bounds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.