2010-08-05 29 views
1

我有一個框架大小的動畫,當UIButton是UIButtonTypeRoundedRect時可以正常工作。但是當我在背景圖像中使用UIButtonStyleCustom時沒有明顯的影響。我的動畫代碼在這裏:是否可以動畫UIButtonStyleCustom類型的UIButton的寬度?

[UIView beginAnimations:@"MyAnimation" context:nil]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:0.25]; 
CGRect tempFrame = myButton.frame; 
tempFrame.size.width = tempFrame.size.width + 100.0f; 
myButton.frame = tempFrame; 
[UIView commitAnimations]; 

感謝您的幫助!

+0

也許'[UIView的beginAnimations:無背景:@ 「MyAnimation」];'真的'[ UIView beginAnimations:@「MyAnimation」context:NULL];(第一個參數是animationID,NSString *; second - context,void *)? – kpower 2010-08-05 10:10:16

+0

@kpower對於錯字,我很抱歉,但仍然無效。 – nonamelive 2010-08-05 10:54:05

+0

你是什麼意思的「不工作」?請明確點。 – kennytm 2010-08-05 11:09:58

回答

0

嘿傢伙,我終於解決了我的問題。我分類UIView並添加了兩個UIImageView s和一個UIButton它。動畫現在非常好!

下面是代碼:

.H

#import <UIKit/UIKit.h> 

@interface NNAnimatableButton : UIView { 

    UIImageView *backgroundImageView; 
    UIImageView *imageView; 
    UIButton *button; 
} 

@property (nonatomic, retain) UIImageView *backgroundImageView; 
@property (nonatomic, retain) UIImageView *imageView; 
@property (nonatomic, retain) UIButton *button; 

@end 

.M

#import "NNAnimatableButton.h" 

@implementation NNAnimatableButton 

@synthesize backgroundImageView, imageView, button; 

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 

     CGRect rect = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height); 

     NSUInteger autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin; 

     // Initialization code. 
     backgroundImageView = [[UIImageView alloc] initWithFrame:rect]; 
     backgroundImageView.autoresizingMask = autoresizingMask; 

     imageView = [[UIImageView alloc] initWithFrame:rect]; 
     imageView.autoresizingMask = autoresizingMask; 
     imageView.contentMode = UIViewContentModeCenter; 

     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.autoresizingMask = autoresizingMask; 
     [button setFrame:rect]; 

     [self addSubview:backgroundImageView]; 
     [self addSubview:imageView]; 
     [self addSubview:button]; 

     [backgroundImageView release]; 
     [imageView release]; 
    } 
    return self; 
} 

- (void)dealloc { 

    [backgroundImageView release]; 
    [imageView release]; 
    [button release]; 

    [super dealloc]; 
} 

@end 
0

我試過你的例子在我的XCode。兩個按鈕都可以正常工作。所以一些額外的問題...你使用BackgroundImage?什麼是視圖模式(Scale to Fill或其他)?你在哪裏測試你的應用程序(設備或模擬器,iPhone 3或iPhone 4或...)?

也做一些檢查。檢查myButton是否連接到IB(也許,你已經創建了一個新的按鈕,並忘記這麼做)。檢查這段代碼是否運行(也許,你忘記了連接到必要的觸摸操作)。

+0

我在Interface Builder中使用Background和Image,模式是Scale to Fill。它只是將框架更改爲新的框架而沒有任何動畫。我在模擬器和設備上測試過,沒有運氣。 – nonamelive 2010-08-05 14:44:06

+0

我可以使用myButton.transform = CGAffileTransformMakeScale(1.2,1.2);縮放按鈕,但我不希望我的圖像縮放。圖像應該只在按鈕視圖中居中。任何幫助? – nonamelive 2010-08-05 15:17:17

+0

儘量只使用背景(無圖像)。 – kpower 2010-08-09 03:32:33