【问题标题】:UIButton animation not working properlyUIButton 动画无法正常工作
【发布时间】:2011-06-29 12:39:55
【问题描述】:

请参阅下面的注释代码。为什么 UIButton 的行为与 UIView 不同?

//button creation
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setImage:[UIImage imageNamed:@"someImage"] forState:UIControlStateNormal];
self.myButton = myBtn;

...
///in the button target method:

UIView* test = [[UIView alloc] initWithFrame:self.myButton.frame];
test.backgroundColor = [UIColor redColor];
[buttonContainer addSubview:test];

[UIView beginAnimations:@"SomeAnimation" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationDelegate:self];
self.myButton.frame = CGRectMake(100,100,200,200); //the position animates smoothly, but the size snaps to 200,200 immediately
//test.frame = CGRectMake(100,100,200,200); //this works perfectly. the position and size both animate smoothly over the 5 seconds.
[UIView commitAnimations];

【问题讨论】:

  • 您找到不同的解决方案了吗? woxneko 发布的那个对我很有用。

标签: ios animation uiview uibutton


【解决方案1】:
@implementation UIButton(fixResizeAnimation)

- (void)setFrame:(CGRect)_frame {
  [super setFrame:_frame];
  for(UIView* v in self.subviews) {
    if([v isKindOfClass:[UIImageView class]]) {
      [v setFrame:_frame];
    }
  }
}

@end

请试一试!

【讨论】:

  • 是的,是的,一千次是的。像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 2012-03-03
  • 2017-08-06
  • 2012-10-04
  • 2013-10-02
  • 1970-01-01
相关资源
最近更新 更多