gpt4 book ai didi

扫描蓝牙设备时,iOS 背景 View 未使用 NSTimer 更新

转载 作者:行者123 更新时间:2023-11-29 02:28:45 24 4
gpt4 key购买 nike

我正在尝试获取蓝牙扫描页面以使用蓝牙配对 BLE 设备。我发现 NSUInteger [ _ble.scannedPeripheral count ] 在扫描时发生变化。然而,当执行时,背景 View 图像和页面甚至无法改变。如果显示可用 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 View ,并将其添加到 self.view 中。因为在再次初始化之前您永远不会从 super View 中删除旧 View ,self.view 包含越来越多的计时器重复 subview ,这最终会消耗所有内存然后您的应用程序将崩溃。

此外,每次触发计时器时都会调用[self.view sendSubviewToBack:_container],并且您永远不会删除旧的_container,因此任何新的 _container 将因此隐藏在后面。

总而言之,我猜您确实创建了更新的 _container[ _ble.scannedPeripheral count ] 已更改,但它落后于其他 subview 。所以你可以尝试像这样修改代码:

- (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];
}

关于扫描蓝牙设备时,iOS 背景 View 未使用 NSTimer 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27170385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com