2
私はデバイスの画面を暗く明るくする方法と、デバイスが充電中/充電中でないときに、ビュー上に黒のUIViewを導入する方法のグループを設定しました。複合CメソッドからObjective-Cメソッドを呼び出しますか?
私が抱えている課題は、デバイスが充電中かどうかをチェックするために使用されるCメソッドからObj Cメソッド(brightenScreenとdimScreen)を呼び出すことです。
ご了承ください。
-(void)setUpChargingCheck{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
// Do whatever you want...
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged){
[self brightenScreen];
}else{
[self dimScreen];
}
// Registering for a specific notification
NSString *notificationName = @"com.apple.springboard.fullycharged";
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
yourCustomMethod,
(__bridge CFStringRef)notificationName,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
// The custom method that will receive the notification
static void yourCustomMethod(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString *nameOfNotification = (__bridge NSString*)name;
if([nameOfNotification isEqualToString:@"com.apple.springboard.fullycharged"])
{
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged){
[[UIScreen mainScreen] setBrightness:1.0];
// brighten screen here
}else{
[[UIScreen mainScreen] setBrightness:0.0];
// dim screen here
}
}
}
-(void)dimScreen{
[[UIScreen mainScreen] setBrightness:1.0];
[self.blackoutView removeFromSuperview];
}
-(void)brightenScreen{
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
[[UIScreen mainScreen] setBrightness:0.0];
self.blackoutView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[self.blackoutView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:self.blackoutView];
}
このコードはでは何クラスですか? 'CFNotificationCenterAddObserver'の代わりに' NSNotificationCenter'を使わないのはなぜですか?その後、問題はすべて消え去る。 – rmaddy