views:

75

answers:

2

I have an animation on frame size which works fine when the UIButton is a UIButtonTypeRoundedRect. 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!

A: 

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).

kpower
I use both Background and Image in Interface Builder, and the mode is Scale to Fill. It just changes the frame to the new one without any animation. I tested on both simulator and device, no luck.
nonamelive
I can use myButton.transform = CGAffileTransformMakeScale(1.2, 1.2); to scale the button, but I don't want my image to scale. The image should just be centered in the button view. Any help?
nonamelive
Try to use only Background (without Image).
kpower
A: 

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
nonamelive