滑块的值在套接字中传输,但滑块的视觉位置不会改变

发布于 2024-11-16 03:05:28 字数 1252 浏览 3 评论 0原文

我在 iPod 客户端和 C 服务器之间有一个连接。 我可以在移动滑块时发送滑块的数据,并正确获取服务器上的数据。唯一的问题是滑块的蓝点没有移动,它下面的标签也没有改变滑块的值。当我断开连接时,蓝点移动到应有的位置,并且标签显示其值。以下是代码:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
  switch (streamEvent)
  {         
    case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

        if (theStream == oStream)
        {
         //send data
             const uint8_t *buffer = (const uint8_t *)[dataSlider UTF8String];  
             NSInteger err = [self writeToServer:buffer];                 
             if ( err == -1)
                NSLog(@"Error sending data."); 
             else   
                NSLog(@"Success sending data.");
        }
        break;
}

- (IBAction)sliderChanged:(id)sender 
{ 
   UISlider *slider = (UISlider *)sender; 
   progressAsInt = (int)(slider.value + 0.5f); 
   sliderValue = [[NSString alloc] initWithFormat:@"%d", progressAsInt]; 
   sliderLabel.text = sliderValue; 
   dataSlider = sliderValue;
}

这是它应该工作的方式:服务器总是要求 printf 提供某些内容。每当服务器调用 read() 时,我都会移动滑块并发送其值。

问题:滑块在视觉上没有移动,但它的值发生了变化(我可以在服务器的 printfs 中看到它)。

有什么想法吗?

I have a connection between iPod client and C server.
I can send the data of the slider as I move the slider, and get the data on the server correctly. Only problem is the blue point of the slider doesn't move, nor does the label under it change the slider's value. When I disconnect, the blue point moves to the point it should be and the label shows its value. Here are the codes:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
  switch (streamEvent)
  {         
    case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

        if (theStream == oStream)
        {
         //send data
             const uint8_t *buffer = (const uint8_t *)[dataSlider UTF8String];  
             NSInteger err = [self writeToServer:buffer];                 
             if ( err == -1)
                NSLog(@"Error sending data."); 
             else   
                NSLog(@"Success sending data.");
        }
        break;
}

- (IBAction)sliderChanged:(id)sender 
{ 
   UISlider *slider = (UISlider *)sender; 
   progressAsInt = (int)(slider.value + 0.5f); 
   sliderValue = [[NSString alloc] initWithFormat:@"%d", progressAsInt]; 
   sliderLabel.text = sliderValue; 
   dataSlider = sliderValue;
}

This is the way it should work: the server is always asking for something to printf. I move the slider and send its value, whenever the server calls read().

Problem: the slider doesn't visually move, but its value changes (i can see it in the server's printfs).

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-11-23 03:05:28

可能有两个问题:

  1. 您需要调用setNeedsDisplay

  2. 您正在使用与服务器的同步/阻塞连接。如果您使用同步/阻塞通信,那么一切(包括 UI 更新)都将等到通信结束。如果是这种情况,则使用异步连接。由于断开连接时 UI 会更新,因此这是最有可能出现的问题。

There might be two problems:

  1. You need to call setNeedsDisplay.

  2. You are using a synchronous/blocking connection with the server. If you use a synchronous/blocking communication then everything, including UI update, will wait until the communication is over. If that is the case then use a asynchronous connection. As the UI is updated when you are disconnected, this is the most probable problem.

不乱于心 2024-11-23 03:05:28

嗯..我让它工作了,但是有点滞后:使用的代码是:(在 stream:handleEvent: 方法中)

case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        if (theStream == oStream)
        {
            //send data
            const uint8_t *buffer = (const uint8_t *)[dadosSlider UTF8String];  
            NSInteger err = [self writeToServer:buffer];
            // [msg release];

            if ( err == -1)
                NSLog(@"Erro a enviar os dados."); 
            else   
                NSLog(@"Transmitiu com sucesso.");


        }
});
        break;

关于如何让它完美工作的想法? Thks

(澄清一下。这里的目的是让 iPod 仅在服务器请求 read() 时发送数据)

Hum.. i got it working, but kinda laggy :s the code used was: (in the stream:handleEvent: method)

case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        if (theStream == oStream)
        {
            //send data
            const uint8_t *buffer = (const uint8_t *)[dadosSlider UTF8String];  
            NSInteger err = [self writeToServer:buffer];
            // [msg release];

            if ( err == -1)
                NSLog(@"Erro a enviar os dados."); 
            else   
                NSLog(@"Transmitiu com sucesso.");


        }
});
        break;

Ideas on how to get it working flawlessly? Thks

(Just to be clear. The aim here is for the iPod only send data at the moment the server asks for a read())

离去的眼神 2024-11-23 03:05:28

似乎常量“知道流有可用空间”正在堵塞程序和 UI。我更改了程序的流程以等待来自服务器的消息,只有在此之后它才会发送数据。这里是:

case NSStreamEventHasBytesAvailable:
        event = @"NSStreamEventHasBytesAvailable";
        if (theStream == iStream)
         {           
            //read data
            uint8_t buffer[1024];
            int len;
            while ([iStream hasBytesAvailable])
            {
                len = [iStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    if (nil != output)
                    {                 
                        //Now that the server gave its signal to move on, send data to server

                        NSString *data = [[torqueValue stringByAppendingString:@" "] stringByAppendingString:angleValue];
                        const uint8_t *buffer2 = (const uint8_t *)[data UTF8String];  
                        NSInteger err = [self writeToServer:buffer2];

                        if ( err == -1)
                            NSLog(@"Erro a enviar os dados."); 
                        else   
                            NSLog(@"Transmitiu com sucesso.");

                    }
                }
            }    
         }
        break;

Seems that the constant "knowing that the stream had space available" was clogging the program and UI. I changed the flow of the program to wait for a message from the sever and only after that it would send the data. Here it is:

case NSStreamEventHasBytesAvailable:
        event = @"NSStreamEventHasBytesAvailable";
        if (theStream == iStream)
         {           
            //read data
            uint8_t buffer[1024];
            int len;
            while ([iStream hasBytesAvailable])
            {
                len = [iStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    if (nil != output)
                    {                 
                        //Now that the server gave its signal to move on, send data to server

                        NSString *data = [[torqueValue stringByAppendingString:@" "] stringByAppendingString:angleValue];
                        const uint8_t *buffer2 = (const uint8_t *)[data UTF8String];  
                        NSInteger err = [self writeToServer:buffer2];

                        if ( err == -1)
                            NSLog(@"Erro a enviar os dados."); 
                        else   
                            NSLog(@"Transmitiu com sucesso.");

                    }
                }
            }    
         }
        break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文