如何实现自定义segue动画?

时间:2014-09-24 11:32:29

标签: ios objective-c storyboard uistoryboardsegue

我将UIStoryboardSegue子类化以执行自定义segue动画。要使动画可见,我必须将目标ViewController中的视图添加为临时视图。执行动画后,我将删除临时视图并显示最终ViewController via( presentViewController:animated:NO )。

然而,这会导致以下警告:“对的开始/结束外观转换的不平衡调用。

如果我保留我的临时视图并且不删除它,则不会出现警告,但最终视图不再响应触摸事件。如何摆脱这种警告?

我的自定义代码:

-(void)perform {

    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;

    destinationViewController.view.alpha = 0.;
    destinationViewController.view.transform = CGAffineTransformScale(destinationViewController.view.transform, 4.0, 4.0);

    [sourceViewController.view addSubview:destinationViewController.view];

    [UIView animateWithDuration:kSSSpriteKitFadeOutDuration
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{

                         destinationViewController.view.transform = CGAffineTransformScale(destinationViewController.view.transform, 0.25, 0.25);
                         destinationViewController.view.alpha = 1.;
                     }
                     completion:^(BOOL finished){
                         [destinationViewController.view removeFromSuperview]; // remove from temp super view
                         [sourceViewController presentViewController:destinationViewController animated:NO completion:NULL]; // present VC
                     }];
}

1 个答案:

答案 0 :(得分:0)

我正在参加4 UIStoryboardSegue类型课程。 2用于动画开始,其余2用于动画停止。所以我将分享4个类的所有代码。请尝试解决这个问题。

<强> CustomSegue.h

 #import <UIKit/UIKit.h>
 @interface CustomSegue : UIStoryboardSegue

 // Originating point for animation
 @property CGPoint originatingPoint;
 @property CGRect Startingframe;
 @end

<强> CustomSegue.m

 #import "CustomSegue.h"
 @implementation CustomSegue

 - (void)perform
 {
   UIViewController *sourceViewController = self.sourceViewController;
   UIViewController *destinationViewController = self.destinationViewController;

   // Add the destination view as a subview, temporarily
   [sourceViewController.view addSubview:destinationViewController.view];

   // Transformation start scale
   float width = 320;
   float height = 480;

   float x_Scale = self.Startingframe.size.width/width;
   float Y_Scale = self.Startingframe.size.height/height;

   destinationViewController.view.transform = CGAffineTransformMakeScale(x_Scale, Y_Scale);

   // Store original centre point of the destination view
   CGPoint originalCenter = destinationViewController.view.center;
   // Set center to start point of the button
   destinationViewController.view.center = self.originatingPoint;

   [UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^
                {
                     // Grow!
                      destinationViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);

                     destinationViewController.view.center = originalCenter;
                 }
                 completion:^(BOOL finished)
                {
                     [destinationViewController.view removeFromSuperview]; // remove from temp super view
                     [sourceViewController presentViewController:destinationViewController animated:NO completion:NULL]; // present VC
                 }];
 }

 @end

<强> CustomUnwindSegue.h

 #import <UIKit/UIKit.h>
 @interface CustomUnwindSegue : UIStoryboardSegue

 // Target point to animate to (on the button)
 @property CGPoint targetPoint;
 @property CGRect endFrame;

 @end

<强> CustomUnwindSegue.m

 #import "CustomUnwindSegue.h"

 @implementation CustomUnwindSegue

 - (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;

    // Transformation End scale
    float width = 320;
    float height = 480;

    float x_Scale = self.endFrame.size.width/width;
    float Y_Scale = self.endFrame.size.height/height;
    // Add view to super view temporarily
    [sourceViewController.view.superview insertSubview:destinationViewController.view atIndex:0];

    [UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     // Shrink!
                     sourceViewController.view.transform = CGAffineTransformMakeScale(x_Scale, Y_Scale);
                     sourceViewController.view.center = self.targetPoint;
                 }
                 completion:^(BOOL finished){
                     [destinationViewController.view removeFromSuperview]; // remove from temp super view
                     [sourceViewController dismissViewControllerAnimated:NO completion:NULL]; // dismiss VC
                 }];
}

@end

只需将您的操作与此自定义segue动画相关联,如下所示:

enter image description here