【问题标题】:Is it possible to animate the width of a UIButton of UIButtonStyleCustom type?是否可以为 UIButton 类型自定义类型的 UIButton 的宽度设置动画?
【发布时间】:2010-08-05 09:45:26
【问题描述】:

我有一个关于帧大小的动画,当 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];

感谢您的帮助!

【问题讨论】:

  • 也许[UIView beginAnimations:nil context:@"MyAnimation"]; 真的是[UIView beginAnimations:@"MyAnimation" context:NULL];(第一个参数是animationID,NSString *;第二个-context,void *)?
  • @kpower 很抱歉打错了字,但还是不行。
  • “不工作”是什么意思?具体一点。

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


【解决方案1】:

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

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

【讨论】:

  • 我在Interface Builder中同时使用Background和Image,模式是Scale to Fill。它只是将帧更改为新帧,没有任何动画。我在模拟器和设备上都进行了测试,没有运气。
  • 我可以使用 myButton.transform = CGAffileTransformMakeScale(1.2, 1.2);缩放按钮,但我不希望我的图像缩放。图像应该在按钮视图中居中。有什么帮助吗?
  • 尽量只使用背景(没有图片)。
【解决方案2】:

大家好,我终于解决了我的问题。我将UIView 子类化并添加了两个UIImageViews 和一个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

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 1970-01-01
    • 2010-10-25
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2012-08-03
    相关资源
    最近更新 更多