15

我正在为带有自定义 UI 的 iOS 8 开发共享扩展,但它没有动画显示,我该怎么做?这是一个常规的 UIViewController。

此外,它出现在 iPad 上的全屏上,我希望它是一个模态视图控制器,它出现在屏幕中央并且不适合它,我该怎么做?

问候。

4

2 回答 2

27

这是迄今为止我发现的最简洁的解决方案,可以让我的自定义视图控制器进出动画!

动画输入:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.view.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
    [UIView animateWithDuration:0.25 animations:^
    {
        self.view.transform = CGAffineTransformIdentity;
    }];
}

解雇:

- (void)dismiss
{
    [UIView animateWithDuration:0.20 animations:^
    {
        self.view.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
    } 
    completion:^(BOOL finished) 
    {
        [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
    }];
}
于 2014-09-19T08:29:40.047 回答
2

我建议采用不同的方法,而不是为UIViewController's设置动画。view

我创建了一个设置UIViewController为. 然后,我在顶部呈现预期的自定义模态(或自定义动画,如果你喜欢)。PresentingViewControllerview.backgroundColor[UIColor clearColor]UIViewController

这是代码PresentingViewController

@implementation PresentingViewController

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self performSegueWithIdentifier:@"PresentController" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"PresentController"]) {
    CustomViewController *controller = (CustomViewController *)[segue.destinationViewController topViewController];
    controller.context = self.extensionContext;
  }
}

- (IBAction)unwindFromShareVC:(UIStoryboardSegue *)segue {
  [self dismissViewControllerAnimated:YES completion:^{
    NSError *error = [NSError errorWithDomain:@"Cancelled" code:0 userInfo:nil];
    [self.extensionContext cancelRequestWithError:error];
  }];
}

@end

笔记:

  1. extensionContext仅在 上设置PresentingViewController,因此需要将其传递给CustomViewController.
  2. 对于动画解雇,我无法使用展开转场,因为很难知道解雇的完成。所以我改用了dismissViewControllerAnimated:completion :。
于 2016-02-26T17:51:01.150 回答