从superview中删除并启用用户交互

时间:2012-06-12 11:59:56

标签: objective-c ios cocoa-touch uiviewanimation

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

这是我的代码:

-(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”:( 在动画期间,按钮继续是交互式的

5 个答案:

答案 0 :(得分:2)

您知道beginIgnoringInteractionEventsendIgnoringInteractionEvents吗? 通常,如果您在动画期间不想要任何userInteraction,则可以使用这些。

无论如何,你仍然需要正确的回调才能触发它们。

答案 1 :(得分:1)

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

-(void)remove:(id)sender {
   UIView *v = (UIView*)sender;
   [v removeFromSuperview];
}

答案 2 :(得分: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}]; }];

希望有所帮助。

答案 3 :(得分:0)

我认为您可以使用它来启用用户互动 self.view.userInteractionEnabled = NO;

答案 4 :(得分: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概念,它就像一个魅力!

谢谢你们!