Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3932194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:25:49+00:00 2026-05-19T23:25:49+00:00

I have a UIBezierPath inside a view. I want to show an alert when

  • 0

I have a UIBezierPath inside a view. I want to show an alert when I click on the shape, but what actually happens is that the alert is shown not only when I click within the shape, but also when I click anywhere within the view.

How do I change this code so that only when I click inside the colored shape, I get the alert?
Also, how do I make the shape draggable on the screen?

#import "draw2D.h"
@implementation draw2D

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    // Initialization code.
}
return self;
}

- (void)drawRect:(CGRect)rect {
UIBezierPath*    aPath = [UIBezierPath bezierPath]; 

[aPath moveToPoint:CGPointMake(200.053,79.688)];    
[aPath addLineToPoint:CGPointMake(100.053,179.688)];
[aPath addLineToPoint:CGPointMake(304.412,280.125)];
[aPath addLineToPoint:CGPointMake(308.055,298.513)];
[aPath addLineToPoint:CGPointMake(200.053,79.688)];

[aPath closePath]; 

[[UIColor blackColor] setStroke]; 
[[UIColor redColor] setFill]; 

CGContextRef aRef = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(aRef, 50, 50); 
aPath.lineWidth = 5; 
[aPath fill]; 
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; 
//After some time 
[alert show]; 
[alert release]; 
}

- (void)dealloc {
[super dealloc];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:25:50+00:00Added an answer on May 19, 2026 at 11:25 pm

    The best way to do this is to hit-test the bezier path. This requires access to the path outside of the drawRect: method. Since your path is the always the same, you can do this easily using static variables.

    //add this method to your class
    - (UIBezierPath *)myPath {
        static UIBezierPath *path = nil;
        if(!path) {
            path = [[UIBezierPath bezierPath] retain];
            [path moveToPoint:CGPoingMake(200.053,79.688)];
            [path addLineToPoint:CGPointMake(100.053,179.688)];
            [path addLineToPoint:CGPointMake(304.412,280.125)];
            [path addLineToPoint:CGPointMake(308.055,298.513)];
            [path addLineToPoint:CGPointMake(200.053,79.688)];
            [path closePath];
            path.lineWidth = 5;
        }
        return path;
    }
    
    - (void)drawRect:(CGRect)rect {
        UIBezierPath*    aPath = [self myPath];
    
        [[UIColor blackColor] setStroke]; 
        [[UIColor redColor] setFill]; 
    
        CGContextRef aRef = UIGraphicsGetCurrentContext(); 
        CGContextTranslateCTM(aRef, 50, 50); 
        [aPath fill]; 
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if(![[self myPath] containsPoint:[[touches anyObject] locationInView:self]]) return;
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; 
        //After some time 
        [alert show]; 
        [alert release]; 
    }
    

    Edit for a custom view which only responds to events inside the path.

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        return [[self myPath] containsPoint:point];
    }
    

    Using this method, you won’t need to check whether or not the touch is inside the path in touchesBegan because you shouldn’t get any events which would fail the test. You can move the view by changing its frame origin the same amount the touch moved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have not tested on windows. but in ubuntu when u disconnect from the network,
Here's what I want to do: I have a UIBezierPath and I want to
Have a problem that seems easy on paper but i'm having a big problem
I have a CAShapeLayer that takes his shape from path. This creates a layer
I'm drawing a filled shape using the UIBezierPath and i want to check if
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
I have a view that's rendering a PDF into a CATiledLayer. This is working
I have a method that will calculate the height of a text line. But
I have a simple drawing class. There is a view controller that includes a
I have a progress view that I'm trying to animate the change in progress.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.