Ios 在栅格中移动对象
我试图让这些盒子在网格中自由移动,但仍然遵守以下简单规则:Ios 在栅格中移动对象,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我试图让这些盒子在网格中自由移动,但仍然遵守以下简单规则: 盒子一次只能朝一个方向移动 长方体不能重叠或分开网格 现在他们从一个细胞跳到另一个细胞,现在我能让他们自由移动吗?如果在X轴上通过触摸移动一个点,则长方体也应仅移动一个点,而不仅仅是设置下一个网格的动画 然后,在touchesend/touchecanceld上,我可以将长方体捕捉到最近的网格 谢谢 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self canMove])
return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
CGPoint cell = [self.grid getCellFromLocation:location];
BOOL allowMove = [self canMoveTo:cell.x y:cell.y];
if (allowMove) {
CGRect rect = [self.grid getCellRect:self.grid.gridView x:cell.x y:cell.y];
[self moved:cell.x y:cell.y];
self.frame = rect;
}
}
你的代码并不是实现那种漂亮动画的最佳方法,但这对你来说可能是一个可行的解决方案,尽管我觉得它有点难看,但这个小小的扩展将为你提供一个想法,你可以在以后的任何时候敲定 所以
将其添加到Box.h文件:
@interface Box : UIView {
BOOL _isAnimationInProgress;
}
而不是这一行:
@interface Box : UIView
并用这些更改覆盖框.m:
- (id)initWithType:(NSInteger)type x:(NSInteger)x y:(NSInteger)y grid:(Grid*)grid {
_isAnimationInProgress = FALSE;
// (...)
}
下面的-touch…
方法应该如下所示:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self canMove])
return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
CGPoint cell = [self.grid getCellFromLocation:location];
BOOL allowMove = [self canMoveTo:cell.x y:cell.y];
if (allowMove && _isAnimationInProgress == FALSE) {
_isAnimationInProgress = TRUE;
didMove = YES;
CGRect rect = [self.grid getCellRect:self.grid.gridView x:cell.x y:cell.y];
[UIView animateWithDuration:0.3f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
if (CGRectContainsPoint(rect, location)) {
[self moved:cell.x y:cell.y];
self.frame = rect;
}
} completion:^(BOOL finished) {
_isAnimationInProgress = FALSE;
}];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
_isAnimationInProgress = FALSE;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
_isAnimationInProgress = FALSE;
}
注意:这是我对你的代码结构所能做的最好的了。这就是我最后所做的,这使得盒子一次只能朝一个方向移动,然后允许它在网格单元内改变方向
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
CGPoint offset = CGPointMake(self.center.x + location.x - self.lastLocation.x, self.center.y + location.y - self.lastLocation.y);
CGPoint closestCenter = [self closestCenter:offset]; // Finds the closest grid cell center
CGRect rect = CGRectMake(offset.x - (self.size.width / 2), offset.y - (self.size.height / 2), self.size.width, self.size.height);
if (fabsf(closestCenter.x - offset.x) < fabsf(closestCenter.y - offset.y)) {
offset.x = closestCenter.x;
}
else {
offset.y = closestCenter.y;
}
if ([self collisionLeftRight:rect]) { // Check collision against left/right side
offset.x = self.center.x;
}
if ([self collisionTopBottom:rect]) { // Check collision against top/bottom side
offset.y = self.center.y;
}
self.lastLocation = location;
self.center = offset;
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
CGPoint location=[触摸位置视图:self.superview];
CGPoint offset=CGPointMake(self.center.x+location.x-self.lastLocation.x,self.center.y+location.y-self.lastLocation.y);
CGPoint closestCenter=[self closestCenter:offset];//查找最近的网格单元中心
CGRect rect=CGRectMake(offset.x-(self.size.width/2)、offset.y-(self.size.height/2)、self.size.width、self.size.height);
if(fabsf(closestCenter.x-offset.x)
显示您现在使用的代码。显示您处理触摸事件的代码。我知道您在做什么。我想要的是完全自由的移动,所以如果我在X轴上移动触摸屏一点,盒子也应该移动一点。我知道你在做什么。我真正想要的是:如果在X轴上通过触摸移动一个点,那么长方体也应该只移动一个点,而不仅仅是为下一个网格设置动画。然后在touchesedend/touchescanced上,我可以将框捕捉到最近的网格。@Martin,为了实现这一点,您需要重写代码的结构,并创建一个全新的概念。这是该代码在不大规模修改结构的情况下可以实现的最佳功能。你的愿望当然需要不同的概念。有什么提示吗?最好的方法是什么?我已经多次重写了这段代码,所以从头开始是没有问题的。你应该为期望的视觉效果开发一个概念。这里没有万能的暗示,你需要运用你的想象力、经验和知识;没有它们,你就无法产生任何视觉效果。