动画UISegmentedControl的框架

cob*_*bal 1 cocoa-touch ios

我有一个UISegmentedControl,我试图使用以下(简化但功能)代码的框架动画:

UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"item1", @"item2", nil]];
[seg setFrame:CGRectMake(0, 300, 100, 50)];
[self addSubview:seg];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^ {
    [UIView animateWithDuration:5.0 animations: ^ {
        [seg setFrame:CGRectMake(100, 0, 400, 50)];
    }];
});
Run Code Online (Sandbox Code Playgroud)

发生的事情是尺寸立即改变,但位置动画正常.为什么这样做,有没有解决方法?

小智 6

如果动画块中需要,将分段控件告诉布局.

[UIView animateWithDuration:.25f animations:^{
    self.segmentedControl.frame = frame;
    [self.segmentedControl layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)