Ios 在用户按住触摸键的同时移动对象
我想创建一个简单的应用程序(实际上是公路战斗机的克隆),其中:矩形UIView(汽车)向左移动,如果用户按下屏幕的左侧,它向右移动,如果用户按下右侧 我已经知道了如何识别触摸(有两种方法:使用“UILongPressGestureRecognizer”和“touchesBeging:”功能),但我不知道如何创建动画 伪代码应该是这样的(也许我错了):Ios 在用户按住触摸键的同时移动对象,ios,objective-c,2d-games,Ios,Objective C,2d Games,我想创建一个简单的应用程序(实际上是公路战斗机的克隆),其中:矩形UIView(汽车)向左移动,如果用户按下屏幕的左侧,它向右移动,如果用户按下右侧 我已经知道了如何识别触摸(有两种方法:使用“UILongPressGestureRecognizer”和“touchesBeging:”功能),但我不知道如何创建动画 伪代码应该是这样的(也许我错了): 如果用户触摸 如果触摸坐标x>160,则 持有 UIView.coordinate.x+=0.01; 如果touch.coordinate.x有另
如果用户触摸
如果触摸坐标x>160,则
持有
UIView.coordinate.x+=0.01;
如果touch.coordinate.x有另一个函数称为-(void)touchesend
,那么当touchesend开始时,您可以将汽车一直移动到一个方向,并在touchesend被调用时停止移动。
简单的解决方案是使用类似于bool carismovingleft
的标志在touchbegin中调用循环移动函数:
- (void)touchesBegan... {
//mark the flag
self.carismovingleft = true;
//start moving the car
[self movingcar];
}
- (void)movingcar {
if (self.carismovingleft) {
//move the car subview to left a little bit
.......
//now we keep moving a little bit till someone stop it
//call moving again after a delay
//otherwise the car will move all the way in one frame
[self performSelector: @selector(movingcar) withObject:nil afterDelay:1];
}
}
- (void)touchesEnded... {
//stop flag
self.carismovingleft = false;
}
这只是如何实现它的一个基本概念,你需要自己编写代码。那么你希望你的对象在用户触摸并按住按钮时开始移动,并一直移动直到用户放开为止
长按手势识别器在发出单个按下事件之前会给您一个延迟。那不行
您需要实现touchestarted/touchesend。@highwing方法的一些变体应该会起作用。您需要微调延迟时间间隔和每间隔移动车辆的量
由于您需要左右移动,我可能会使用枚举来指定移动方向,然后在movingCar方法中使用switch语句来处理从一个方向到所有方向的移动
在high wing的带领下,代码可能如下所示:
typedef enum
{
notMoving,
movingLeft,
movingRight
} movementDirections;
movementDirections currentDirection;
- (void)touchesBegan...
{
//mark the flag
if (touchPoint.x > 160)
{
currentDirection = movingRight;
}
else if (touchPoint.x < 160)
{
currentDirection = movingLeft;
}
//start moving the car
[self movingCar];
}
- (void)movingCar
{
switch self.currentDirection
{
case movingLeft:
//move the car subview to left a little bit
.......
//now we keep moving a little bit till someone stop it
//call moving again after a delay
//otherwise the car will move all the way in one frame
[self performSelector: @selector(movingCar) withObject:nil afterDelay:.1];
break;
case movingRight:
//move the car subview to right a little bit
.......
//now we keep moving a little bit till someone stop it
//call moving again after a delay
//otherwise the car will move all the way in one frame
[self performSelector: @selector(movingCar) withObject:nil afterDelay:.1];
break;
default:
break; //Do nothing (we're not moving.)
}
}
- (void)touchesEnded...
{
//stop flag
self.currentDirection = notMoving;
}
typedef枚举
{
不动,
向左移动,
移动权
}运动方向;
运动方向;
-(无效)触摸开始。。。
{
//标记旗帜
如果(接触点x>160)
{
当前方向=向右移动;
}
否则如果(接触点x<160)
{
currentDirection=向左移动;
}
//开始移动汽车
[自动驾驶汽车];
}
-(无效)移动汽车
{
开关自导向
{
案例左移:
//将汽车子视图向左移动一点
.......
//现在我们继续前进,直到有人阻止它
//呼叫延迟后再次移动
//否则,汽车将在一个框架内一路移动
[自执行选择器:@selector(movingCar),对象:nil afterDelay:.1];
打破
案件移动权:
//将汽车子视图向右移动一点
.......
//现在我们继续前进,直到有人阻止它
//呼叫延迟后再次移动
//否则,汽车将在一个框架内一路移动
[自执行选择器:@selector(movingCar),对象:nil afterDelay:.1];
打破
违约:
break;//什么也不做(我们不移动。)
}
}
-(无效)触碰。。。
{
//停车标志
self.currentDirection=不移动;
}
编辑:我是缩进(链接)的支持者。这是我所鄙视的K&R风格缩进的有效替代品@evlogii,请不要将我的代码更改为使用K&R样式。这是个人品味的问题。我不会将您的代码从K&R样式转换为Allman样式,所以您不会将我的代码转换为K&R样式,好吗
typedef enum
{
notMoving,
movingLeft,
movingRight
} movementDirections;
movementDirections currentDirection;
- (void)touchesBegan...
{
//mark the flag
if (touchPoint.x > 160)
{
currentDirection = movingRight;
}
else if (touchPoint.x < 160)
{
currentDirection = movingLeft;
}
//start moving the car
[self movingCar];
}
- (void)movingCar
{
switch self.currentDirection
{
case movingLeft:
//move the car subview to left a little bit
.......
//now we keep moving a little bit till someone stop it
//call moving again after a delay
//otherwise the car will move all the way in one frame
[self performSelector: @selector(movingCar) withObject:nil afterDelay:.1];
break;
case movingRight:
//move the car subview to right a little bit
.......
//now we keep moving a little bit till someone stop it
//call moving again after a delay
//otherwise the car will move all the way in one frame
[self performSelector: @selector(movingCar) withObject:nil afterDelay:.1];
break;
default:
break; //Do nothing (we're not moving.)
}
}
- (void)touchesEnded...
{
//stop flag
self.currentDirection = notMoving;
}