是否可以为UIButtonStyleCustom类型的UIButton的宽度设置动画?

时间:2010-08-05 09:45:26

标签: iphone objective-c cocoa-touch uikit core-animation

我有一个帧大小的动画,当UIButton是UIButtonTypeRoundedRect时,它可以正常工作。但是当我使用带背景图像的UIButtonStyleCustom时没有明显的影响。我的动画代码在这里:

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我在我的XCode中试过你的例子。两个按钮都可以正常工作。还有一些其他问题......您使用的是Background还是Image?什么是视图模式(Scale to Fill或其他)?你在哪里测试你的应用程序(设备或模拟器,iPhone 3或iPhone 4或......)?

还要做一些检查。检查myButton是否在IB中连接(也许,您已经创建了一个新按钮而忘记这样做)。检查此代码是否运行(可能,您忘记了与必要的触摸操作的连接)。

答案 1 :(得分:0)

嘿伙计们,我终于解决了我的问题。我将UIView子类化,并为其添加了两个UIImageView和一个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