2012-06-12 2 views
1

Я делаю некоторые пользовательские анимации для изменения представлений одним методом. Я уже удалил «fromView» из superView, используя [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" удаляется :( Кнопки продолжают быть интерактивным во время анимации

ответ

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 концепции, и она работает как шарм !

Спасибо, ребята!

1

Вам необходимо определить свой собственный метод, например remove:, и передать это как селектор. В этом методе используйте только принятый UIView, чтобы удалить его.

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

Но мне нужны два «отправителя» ... toView (для удаления из superView) и fromView (чтобы включить взаимодействие). Могу ли я установить два разных «setAnimationDidStopSelector»? Это будет работать? –

+0

Разве это не «из вида» оригинального супервизора? Если да, почему бы не использовать эти отношения в описанном выше методе? – Till

+0

@ Наверное, есть супервизор «mainView» ... Я мог бы попробовать что-то вроде этого ... –

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}]; }]; 

Надежда полезным.

+0

Nope ... не работал ... Код в редакции –

+0

Как-то этот код не работает ... но он действительно кажется в правильный путь ... Я сделаю с ним несколько тестов, спасибо! –

0

Я думаю, вы можете использовать для взаимодействия с пользователем
self.view.userInteractionEnabled = NO;

+0

Я сделал это ... но мне нужно, чтобы он был повторно включен после анимации. –

2

Знаете ли вы, что beginIgnoringInteractionEvents и endIgnoringInteractionEvents? Обычно, если вы не хотите никакого вмешательства пользователя во время анимации, вы должны использовать их.

В любом случае вам все еще нужны правильные обратные вызовы для их запуска.

+0

Нет ... Я проверю, как использовать это вместо «makeInteractionEnabled». –

+0

Он также отлично работает ... но я все еще не могу выполнять оба действия «remove fromView» и «endIgnoringInteractionEvents» вместе ... Я попробую создать метод (например, предложенный Microsoft), удалить «fromView» и вызвать «endIgnoringInteractionEvents». –

Смежные вопросы