2009-11-03 38 views
6

您是否有AppStore Price - > Buy按鈕的UIButton動畫的示例代碼。我嘗試了很多,但它在CAAnimationGroup(scale + translation)方面效果並不好,並且只是將它設置爲UIView beginAnimations中的新幀大小而無法使用。該動畫首先設置新的寬度(立即),然後新的起源..所以它不是正確的效果...UIButton AppStore購買按鈕動畫

謝謝!

回答

5

您是否正在Core Animation塊內進行更改?

我創建了一個簡單的基於視圖的iPhone應用程序來測試它。該視圖包括兩個圓角的矩形的UIButton對象:

  1. 所述第一按鈕與寬度62(和高度35)右上角和初始標題「$ 0.99」。它連接到視圖控制器中的「按鈕」插座和「動畫」動作。這是一個按鈕,它會被點擊時動畫。

  2. 第二個按鈕位於屏幕底部,標題爲「重置」,並連接到我的視圖控制器中的「重置」動作。

這裏是視圖控制器的代碼:

UIButtonAnimationTestViewController.h:

#import <UIKit/UIKit.h> 

@interface UIButtonAnimationTestViewController : UIViewController { 
    IBOutlet UIButton *button; 
    CGRect originalFrame; 
} 

- (IBAction)animate; 
- (IBAction)reset; 

@end 

UIButtonAnimationTestViewController.m:

#import "UIButtonAnimationTestViewController.h" 

@implementation UIButtonAnimationTestViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    originalFrame = button.frame; 
} 

- (IBAction)animate { 
    CGRect frame = button.frame; 
    frame.origin.x -= 30; 
    frame.size.width += 30; 

    [UIView beginAnimations:@"button" context:nil]; 
    button.frame = frame; 
    [button setTitle:@"" forState:UIControlStateNormal]; 
    [UIView setAnimationDelegate:self]; 
    [UIView commitAnimations]; 
} 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
    [button setTitle:@"BUY NOW" forState:UIControlStateNormal]; 
} 

- (IBAction)reset { 
    button.frame = originalFrame; 
    [button setTitle:@"$0.99" forState:UIControlStateNormal]; 
} 
+0

它不會爲我創造正確的效果。寬度首先被設置,所以Button隨後移動到新的位置。在AppStore中,右角的Right-X-Position總是保持在他們的位置,*同時像一個縮放和翻譯。 – erazorx 2009-11-04 07:38:37

+0

我的代碼像AppStore一樣工作:按鈕的右側不移動。由於我們將兩個更改(平移左移和增加寬度)都設置爲框架的副本,然後更改動畫塊內的按鈕框架,因此兩個更改都必須同時進行動畫製作。 – gerry3 2009-11-04 08:37:06

+0

完美的作品,我的朋友。 – iwasrobbed 2010-08-12 00:51:51