Ios 通过触摸结束事件处理重叠按钮?
我对这个逻辑感到困惑,请帮我找到解决办法。 我正在制作一个Ios 通过触摸结束事件处理重叠按钮?,ios,objective-c,touch-event,Ios,Objective C,Touch Event,我对这个逻辑感到困惑,请帮我找到解决办法。 我正在制作一个ui按钮,每次点击ui视图,它在原则上工作但第二次触摸时,该按钮不应与上一个按钮重叠。 以下是在“触控结束”事件中创建按钮的代码 int const kRadius = 4; - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { loop = [[MagnifierView alloc] init];
ui按钮
,每次点击ui视图
,它在原则上工作但第二次触摸时,该按钮不应与上一个按钮重叠。
以下是在“触控结束”事件中创建按钮的代码
int const kRadius = 4;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
loop = [[MagnifierView alloc] init];
loop.viewToMagnify = self;
[loop setNeedsDisplay];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[btnCamera removeFromSuperview];
if(self.activateEditMode){
self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(addLoop)
userInfo:nil
repeats:NO];
// just create one loop and re-use it.
if(loop == nil){
loop = [[MagnifierView alloc] init];
loop.viewToMagnify = self;
}
UITouch *touch = [touches anyObject];
loop.touchPoint = [touch locationInView:self];
[loop setNeedsDisplay];
}else{
// Message
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleAction:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
self.touchTimer = nil;
if(self.activateEditMode){
[self createCameraBtn];
[loop removeFromSuperview];
[self setBackgroundColor:[UIColor colorWithRed:(102/255) green:(102/255) blue:(102/255) alpha:1]];
}
}
每当用户触摸视图时,我都会获取视图的x,y值,并将其保存到CGPoint
循环中。touchPoint
我还将x,y值保存到一个数据库中,以便在创建下一个按钮之前对照我存储在数据库中的前一个x,y值进行检查
到目前为止一切正常。
当我处理前面的值时,我在代码中没有正确地执行它
处理代码和按钮创建
- (BOOL)handleOverlapping{
for (ImageInfo *img in self.profileInfo.imageInfo)
{
int xr = [img.xcord intValue] + kRadius;
int yr = [img.ycord intValue] + kRadius;
if ((([selectedXCord intValue] - kRadius) <= xr) && (([selectedYCord intValue] - kRadius) <=yr))
{
[CSNotificationView showInViewController:[(SkinViewController *)[self.superview nextResponder] navigationController]
style:CSNotificationViewStyleError message:kOVERLAPING_REDDOT_ERROE];
return false;
}
else if ((([selectedXCord intValue] - kRadius+10) <= xr) && (([selectedYCord intValue] - kRadius+10) <=yr))
{
[CSNotificationView showInViewController:[(SkinViewController *)[self.superview nextResponder] navigationController]
style:CSNotificationViewStyleError message:kOVERLAPING_REDDOT_ERROE];
return false;
}
}
return true;
}
- (void)createCameraBtn{
//[self colorOfPoint:loop.touchPoint];
selectedXCord = [NSNumber numberWithDouble:loop.touchPoint.x-12];
selectedYCord = [NSNumber numberWithDouble:loop.touchPoint.y-75];
// Check whether user focusing on monitored region.
if(![self handleOverlapping])
return;
// else if (![self red:red green:green blue:blue])
// return;
btnCamera = [UIButton buttonWithType:UIButtonTypeCustom];
btnCamera.frame = CGRectMake(loop.touchPoint.x-12, loop.touchPoint.y-75, 25, 25);
[btnCamera setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnCamera setImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
[btnCamera addTarget:self action:@selector(captureSkin) forControlEvents:UIControlEventTouchDown];
[self addSubview:btnCamera];
}
我认为我错误地处理了重叠方法
用这种方法
1. xr,yr
是上一个按钮的x,y值,
2. selectedYcord,selectedXcore
是当前触摸位置。
3.每个按钮的宽度和高度均为25
我想在这里做的是确保第二个按钮不会与前一个按钮重叠
示例x、顶部、y、底部值
它可以为任何一侧创建相对于上一个按钮减去10点的按钮
提前感谢。实际上,我的代码是正确的,我在for循环中犯了一个错误。它每次都检查,因为我返回了一个值,所以它从不检查其他按钮。最后,我通过这样更改代码找到了解决方案
- (BOOL)handleOverlapping{
BOOL firstCondition=false;
BOOL secondCondition=false;
for (ImageInfo *img in self.profileInfo.imageInfo){
int xr = [img.xcord intValue];
int yr = [img.ycord intValue];
if (xr+12<=[selectedXCord intValue]||xr-12>=[selectedXCord intValue]){
firstCondition=true;
}
else if (yr+12<=[selectedYCord intValue]||yr-12>=[selectedYCord intValue]){
secondCondition=true;
}
else
{
[CSNotificationView showInViewController:[(SkinViewController *)[self.superview nextResponder] navigationController]
style:CSNotificationViewStyleError message:kOVERLAPING_REDDOT_ERROE];
return false;
}
}
if (firstCondition || secondCondition){
return true;
}
return true;
}
-(BOOL)手柄重叠{
BOOL firstCondition=false;
BOOL secondCondition=false;
用于(self.profileInfo.ImageInfo中的ImageInfo*img){
int xr=[img.xcord intValue];
int yr=[img.ycord intValue];
如果(xr+12=[selectedXCord intValue]){
firstCondition=true;
}
否则如果(yr+12=[selectedYCord intValue]){
secondCondition=true;
}
其他的
{
[CSNotificationView showInViewController:[(SkinViewController*)[self.superview下一响应者]导航控制器]
样式:CSNotificationViewStyle错误消息:kOVERLAPING_REDDOT_ERROE];
返回false;
}
}
如果(第一个条件| |第二个条件){
返回true;
}
返回true;
}
我认为loop.touchPoint.x的问题在于,您并没有得到实际的触摸x点,它返回相同的结果,这就是为什么您得到OVERRITE按钮,因为它位于相同的x和y位置。