2011-02-18 108 views
0

請參閱下面的註釋代碼。爲什麼UIButton的行爲與UIView不同?UIButton動畫無法正常工作

//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]; 
+0

您是否找到不同的解決方案?一個woxneko張貼對我很好。 – 2011-04-15 04:03:30

回答

4
@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 

請試試吧!

+0

是的,是的,有一千次是的。奇蹟般有效。 – 2011-04-15 04:02:59