for循环执行不';我不允许触摸iPhone

for循环执行不';我不允许触摸iPhone,iphone,objective-c,xcode,sleep,Iphone,Objective C,Xcode,Sleep,嗯。我知道标题可能会让人困惑 我实现的逻辑是这样的 应用中有一个检测器(如自行车速度表-移动箭头) 当用户点击开始扫描按钮时,第一个方法执行 NowStartMovements决定随机旋转和要停止的随机数 探测器上有1到10个数字 到目前为止,一切正常 下面的代码是无错误的 箭头完美移动并在适当位置停止(随机决定) 但问题是“我已经为运动实现了for循环” 因此,在执行for循环时,用户交互未启用。 我还添加了我已经实现的代码 -(iAction)扫描开始:(id)发送方 { btnSca

嗯。我知道标题可能会让人困惑

我实现的逻辑是这样的

  • 应用中有一个检测器(如自行车速度表-移动箭头)
  • 当用户点击开始扫描按钮时,第一个方法执行
  • NowStartMovements决定随机旋转和要停止的随机数
  • 探测器上有1到10个数字
  • 到目前为止,一切正常
  • 下面的代码是无错误的
  • 箭头完美移动并在适当位置停止(随机决定)

  • 但问题是“我已经为运动实现了for循环”

  • 因此,在执行for循环时,用户交互未启用。
我还添加了我已经实现的代码


-(iAction)扫描开始:(id)发送方
{
btnScan.enabled=NO;stopThroughButtons=NO;shouldNeedleGoRightSide=YES;currentNeedleValue=1;nxtNeedleValue=2;
[NSTimer scheduledTimerWithTimeInterval:0目标:自选择器:@selector(nowStartMovements)用户信息:无重复:否];
}
-(无效)立即开始移动{

totalRotations=arc4random()%9;如果(totalRotations您需要重写您的逻辑,因此是计时器在执行休眠,而不是usleep。重写您的函数,以便可重复计时器的每次迭代都执行for循环中的操作


问题是for循环在主线程上休眠。如果使用计时器并将repeats设置为YES,则此操作通常会执行正在执行的for/sleep模式。如果要停止它,请调用[timer invalidate];

您需要重写您的逻辑,这样做的是计时器而不是usleep。重写您的函数,这样可重复计时器的每次迭代都会执行for循环中的操作


问题是for循环在主线程上休眠。如果您使用计时器并将repeats设置为YES,则这通常会执行您正在执行的for/sleep模式。当您要停止它时,请调用[timer invalidate];

理想情况下,您应该使用计时器来安排针的移动。现有代码的最快解决方案是:

  • StartScan
    中,将
    -scheduledTimerWithTimeInterval:
    更改为
    -performSelect背景:

  • nowStartMovements
    中,将
    -detachNewThreadSelector:
    更改为
    -performSelectorOnMainThread:


通过这种方式,
usleep
发生在后台线程上,不会阻止主线程。只要主线程被阻止,UI就会被冻结。

理想情况下,您应该使用计时器来安排针的移动。现有代码的最快解决方案是:

  • StartScan
    中,将
    -scheduledTimerWithTimeInterval:
    更改为
    -performSelect背景:

  • nowStartMovements
    中,将
    -detachNewThreadSelector:
    更改为
    -performSelectorOnMainThread:


这样,
usleep
发生在后台线程上,不会阻止主线程。只要主线程被阻止,UI就会被冻结。

我已经按照你的建议实现了。我已经按照你的建议实现了。
-(IBAction)ScanStart:(id)sender
{
btnScan.enabled=NO; stopThroughButtons=NO; shouldNeedleGoRightSide=YES; currentNeedleValue=1; nxtNeedleValue=2;
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(nowStartMovements) userInfo:nil repeats:NO];
}

-(void)nowStartMovements{
totalRotations=arc4random()%9; if(totalRotations<3) totalRotations+=3;
currentRotation=0;stopValue=arc4random()%11; if(stopValue<1)stopValue=1;
int totalMovements=(totalRotations-1)*10 + ( (totalRotations%2==0)?10-stopValue:stopValue ), i;
for(i=0;i<totalMovements;i++){
    if (stopThroughButtons) return;
    [NSThread detachNewThreadSelector:@selector(moveNeedle) toTarget:self withObject:nil];
    usleep(200000);
}
}

-(void)moveNeedle{
spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
double fromValue=[[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue];
double toValue=[[arrayOfFloatValues objectAtIndex:nxtNeedleValue-1] doubleValue];
spinAnimation.duration=0.2;
spinAnimation.fromValue=[NSNumber numberWithFloat:fromValue];
spinAnimation.toValue = [NSNumber numberWithFloat:toValue];
[imgNideel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
[NSThread detachNewThreadSelector:@selector(MoveActualNeedle) toTarget:self withObject:nil];
}

-(void)MoveActualNeedle{
if(shouldNeedleGoRightSide){        
    if(currentNeedleValue<9) { currentNeedleValue++; nxtNeedleValue++;}
    else { shouldNeedleGoRightSide=NO; currentNeedleValue=10; nxtNeedleValue=9;
}
    imgNideel.transform=CGAffineTransformMakeRotation([[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue]);
} else {
    if(currentNeedleValue>2){ currentNeedleValue--; nxtNeedleValue--;} 
    else { shouldNeedleGoRightSide=YES; currentNeedleValue=1; nxtNeedleValue=2;
}
    imgNideel.transform=CGAffineTransformMakeRotation([[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue]);
}   
}