簡體   English   中英

掃描藍牙設備時未使用NSTimer更新iOS背景視圖

[英]iOS background view not updated using NSTimer while scanning the bluetooth DEVICE

我正在嘗試獲取藍牙掃描頁面以使用藍牙配對BLE設備。 我發現NSUInteger [ _ble.scannedPeripheral count ]在掃描時確實發生了變化。 但是,在執行時,背景視圖圖像和頁面甚至無法更改。 如果顯示可用BLE設備的變量從0更改為1,2或3,您能否告訴我其他方法來更改頁面?

以下是我的代碼:(僅相關)

- (void)viewDidAppear:(BOOL)animated
{
    if (_ble)
    {
        _ble.delegate = (id) self;
        _ble.btStatus = BT_IDLE;
        [_ble startScanning];
    }

    [NSTimer scheduledTimerWithTimeInterval:0.2f  target:self selector:@selector(reloadData) userInfo:nil repeats:YES];

}


-(void) reloadData {


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time consuming workout
            dispatch_async(dispatch_get_main_queue(), ^{
            // UI update workout for bluetooth scanning

            if( [ _ble.scannedPeripheral count ] > 0 ){
                [self stopAnimatingImages];
                [self setTapDemo :  [UIImage imageNamed:@"pairing_d.png"]  : @"Pairing"  : @"#C4CCCF"] ;
            }else{
                [self setTapDemo : [self loadingImage] : @"Pairing"  : @"#C4CCCF"] ;
                [self animateImages];
            }
        });
    });
}




- (void) setTapDemo: (UIImage *) cover  : (NSString *) title : (NSString *) colorHex{

    image = [UIImage imageNamed:@"shaded_cal.png"];
    imageA = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _container = [[UIView alloc] initWithFrame:[self.view bounds]];
    [imageA setImage:cover];
    imageA.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [imageA addGestureRecognizer:myGesture];
    [imageA setContentMode:UIViewContentModeScaleAspectFill];

    myLabelQ = [self constructLabelT:title:0.27:0.08: colorHex:25];
    myLabelBack =[self constructLabelT:@"BACK":0.04:0.01:@"#C4CCCF":18] ;
    if( bleCount > 0){

         for(NSUInteger  i = 0 ; i < [ _ble.scannedPeripheral count ] ; i ++){
            DevicePeriperal *device;
            NSString *uuid = [_ble.scannedPeripheralKey objectAtIndex:i];
             NSLog (@"device uuid = %@",  uuid);
             if (uuid)
             {
                 device = [_ble.scannedPeripheral objectForKey:uuid];
                 NSData * ssx = device.advertdata ;
                 device.rowIndex = i;
                 NSLog (@"device advert = %@",  ssx);
                 if([ssx length] > 0){
                     NSData *macD = [ssx subdataWithRange:NSMakeRange(0, 6)];
                     NSData *pairD = [ssx subdataWithRange:NSMakeRange(6, 1)];
                     NSString* newStr =  [self hexRepresentationWithSpaces:pairD : NO];
                     NSString* newMAC =  [self hexRepresentationWithSpaces:macD : YES];
                     NSLog (@"newStr = %@", newStr );
                     NSLog (@"newMAC = %@", newMAC );
                     _checkSumByte = [self calculateChecksum:newMAC];
                 }

                 NSLog (@"device = %@", device.uuid);
                 if (device )
                 {
                     UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
                     float change =  0.15*i;
                     float yPosition = 0.25 + change ;
                     [imageA addSubview:[self deviceGet:dImage:device.deviceName: 0.40 : yPosition : @"#C4CCCF"]];
                 }
             }

         }
         //UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.25 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.40 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.55 : @"#C4CCCF"]];
         //[imageA addSubview:myLabelS3];

         myLabelS1 = [self constructLabelT:@"SPOTTED":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"(choose the one you want to connect)":0.55:0.76:@"#C4CCCF":10] ;
         myLabelS3 = [self constructLabelT:@"devices":0.30:0.76: colorHex:25];

     }else{

         myLabelS1 = [self constructLabelT:@"SCANNING":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"devices":0.51:0.76:@"#C4CCCF":25] ;
         myLabelS3 = [self constructLabelT:@"for":0.30:0.76: colorHex:25];
     }

    [imageA addSubview:myLabelQ];
    [imageA addSubview:myLabelBack];

    [imageA addSubview:myLabelS1];
    [imageA addSubview:myLabelS2];

    [imageA addSubview:myLabelS3];
    [_container addSubview:imageA];

    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];

}

每次調用setTapDemo ::: ,都將創建一個新的_container視圖,並將其添加到self.view 因為您從未在超級視圖中刪除舊視圖,然后再對其進行初始化,所以self.view包含越來越多的子視圖(通過計時器重復),這些子視圖最終會消耗所有內存,然后您的應用程序將崩潰。

此外,每次在觸發計時器時都會調用[self.view sendSubviewToBack:_container] ,並且您永遠不會刪除舊的_container ,因此任何新的_container都將被隱藏在后面。

總之,我想您在[ _ble.scannedPeripheral count ]更改時確實創建了更新的_container ,但它仍位於其他子視圖的后面。 因此,您可以嘗試修改如下代碼:

- (void) setTapDemo: (UIImage *) cover  : (NSString *) title : (NSString *) colorHex{

    // remove any old _container view
    if (_container) [_container removeFromSuperView];    

    image = [UIImage imageNamed:@"shaded_cal.png"];
    imageA = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _container = [[UIView alloc] initWithFrame:[self.view bounds]];
    [imageA setImage:cover];
    imageA.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [imageA addGestureRecognizer:myGesture];
    [imageA setContentMode:UIViewContentModeScaleAspectFill];

    myLabelQ = [self constructLabelT:title:0.27:0.08: colorHex:25];
    myLabelBack =[self constructLabelT:@"BACK":0.04:0.01:@"#C4CCCF":18] ;
    if( bleCount > 0){

         for(NSUInteger  i = 0 ; i < [ _ble.scannedPeripheral count ] ; i ++){
            DevicePeriperal *device;
            NSString *uuid = [_ble.scannedPeripheralKey objectAtIndex:i];
             NSLog (@"device uuid = %@",  uuid);
             if (uuid)
             {
                 device = [_ble.scannedPeripheral objectForKey:uuid];
                 NSData * ssx = device.advertdata ;
                 device.rowIndex = i;
                 NSLog (@"device advert = %@",  ssx);
                 if([ssx length] > 0){
                     NSData *macD = [ssx subdataWithRange:NSMakeRange(0, 6)];
                     NSData *pairD = [ssx subdataWithRange:NSMakeRange(6, 1)];
                     NSString* newStr =  [self hexRepresentationWithSpaces:pairD : NO];
                     NSString* newMAC =  [self hexRepresentationWithSpaces:macD : YES];
                     NSLog (@"newStr = %@", newStr );
                     NSLog (@"newMAC = %@", newMAC );
                     _checkSumByte = [self calculateChecksum:newMAC];
                 }

                 NSLog (@"device = %@", device.uuid);
                 if (device )
                 {
                     UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
                     float change =  0.15*i;
                     float yPosition = 0.25 + change ;
                     [imageA addSubview:[self deviceGet:dImage:device.deviceName: 0.40 : yPosition : @"#C4CCCF"]];
                 }
             }

         }
         //UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.25 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.40 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.55 : @"#C4CCCF"]];
         //[imageA addSubview:myLabelS3];

         myLabelS1 = [self constructLabelT:@"SPOTTED":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"(choose the one you want to connect)":0.55:0.76:@"#C4CCCF":10] ;
         myLabelS3 = [self constructLabelT:@"devices":0.30:0.76: colorHex:25];

     }else{

         myLabelS1 = [self constructLabelT:@"SCANNING":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"devices":0.51:0.76:@"#C4CCCF":25] ;
         myLabelS3 = [self constructLabelT:@"for":0.30:0.76: colorHex:25];
     }

    [imageA addSubview:myLabelQ];
    [imageA addSubview:myLabelBack];

    [imageA addSubview:myLabelS1];
    [imageA addSubview:myLabelS2];

    [imageA addSubview:myLabelS3];
    [_container addSubview:imageA];

    //[self.view addSubview:_container];
    //[self.view sendSubviewToBack:_container];
    // One line of code can do this trick

    [self.view insertSubview:_container atIndex:0];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM