UIViewController动画iOS?

4

尝试在Objective-C中创建类似于Swift中的下列代码。我尝试使用桥接等方法,将此代码用于我的Objective C项目,但出现了导入问题。

在Objective-C中,我尝试了以下操作,并在UIView上部分成功,但我猜想我需要添加转换来完整UIViewController。

enter image description here

我尝试的操作:

- (IBAction)menuButtonPressed:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(menuButtonPressed)]) {
        [self.delegate menuButtonPressed];
    }
    if (!isVertical) {
        [UIView animateWithDuration: 0.6
                              delay: 0.0
             usingSpringWithDamping: 0.3
              initialSpringVelocity: .8
                            options: 0
                         animations: ^
         {
             self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
             self.transform = CGAffineTransformRotate(self.transform, 90 * M_PI / 180);
             self.transform = CGAffineTransformTranslate(self.transform, 240, 0);
         }
                         completion:^(BOOL finished) {
                             isVertical = YES;

                         }
         ];

    }else{
        [UIView animateWithDuration: 0.45
                              delay: 0.0
             usingSpringWithDamping: 0.44
              initialSpringVelocity: .8
                            options: 0
                         animations: ^
         {
             self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
             self.transform = CGAffineTransformRotate(self.transform, -90 * M_PI / 180);
             self.transform = CGAffineTransformTranslate(self.transform, 240, 0);

         }
                         completion:^(BOOL finished) {
                             isVertical = NO;

                         }
         ];
    }
}

任何输入都会有帮助。

你能详细说明你遇到的问题吗?当你更加详细地阐述时,很可能你自己就能找到答案了! - Arpit B Parekh
我正在尝试实现自定义的UIViewController转换,它看起来像gif中的样子。 - BhushanVU
它在GitHub上 - 只需搜索iOS断头台菜单 https://github.com/Yalantis/GuillotineMenu - Alessign
1个回答

2

这是一个非常简单的实现,您可以使用它来动画化视图控制器之间的转换。如果您想将其与UIView一起使用,基本上可以使用相同的动画要点。

let generateRandomColor: Void -> UIColor = {
    let red = CGFloat(arc4random_uniform(255)) / 255.0
    let green = CGFloat(arc4random_uniform(255)) / 255.0
    let blue = CGFloat(arc4random_uniform(255)) / 255.0
    return UIColor(red:red, green: green, blue: blue, alpha: 1)
}

class SlideAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
    let presenting: Bool

    init(presenting: Bool) {
        self.presenting = presenting
        super.init()
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()!

        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        var animatingView: UIView
        var initialTransform: CGAffineTransform
        var finalTransform: CGAffineTransform

        if presenting {
            containerView.addSubview(toView)
            animatingView = toView
            initialTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
            finalTransform = CGAffineTransformIdentity
        } else {
            containerView.insertSubview(toView, belowSubview: fromView)
            animatingView = fromView
            initialTransform = CGAffineTransformIdentity
            finalTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
        }

        animatingView.layer.anchorPoint = CGPointMake(0, 0)
        animatingView.frame = containerView.bounds
        animatingView.transform = initialTransform

        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 16, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            animatingView.transform = finalTransform
            }) {
            _ in
                toView.layer.anchorPoint = CGPointMake(0.5, 0.5)
                toView.frame = containerView.bounds
                transitionContext.completeTransition(true)
        }
    }

}

class PresentedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = generateRandomColor()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self,
            action: "tapped")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapped() {
        dismissViewControllerAnimated(true, completion: nil)
    }

}

class TestViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = generateRandomColor()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self,
            action: "tapped")
        definesPresentationContext = true
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapped() {
        let presentedViewController = PresentedViewController()
        presentedViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
        presentedViewController.transitioningDelegate = self
        presentViewController(presentedViewController, animated: true, completion: nil)
    }

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SlideAnimationController(presenting: true)
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SlideAnimationController(presenting: false)
    }

}

谢谢,如果是Objective-C的话会更有帮助。 - BhushanVU
请查看此示例项目,以获取有关在Objective-C中进行转换的更多详细信息 https://github.com/ariok/TB_CustomTransition - Pauls
@Pauls:谢谢,我会尝试这个。 - BhushanVU

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接