我尝试使用UIView
制作自定义按钮,并在触摸视图的特定部分时执行操作,例如触摸视图日志的左侧部分时左键和触摸时右侧视图日志右侧。
我使用touchesBegan
和touchesEnded
来检测触摸。
在纵向模式下一切正常,但是一旦旋转到横向模式它不起作用,视图显示在正确的位置并且检测到视图上的触摸但似乎触摸点不是在正确的x,y坐标中,所以我无法正确检测LEFT / RIGHT触摸。
无法在此找到任何有助于我的答案。
尝试调查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));
}
在视图控制器viewDidLoad中:
CustomButton *customButton = [[CustomButton alloc] initWithFrame:CGRectMake(1, self.view.bounds.size.height - 106, 105, 105)];
[self.view addSubview:customButton];
任何帮助将不胜感激。
由于
答案 0 :(得分:1)
如果按钮的大小不随旋转而改变,您可以在initWithFrame中设置一次点,并使用bounds
而不是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);
然后在touchesBegan中使用[touch locationInView:self];
来获取按钮自己的坐标系中的触摸位置。
如果尺寸发生变化,解决方法可能是在每次触摸时重新计算点数,因此将上述代码放在touchesBegan中。
编辑:
如果要使用本地按钮坐标,则必须计算中心,因为self.center
包含超视图坐标系中视图的中心点。