IOS Custom button Effective Click Range

Source: Internet
Author: User

So how to customize the active area of the button click is actually very simple, just rewrite a function:

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event

When a touch event occurs, the system detects whether the touch point is in it from the window to the parent control to the child control, and if so, returns Yes, and finally returns the child control of Yes as the control that responds to the event.

We just have to rewrite this method, in which we can judge if we click on the area we want, yes, then return yes, otherwise return no, thus implementing a custom click of the effective region. Note that this side does not change the shape of the button, the button or the rectangle button, just change the button in the response area only.

So the next question is, how do you know if a point is in an area that is not rectangular, this way you can use the Uibezierpath method:

-(BOOL) Containspoint: (cgpoint) point;

We can draw a path based on the desired shape, and then judge by calling the method above to determine it.

For example a square button, how to let him in its circle of the range of a click to react, the other place does not react?

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *)Event {    //first call the parent class method to determine that the clicked area is indeed in the area of the buttonBOOL res = [Super Pointinside:point withevent:Event]; if(res) {//draw a circular pathUibezierpath *path =[Uibezierpath bezierPathWithOvalInRect:self.bounds]; if([path Containspoint:point]) {//if within the path area, return YesReturnyes;    } Returnno; } Returnno;}

This way, clicking on the Circle of the button will have an effect, and no response elsewhere.

IOS Custom button Effective Click Range

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.