2012-05-28 90 views
3

我是新來的編碼。我正在製作一個應用程序,當點擊一個按鈕時,我需要使視圖顯示出來,並且視圖應該顯示爲來自按鈕本身。再次點擊按鈕時,視圖應該返回到按鈕(動畫)。單擊按鈕時如何通過動畫顯示視圖?

我有像翻轉,捲曲的動畫,但我不知道如何做到這一點。

回答

-2

//這裏animationButton是按鈕的名稱 //這裏aView是一個視圖

aView.view.center = animationButton.center;

現在,如圖所示將視圖縮小到一個小的比例,以便當它圍起來時,它會顯示爲好像它是來自按鈕本身。

CGAffineTransform trans = CGAffineTransformScale(aView.view.transform, 0.01, 0.01); 

aView.view.transform = trans; // do it instantly, no animation 

[self.view addSubview:aView.view]; 

// now return the view to normal dimension, animating this transformation 

//現在用動畫的幫助下,通過動畫縮放視圖一定很大程度上

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseInOut 
       animations:^{ 
            aView.view.transform = CGAffineTransformScale(aView.view.transform, 70.0, 70.0); 
       } 
       completion:nil]; 
6

這是一個簡單的例子。設置showView:作爲按鈕的動作。

- (IBAction)showView:(UIButton *)sender { 
    // Create a view with the size and position of the tapped button 
    UIView *view = [[UIView alloc] initWithFrame:sender.frame]; 
    // Set a color on the view 
    view.backgroundColor = [UIColor redColor]; 
    // Add the new view to the main view. The new view will be displayed over any other views 
    [self.view addSubview:view]; 
    // Animate the change of the new view's frame. We use the bounds of the main view. 
    [UIView animateWithDuration:3.6 animations:^{ 
     view.frame = self.view.bounds; 
    }]; 
} 

完整的解決方案:

首先產生用於視圖和按鈕的屬性。你如何初始化這些取決於你。

@property (strong, nonatomic) UIButton *button; 
@property (strong, nonatomic) UIView *aView; 
... 
@synthesize button = _button; 
@synthesize aView = _aView; 

然後,創建動畫兩幀之間的圖,並且如果要求將在動畫結束除去從它的父視圖的方法。

- (void)animateView:(UIView *)view 
      fromRect:(CGRect)from 
      toRect:(CGRect)to 
     inParentView:(UIView *)parent 
    removeWhenDone:(BOOL)remove 
{ 
    if (!remove) { 
     [parent addSubview:view]; 
    } 
    view.frame = from; 
    [UIView animateWithDuration:3.6 animations:^{ 
     view.frame = to; 
    } completion:^(BOOL finished) { 
     if (remove) { 
      [view removeFromSuperview]; 
     } 
    }]; 
} 

然後創建一個布爾屬性,指示是否顯示的視圖,並實現該屬性的定製設定器。

@property (assign, nonatomic) BOOL viewShown; 
... 
@synthesize viewShown = _viewShown; 

- (void)setViewShown:(BOOL)viewShown 
{ 
    _viewShown = viewShown; 
    if (_viewShown) { 
     // Insert your own toRect 
     [self animateView:self.aView fromRect:self.button.frame toRect:CGRectMake(0, 0, 100, 100) inParentView:self.view removeWhenDone:NO]; 
    } else { 
     [self animateView:self.aView fromRect:self.aView.frame toRect:self.button.frame inParentView:self.view removeWhenDone:YES]; 
    } 
} 

最後,在按鈕的動作中,您翻轉了viewShown屬性。

- (IBAction)showView:(UIButton *)sender { 
    self.viewShown = !self.viewShown; 
} 
+0

Thanks..it是有益的......請看到我的編輯的問題.. –

+0

請參閱我編輯的答案:) –

相關問題