2012-06-12 77 views
1

我正在做一些自定義動畫以在單一方法中更改視圖。我已經使用[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這兩種方法,實際上使他們一起工作?

EDIT 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

@Till,實際上,有一個「mainView」superview ...我可以嘗試這樣的... –

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

沒有...沒有工作... 代碼 –

+0

不知何故,這段代碼不起作用......但它似乎真的在正確的道路......我會用它做一些測試,謝謝! –

0

我想你可以用來啓用用戶交互
self.view.userInteractionEnabled = NO;

+0

我這樣做了...但我需要它在動畫後重新啓用。 –

2

你知道嗎beginIgnoringInteractionEventsendIgnoringInteractionEvents? 通常,如果您在動畫期間不需要任何userInteraction,則可以使用這些。

無論如何,你仍然需要正確的回調來觸發它們。

+0

不...我會看看如何使用它來代替「makeInteractionEnabled」。 –

+0

它也很好......但我仍然不能同時執行「從視圖中刪除」和「endIgnoringInteractionEvents」... 我會嘗試創建一個方法(如@Mundi建議)刪除「fromView」並調用「endIgnoringInteractionEvents」。 –

相關問題