【发布时间】:2015-05-16 08:54:27
【问题描述】:
我对这个逻辑感到困惑,请帮助我找到解决方案。
我正在制作一个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 值。
它可以为任何一侧创建相对于前一个按钮减去10点的按钮。
提前致谢。
【问题讨论】:
-
我认为 loop.touchPoint.x 的问题是你没有真正触摸 x 点返回相同这就是为什么你得到 ovverrite 按钮因为它的位置在相同的 x 和 y 位置。
标签: ios objective-c touch-event