Monday 15 June 2015

ios - Wrong touch point location in UIView after rotatation -



ios - Wrong touch point location in UIView after rotatation -

i'm trying create custom button using uiview , when touched on specific part of view perform action, illustration when touching left part of view log left , when touching right part of view log right. i'm using touchesbegan , touchesended observe touch. works fine when in portrait mode, however, 1 time rotated landscape mode doesn't work, view displayed in right place , touch on view detected seems touch point not in right x,y coordinates can't observe left/right touches correctly.

couldn't find previous answers here helped me.

tried looking cgaffinetransform, still can't prepare it.

- (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_default.png"]]; self.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibletopmargin; topleft = cgpointmake(self.frame.origin.x, self.frame.origin.y); topright = cgpointmake(self.frame.origin.x + 105, self.frame.origin.y); bottomleft = cgpointmake(self.frame.origin.x, self.frame.origin.y + 105); bottomright = cgpointmake(self.frame.origin.x + 105, self.frame.origin.y + 105); nslog(@"topleft: x = %f, y = %f", topleft.x, topleft.y); nslog(@"topright: x = %f, y = %f", topright.x, topright.y); nslog(@"bottomleft: x = %f, y = %f", bottomleft.x, bottomleft.y); nslog(@"bottomright: x = %f, y = %f", bottomright.x, bottomright.y); nslog(@"self.center: x = %f, y = %f", self.center.x, self.center.y); } homecoming self; } - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { nslog(@"topleft: x = %f, y = %f", topleft.x, topleft.y); nslog(@"topright: x = %f, y = %f", topright.x, topright.y); nslog(@"bottomleft: x = %f, y = %f", bottomleft.x, bottomleft.y); nslog(@"bottomright: x = %f, y = %f", bottomright.x, bottomright.y); nslog(@"self.center: x = %f, y = %f", self.center.x, self.center.y); uitouch *touch = [touches anyobject]; cgpoint touchpoint = [touch locationinview:nil]; nslog(@"touchesbegan: touchpoint: x = %f, y = %f", touchpoint.x, touchpoint.y); if ([self ispoint:touchpoint intriangle:topleft :self.center :topright]) { nslog(@"touchesbegan: up"); direction = 1; self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_pressed_up.png"]]; } else if ([self ispoint:touchpoint intriangle:bottomleft :self.center :bottomright]) { nslog(@"touchesbegan: down"); direction = 2; self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_pressed_down.png"]]; } else if ([self ispoint:touchpoint intriangle:topleft :self.center :bottomleft]) { nslog(@"touchesbegan: left"); direction = 3; self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_pressed_left.png"]]; } else if ([self ispoint:touchpoint intriangle:topright :self.center :bottomright]) { nslog(@"touchesbegan: right"); direction = 4; self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_pressed_right.png"]]; } else { nslog(@"touchesbegan: nothing"); } } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { self.backgroundcolor = [[uicolor alloc] initwithpatternimage:[uiimage imagenamed:@"round_directions_button_default.png"]]; } - (bool) ispoint:(cgpoint)point intriangle:(cgpoint)v1 :(cgpoint)v2 :(cgpoint)v3 { bool b1, b2, b3; b1 = ((point.x - v2.x) * (v1.y - v2.y) - (v1.x - v2.x) * (point.y - v2.y)) < 0.0f; b2 = ((point.x - v3.x) * (v2.y - v3.y) - (v2.x - v3.x) * (point.y - v3.y)) < 0.0f; b3 = ((point.x - v1.x) * (v3.y - v1.y) - (v3.x - v1.x) * (point.y - v1.y)) < 0.0f; homecoming ((b1 == b2) && (b2 == b3)); }

in view controller viewdidload:

custombutton *custombutton = [[custombutton alloc] initwithframe:cgrectmake(1, self.view.bounds.size.height - 106, 105, 105)]; [self.view addsubview:custombutton];

any help appreciated.

thanks

if size of button doesn't alter rotation can set points 1 time within initwithframe , utilize bounds instead of frame.

topleft = cgpointmake(0, 0); topright = cgpointmake(self.bounds.size.width, 0); bottomleft = cgpointmake(0, self.bounds.size.height); bottomright = cgpointmake(self.bounds.size.width, self.bounds.size.height);

then in touchesbegan utilize [touch locationinview:self]; touch location in button own coordinate system.

if size changes, solution can recalculate points on every touch place above code within touchesbegan.

edit: if want utilize local button coordinate have calculate center because self.center contains center point of view in superview’s coordinate system.

ios uiview

No comments:

Post a Comment