1

我正在做一些自定义动画以通过一种方法更改视图。我已经从 superView 中删除了“fromView” [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)],但我还想在动画结束后启用用户交互。

这是我的代码:

-(void) switchFrom:(UIViewController*) fromViewController To:(UIViewController*) toViewController usingAnimation:(int) animation{
    UIView *fromView = fromViewController.view;
    UIView *toView = toViewController.view;
    /*************** SET ALL DEFAULT TRANSITION SETTINGS ***************/
    // Get the current view frame, width and height
    CGRect pageFrame = fromView.frame;
    CGFloat pageWidth = pageFrame.size.width;
    
    // Create the animation
    [UIView beginAnimations:nil context:nil];
    
    // Create the delegate, so the "fromView" is removed after the transition
    [UIView setAnimationDelegate: fromView];
    [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
    
    // Set the transition duration
    [UIView setAnimationDuration: 0.4];

    /*************** IT DOESN'T WORK AT ALL ***************/
    [toView setUserInteractionEnabled:NO];
    [UIView setAnimationDelegate: toView];
    [UIView setAnimationDidStopSelector:@selector(setUserInteractionEnabled:)];
    [toView setUserInteractionEnabled:YES];

    // Add the "toView" as subview of "fromView" superview
    [fromView.superview addSubview:toView];
    switch (animation) {
        case AnimationPushFromRigh:{
            // Position the "toView" to the right corner of the page            
            toView.frame = CGRectOffset(pageFrame, pageWidth,0);
            
            // Animate the "fromView" to the left corner of the page
            fromView.frame = CGRectOffset(pageFrame, -pageWidth,0);
            
            // Animate the "toView" to the center of the page
            toView.frame = pageFrame;
            
            // Animate the "fromView" alpha
            fromView.alpha = 0;
            
            // Set and animate the "toView" alpha
            toView.alpha = 0;
            toView.alpha = 1;

            // Commit the animation
            [UIView commitAnimations];
        }
        .
        .
        .

知道如何调用这两种方法setAnimationDidStopSelector并实际使它们一起工作吗?

编辑 1

尝试了这样的 @safecase 代码,替换了这个注释块:

/*************** IT DOESN'T WORK AT ALL ***************
[toView setUserInteractionEnabled:NO];
[UIView setAnimationDelegate: toView];
[UIView setAnimationDidStopSelector:@selector(setUserInteractionEnabled:)];
[toView setUserInteractionEnabled:YES];
*************** IT DOESN'T WORK AT ALL ***************/
// Remove the interaction
[toView setUserInteractionEnabled:NO];
[fromView setUserInteractionEnabled:NO];
// Create the animation
[UIView animateWithDuration:0.4 animations:^{
    [fromView performSelector:@selector(removeFromSuperview)];
    }
    completion:^(BOOL finished){
        [UIView animateWithDuration:0.4 
        animations:^{ 
            C6Log(@"finished");
            [toView performSelector: @selector(setUserInteractionEnabled:)];
        }];
    }];

删除了“toView”而不是删除了“fromView”:(在动画期间按钮继续交互

4

5 回答 5

2

你知道beginIgnoringInteractionEventsendIgnoringInteractionEvents?通常,如果您在动画期间不想要任何 userInteraction,您会使用它们。

无论如何,您仍然需要正确的回调来触发它们。

于 2012-06-14T01:47:25.947 回答
1

您需要定义自己的方法,例如remove:并将其作为选择器传递。在方法中,只需使用传递UIView的来删除它。

-(void)remove:(id)sender {
   UIView *v = (UIView*)sender;
   [v removeFromSuperview];
}
于 2012-06-12T12:07:52.517 回答
1

你可以使用这个:

 [UIView animateWithDuration:0.4

 animations:^{ [self performSelector:@selector(removeFromSuperview)]; // other code here}

 completion:^(BOOL finished){ [UIView animateWithDuration:0.4

 animations:^{ [self performSelector:@selector(setUserInteractionEnabled:)]; //other code here}]; }];

希望有帮助。

于 2012-06-13T05:39:13.180 回答
0

我想你可以用来启用用户交互
self.view.userInteractionEnabled=NO;

于 2012-06-13T05:48:20.387 回答
0

我最终为 UIView 创建了一个类别扩展……它终于可以工作了!

UIView+Animated.h 代码

#import <UIKit/UIKit.h>

@interface UIView (Animated)

- (void) finishedAnimation;

@end

UIView+Animated.m 代码

#import "UIView+Animated.h"

@implementation UIView (Animated)

- (void) finishedAnimation{
    C6Log(@"FinishedAnimation!");
    [self removeFromSuperview];
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

@end

然后,我在我的协调控制器中 #imported 这个扩展:

C6CoordinatingController.h 部分代码

#import "UIView+Animated.h"

并在 setAnimationDidStopSelector 中调用选择器:

C6CoordinatingController.m 部分代码

// Create the delegate, so the "fromView" is removed after the transition
[UIView setAnimationDelegate: fromView];

// Ignore interaction events during the animation
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

// Set the transition duration
[UIView setAnimationDuration: 0.4];

// Call UIView+Animated "finishedAnimation" method when animation stops
[UIView setAnimationDidStopSelector:@selector(finishedAnimation)];

所以,我最终使用了@jaydee3 和@Mundi 概念,它就像一个魅力!

谢谢你们!

于 2012-06-14T10:37:45.500 回答