通过触摸结束事件处理重叠按钮?

Handling overlapping button by touch ended event?

我对这个逻辑很困惑,请帮我想办法。 我在 UIview 上的每一次触摸都在制作 uibutton 并且它原则上有效。 但第二次触摸时,该按钮不应与前一个按钮重叠。 这是在 'touch ended' 事件上创建按钮的代码。

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 loop.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 循环中犯了错误..它每次都检查,因为我返回了一个值,所以它永远不会检查另一个 button.finally 我通过像这样更改代码来解决问题。 .

- (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;
}