我正在尝试在 iOS 中制作自定义展开 View Controller 动画。
这是一个 iPad 应用程序,在主屏幕上有一个 160*160 大小的大方形按钮。我已将模态转场连接到下一个 View Controller 场景。我想要实现的是,当我点击 UIButton 时,目标 viewcontroller 应该从按钮扩展,并且应该在动画完成时动画到全屏。
我将 NSObject 子类化并创建了 ExpandAnimationController,实现了 transitionDuration 和 animateTransition 方法。但是我在确定目标 View Controller View 的初始框架时遇到了困难。
我知道对于一个按钮我可以静态设置框架但是在一个场景中出现有多个按钮并且每个按钮都必须以模式转场到不同的 View Controller 而不是如何将按钮框架传递给动画 Controller 以设置初始 View 框架。
在尝试了 raywanderlich 的自定义 View Controller 转换教程后,我这样做只是为了学习目的。所以现在的问题是将一个框架对象从“源 Controller 传递到动画 Controller ”。我已经搜索过,互联网上并没有太多自定义转换问题。任何帮助将不胜感激。
这是我在 ExpandAnimationController 中尝试过的代码,
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
// 1. Fetch all the required objects.
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *containerView = [transitionContext containerView];
CGRect finalFrame = toVC.view.frame;
// 2. Set initial frames.
// This is where I am stuck.
CGRect initialFrame = CGRectZero;
toVC.view.frame = initialFrame;
[containerView addSubview:toVC.view];
// 3. Do the animation.
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration animations:^{
fromVC.view.alpha = 0.5;
toVC.view.alpha = 0.5;
toVC.view.frame = finalFrame;
} completion:^(BOOL finished) {
fromVC.view.alpha = 1.0;
toVC.view.alpha = 1.0;
toVC.view.frame = finalFrame;
[transitionContext completeTransition:YES];
}];
}
在源 Controller 中,我采用了 UIViewControllerTransitioningDelegate
,创建了一个 ExpandAnimationController 实例并像下面这样使用,
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
return _expandAnimationController;
}
但是在调用 prepareForSegue
之后,我想将发送器按钮框架传递给过渡动画 Controller 。
提前致谢,编码愉快!!
最佳答案
关于如何执行此操作的方法有很多,例如可以将 iVar 传递给您的过渡或过渡委托(delegate),您可以将其传递给按钮矩形,但我将建议一种更简单的方法(尽管有一个警告):
首先为您的 UIButton
分配一个唯一标签通过代码或在 Interface Builder 中。
在这个例子中,我使用了 111 标签:
在你的- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
您可以从 ViewController 中获取对该按钮的引用:
CGRect myButtonRect = CGRectZero;
UIButton *myButton = (UIButton*)[fromVC.view viewWithTag:111];
if (myButton) {
myButtonRect = myButton.frame;
NSLog(@"button Frame:%@",NSStringFromCGRect(myButtonRect));
}
有什么注意事项? 如果您使用相同的动画向后过渡,则 From 和 To View Controller 将颠倒过来。
此外,如果您在另一个 View Controller 上使用相同的 Transition,并且该 View Controller 对另一个对象具有相同的标签号,您将获得该对象而不是您的 Button。
因此,如果您使用这种方法,请确保为您的整个应用程序使用全局唯一标签,例如 32565(这里的随机数足够大,因此您不会再次使用它)
现在如果你有多个按钮,你不能使用上面的方法,所以使用一个 iVar 到你的自定义转换:
@interface MyCustomTransition : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) CGRect initialFrame;
@end
然后在 View Controller 中使用自定义过渡,只需传入框架即可:
MyCustomTransition *myTransition = [MyCustomTransition new];
myTransition.initialFrame = myButton.frame;
最后在您的 - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
中从 self.initialFrame
获取 iVar 值
关于ios - 如何将 CGRect 帧值传递给自定义过渡动画 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710314/