开发者

Is it possible to animate the width of a UIButton of UIButtonStyleCustom type?

I have an animation on frame size which works fine when the UIButton is a UIButtonTypeR开发者_高级运维oundedRect. But has no visible affect when I am using a UIButtonStyleCustom with background image. My animation code is here:

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

Thanks for the help!


I've tried your example in my XCode. All works fine with both buttons. So some additional questions... Do you use Background or Image? What is view's mode (Scale to Fill or something else)? And where do you test your app (device or simulator, iphone 3 or iphone 4 or ...)?

Also make some checks. Check that myButton is connected in IB (maybe, you've created a new button and forgot to do this). Check that this code runs (maybe, you forgot connection to necessary touch action).


Hey guys, I finally resolve my problem. I subclassed UIView and added two UIImageViews and an UIButton to it. The animation is perfectly good now!

Here is the code:

.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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜