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

时间:2015-05-16 08:54:27

标签: ios objective-c touch-event

not coming like this我对此逻辑感到困惑,请帮我找一个解决方案。 我uibutton每次触摸都会UIview,原则上它起作用。 但第二次触摸时按钮不应与上一个按钮重叠。 以下是在触摸结束时创建按钮的代码。事件

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,top,y,bottom values。

相对于任何一方的上一个按钮,它可以创建减去10个点的按钮。

提前致谢。

1 个答案:

答案 0 :(得分:1)

Acutally我的代码是正确的,我在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;
}