iphone - UIBezierPath Path Shape Clickble and Draggable -


i have uibezierpath inside view. want show alert when click on shape, happens alert shown not when click within shape, when click anywhere within view.

how change code when click inside colored shape, alert? also, how make shape draggable on 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 time  [alert show];  [alert release];  }  - (void)dealloc { [super dealloc]; }  @end 

the best way hit-test bezier path. requires access path outside of drawrect: method. since path same, can using static variables.

//add method 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 time      [alert show];      [alert release];  } 

edit custom view responds events inside path.

- (bool)pointinside:(cgpoint)point withevent:(uievent *)event {     return [[self mypath] containspoint:point]; } 

using method, won't need check whether or not touch inside path in touchesbegan because shouldn't events fail test. can move view changing frame origin same amount touch moved.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -