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

标签 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]];
    }
}

每当用户触摸 View 时,我都会获取 View 的 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 值。

它可以为任何一侧创建相对于前一个按钮减去 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;
}

关于ios - 通过触摸结束事件处理重叠按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273701/

相关文章:

ios - 我的 $.ajax 请求在 iOS 11.3 上停止正常工作

ios - 如何在推送通知中使用 iPhone 默认铃声和长达 30 秒的持续振动,

objective-c - beginTrackingWithTouch 和 touchesBegan 之间有什么区别?

javascript - JQuery .on ("click") 在触摸设备上触发 "mouseover"

java - 在android上拖放一个按钮..帮助

ios - 具有多个部分数据问题的双向滚动 collectionView

ios - 如何针对 CoreData 中的多个关系执行复合查询

objective-c - NSOperation子类发布使Instruments崩溃

ios - 调整 UICollectionView 大小以适应 UIImage

android - 为 View 禁用点击事件但不为它的 child 禁用点击事件