For example, the writing method of HitTest touch event in IOS application development _ios

Source: Internet
Author: User

Hittest:withevet Call procedure

For example, if this is the current view a, there is also a VIEWB

If you do not override the HitTest method, the system defaults to calling the Viewa Hitest method before calling the Viewb Htest method.

The process of calling the system is exactly the same as the following rewrite Hitest method.

Copy Code code as follows:

-(uiview*) HitTest: (cgpoint) point withevent: (Uievent *) event
{
if ([self pointinside:point withevent:event]) {
}
else {
return nil;
}
For (UIView *subview in self.subviews) {
if ([Subview hittest:point Withevent:event]!=nil) {
return subview;
}
}

return self;
}

Once again, if you do not rewrite the Hitest method, then each uivieew default Hitest method is the above process.

There's nothing to say.

But for some special process, it's not going to work.

So rewrite the HitTest method, is usually to penetrate the upper UIView, so that touch events can reach the following UIView,

Like view A and view B,

View B completely blocks view a, but I want to click on VIEWB, view a can respond to the clicked event. You can use the following methods:

Copy Code code as follows:

-(uiview*) HitTest: (cgpoint) point withevent: (Uievent *) event
{
if ([self pointinside:point withevent:event]) {
NSLog (@ "In view A");
return self;
}
else {
return nil;
}

}

Depth
let's go a bit deeper, now there's an example requirement interface as follows,

Window

-viewa

-buttona

-viewb

-buttonb

Hierarchy: Viewb completely covers the buttona,buttonb on VIEWB, now needs to be implemented:
(1) Buttona and BUTTONB can respond to messages (2) Viewa can also receive VIEWB received touches message (3) do not let VIEWB (BUTTONB) receive the message.

(First parse, by default, click on the Buttonb area, the iOS message processing process.)

-viewa

-buttona

-viewb

-buttonb

When you click on the Buttonb area, the process: Call HitTest from Viewa start

The Pointinside values are:

Viewa:no;

Viewb:yes;

Buttonb:yes;

The Subviews:no of BUTTONB;

So Buttonb's subviews hittest all return nil, so the object returned is Buttonb himself. Next, start processing the touches series method, which is the method that calls the BUTTONB binding. The message stops after processing, and the whole process ends. )

Analysis:

Implemented in a variety of ways, where two requirements are disassembled to implement, because the implementation of 2 can meet 1.

The implementation of demand 1, VIEWB cover the Buttona, so by default Buttona not receive messages, but in the message mechanism to find the message response is from the parent view, so we can in the Viewa HitTest method to make judgments, if touch Point is on the Buttona, the Buttona is returned as a message processing object.

The code is as follows:

Copy Code code as follows:

#pragma mark-hittest
-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event
{
When the touch point is on the _BTN, the HitTest returns _BTN
Cgpoint Btnpointina = [_btn convertpoint:point fromview:self];
if ([_btn Pointinside:btnpointina withevent:event]) {
return _btn;
}

Otherwise, returns the default processing
return [Super Hittest:point withevent:event];

}

So, when the touch point is on the Buttona, the touch message is intercepted on the Viewa, VIEWB will not be received. Then Buttona receives the touch message and triggers the OnClick method.

The implementation of demand 2, which says the response chain, VIEWB as long as override the touches series of methods, and then after their own processing, pass the message to the next responder (that is, the parent view is Viewa).

The code is as follows: in the VIEWB code

Copy Code code as follows:

#pragma mark-touches
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "B-touchesbeagan ...");

Pass the event on to the parent view or include his Viewcontroller
[Self.nextresponder touchesbegan:touches withevent:event];
}

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "b-touchescancelled ...");
Pass the event on to the parent view or include his Viewcontroller
[Self.nextresponder touchesbegan:touches withevent:event];
}

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "b-touchesended ...");
Pass the event on to the parent view or include his Viewcontroller
[Self.nextresponder touchesbegan:touches withevent:event];
}

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "b-touchesmoved ...");
Pass the event on to the parent view or include his Viewcontroller
[Self.nextresponder touchesbegan:touches withevent:event];

}

Then, on the Viewa, you can receive the touches message, written on the Viewa:
Copy Code code as follows:

#pragma mark-touches
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "A-touchesbeagan ...");
}

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "a-touchescancelled ...");
}

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "a-touchesended ...");
}

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "a-touchesmoved ...");

}

This enables the message to be passed to the parent view.

Do not let VIEWB receive the message, you can set Viewb.userinteractionenable=no, in addition to this can also override off Viewb ponitinside, the principle of reference above.

Write on the VIEWB:

Copy Code code as follows:

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event
{
This view does not respond to user events
return NO;

}

Related Article

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: info-contact@alibabacloud.com 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.