从复杂的C方法调用Objective-C方法?

时间:2016-09-27 15:49:24

标签: ios objective-c

我已经设置了一组方法来调暗和照亮设备的屏幕,并在设备充电/不充电时在视图上引入黑色UIView。

我遇到的挑战是从用于检查设备是否正在充电的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];
}

1 个答案:

答案 0 :(得分:2)

par('cin')