iOS 8 CATransition - 屏幕外的幻灯片视图

iOS 8 CATransition - slide view off screen

我正在尝试将视图滑到屏幕上,并在用户点击取消按钮时将其滑出屏幕。使用以下代码,滑动到屏幕上效果很好。

CATransition *trans = [CATransition animation];
trans.duration = 0.2;
trans.type = kCATransitionMoveIn;
trans.subtype = kCATransitionFromLeft;

[viewToAnimate.layer addAnimation:trans forKey:nil];

[self.view addSubView:viewToAnimate];

但是,我无法找到通过 CATransition(或其他方式)让它在屏幕外显示动画的最佳方式。这是如何实现的?

我敢肯定它有点小,但我看的其他问题似乎并没有削减它。在此先感谢您的帮助。

试试这个,它实际上将屏幕的 y 位置更改为屏幕末尾,并创建与您尝试的相同的动画。

CGRect temp = self.view.frame;
temp.origin.x = [[UIScreen mainScreen] bounds].size.width ;
[UIView animateWithDuration:0.5
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     self.view.frame = temp;
                 }completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

对于 Swift,请参阅 Slide in Animation in Swift 教程。示例是从左滑入,但只需更改一行使其从右滑入。

func slideInFromLeft(duration: TimeInterval = 0.7, completionDelegate: CAAnimationDelegate? = nil) {
    let slideInFromLeftTransition = CATransition()

    if let delegate: CAAnimationDelegate = completionDelegate {
        slideInFromLeftTransition.delegate = delegate
    }

    // Customize the animation's properties
    slideInFromLeftTransition.type = kCATransitionPush
    slideInFromLeftTransition.subtype = kCATransitionFromLeft //or kCATransitionFromRight
    slideInFromLeftTransition.duration = duration
    slideInFromLeftTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    slideInFromLeftTransition.fillMode = kCAFillModeRemoved

    // Add the animation to the View's layer
    self.layer.add(slideInFromLeftTransition, forKey: "slideInFromLeftTransition")
}