Ios 如何在外部屏幕背景模式下更改UIImageView
当我的ios应用程序处于后台模式时,我需要使用UIImageView在外部屏幕上显示更改 我使用此代码更改UIImageViewIos 如何在外部屏幕背景模式下更改UIImageView,ios,screen,external,Ios,Screen,External,当我的ios应用程序处于后台模式时,我需要使用UIImageView在外部屏幕上显示更改 我使用此代码更改UIImageView campaingTimer = [NSTimer scheduledTimerWithTimeInterval:timeFirstAd target:self selector:@selector(changeImage) userInfo:nil repeats:NO]; 当我的应用处于活动状态,但在后台时,它会进入changeImage方法,但不会更改图片。n计
campaingTimer = [NSTimer scheduledTimerWithTimeInterval:timeFirstAd target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
当我的应用处于活动状态,但在后台时,它会进入changeImage方法,但不会更改图片。n计时器选择器不能保证在后台启动。除非您注册特定权限,例如在后台播放音乐,并且您在后台实际执行的任何操作都与您请求的权限直接相关,否则您应该在应用程序处于后台时无法执行代码的假设下工作,因为这会让你比试图找到解决办法更容易成功 在这种情况下,您似乎希望在经过这么长时间后更改图像。假设您的方法编写正确,则当应用程序位于前台时,您拥有的NSTimer将正常工作,但要处理后台,我建议您收听appDidEnterBackground和appWillEnterForeground并发布通知,请参见下面的示例代码
AppDelegate.m
================
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.currentTime = [NSDate date];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationNameForBecameActive object:nil userInfo:@{kUserInfoForBecameActive: self.currentTime}];
}
================
ViewController.m
================
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive:) name:kNotificationNameForBecameActive object:nil];
}
- (void)didBecomeActive:(NSNotification *)notification
{
NSDate *sleepDate = notification.userInfo[kUserInfoForBecameActive];
NSTimeInterval secondsPassed = [[NSDate date] timeIntervalSinceDate:sleepDate];
if (secondsPassed >= timeFirstAd)
{
[self changeImage];
}
// reinitialize NSTimer
}
================
或者,您可以发布appDidEnterBackground和appWillEnterForeground的通知,并在那里节省时间,同时使NSTimer无效并重新启动