如何在应用程序进入前景

时间:2015-12-16 03:21:15

标签: ios user-interface animation

我知道Apple不推荐在后台使用App时更新UI,特别是对于OpenGL。

然而,我刚刚意识到iMessenger和Facebook的使者看起来能够做到这一点。与您的朋友一起输入消息主题,然后转到后台,然后在应用程序仍在后台时收到新消息,然后将此应用程序带到前台(单击推送通知或应用程序图标),您会发现新消息气泡是已经有了图标中的应用扩展动画。

根据我的理解,这只会发生,因为新的消息气泡已经在后台模式下绘制。然后当app进入前景时,它可以出现在动画中。

但是从我的测试结果来看,在iOS8和iOS9中,所有后台UI更新都会在应用程序生效后推迟。此外,iOS将为该UI更新添加隐式动画事务。

我在下面列出了我的测试代码,当应用程序进入前台时,你会看到新的单元格被添加到具有明显动画事务的表中,完全不同于iMessenger。 tableView:numberOfRowsInSection:仅在app进入前台时执行延迟。

不仅对于tableview单元更新,即使在后台添加子视图也会触发类似的延迟事务以进入前台。

也许我对这个方向完全错误。谁能帮助我了解iMessenger和FB的使者如何能够达到这种效果? 提前致谢!

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *dataTable;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:self.tableView];
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  [self.tableView registerClass:[UITableViewCell class]
         forCellReuseIdentifier:@"kCellId"];

  self.dataTable = [[NSMutableArray alloc] initWithArray:@[@"1", @"2", @"3"]];

  __weak __typeof(self) weakSelf = self;
  [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
                                                    object:nil
                                                     queue:[NSOperationQueue mainQueue]
                                                usingBlock:^(NSNotification * _Nonnull note) {
                                                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
                                                                 dispatch_get_main_queue(), ^{
                                                    [weakSelf.dataTable addObject:[@(weakSelf.dataTable.count + 1) stringValue]];
                                                    [weakSelf.tableView reloadData];
                                                  });
                                                }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.dataTable.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellId"];
  cell.textLabel.text = self.dataTable[indexPath.row];
  return cell;
}

@end

2 个答案:

答案 0 :(得分:1)

我建议您查看APN payload documentation launch-mage

launch-image - 应用包中图片文件的文件名;它可能包括扩展名或省略它。当用户点击操作按钮或移动动作滑块时,图像将用作启动图像。如果未指定此属性,系统将使用上一个快照,使用应用程序的Info.plist文件中UILaunchImageFile键标识的图像,或者回退到Default.png。 此属性已在iOS 4.0中添加。

答案 1 :(得分:1)

后台获取允许系统在后台唤醒应用程序

更多细节 enter link description here