Ios 旋转后UIView中的接触点位置错误
我尝试使用Ios 旋转后UIView中的接触点位置错误,ios,uiview,Ios,Uiview,我尝试使用ui视图制作一个自定义按钮,当触摸视图的特定部分时,执行一个操作,例如,当触摸视图日志的左侧部分时,以及当触摸视图日志的右侧部分时。 我正在使用touchsbegind和touchsended来检测触摸。 在纵向模式下,一切正常,但是,一旦旋转到横向模式,它就不工作了,视图显示在正确的位置,并检测到视图上的触摸,但触摸点似乎不在正确的x、y坐标中,因此我无法正确检测左/右触摸 在这里找不到任何以前对我有帮助的答案 已尝试查看CGAffineTransform,但仍无法修复它 - (id
ui视图制作一个自定义按钮,当触摸视图的特定部分时,执行一个操作,例如,当触摸视图日志的左侧部分时,以及当触摸视图日志的右侧部分时。
我正在使用touchsbegind
和touchsended
来检测触摸。
在纵向模式下,一切正常,但是,一旦旋转到横向模式,它就不工作了,视图显示在正确的位置,并检测到视图上的触摸,但触摸点似乎不在正确的x、y坐标中,因此我无法正确检测左/右触摸
在这里找不到任何以前对我有帮助的答案
已尝试查看CGAffineTransform
,但仍无法修复它
- (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);
}
return 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;
return ((b1 == b2) && (b2 == b3));
}
任何帮助都将不胜感激
谢谢如果按钮的大小不随旋转而改变,您可以在initWithFrame内只设置一次点,并使用边界
而不是框架
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);
然后在触摸中开始使用[触摸位置查看:self]代码>以获取按钮自身坐标系中的触摸位置
如果大小发生变化,一个解决方案是重新计算每次触摸的点数,因此将上述代码放在TouchesStart中
编辑:
如果要使用本地按钮坐标,则必须计算中心,因为self.center
包含superview坐标系中视图的中心点。请发布自定义按钮的代码。提供按钮的代码,以检测左侧或右侧以及屏幕截图。
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);