ios - 从复杂的 C 方法调用 Objective-C 方法?
<p><p>我设置了一组方法来使设备的屏幕变暗和变亮,并在设备正在充电/不充电时在 View 上引入黑色 UIView。</p>
<p>我面临的挑战是从用于检查设备是否正在充电的 C 方法调用我的 Obj C 方法(brightenScreen 和 dimScreen)。</p>
<p>任何输入/帮助将不胜感激!</p>
<pre><code>-(void)setUpChargingCheck{
[ setBatteryMonitoringEnabled:YES];
// Do whatever you want...
if ([ batteryState] != UIDeviceBatteryStateUnplugged){
;
}else{
;
}
// 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()
{
if ([ batteryState] != UIDeviceBatteryStateUnplugged){
[ setBrightness:1.0];
// brighten screen here
}else{
[ setBrightness:0.0];
// dim screen here
}
}
}
-(void)dimScreen{
[ setBrightness:1.0];
;
}
-(void)brightenScreen{
CGFloat width = .bounds.size.width;
CGFloat height = .bounds.size.height;
[ setBrightness:0.0];
self.blackoutView=[initWithFrame:CGRectMake(0, 0, width, height)];
];
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code> YourClass *yourClassObject = (__bridge YourClass *)(observer);
;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从复杂的 C 方法调用 Objective-C 方法?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39729082/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39729082/
</a>
</p>
页:
[1]