复制带有Content的UILabel,一左一右滑出

Duplicate UILabel with Content and slide out one right & one left

我是 objective-c 的新手,不知道如何解决这个问题。我想我需要用调用标签并更改标签文本的按钮对此进行编码。任何人都知道如何为 funFactLabel.text 设置动画,以便在单击按钮时(旧)文本滑出并且(新)文本滑入?欢迎任何帮助!

- (IBAction)showFunFact:(UIButton *)sender {

    // ease of facts
   funFactLabel.text = [self.factBook randomFact];
}

让旧文本滑出新文本滑入的最简单方法是创建第二个标签,并在动画完成时将其移除:

UILabel *oldLabel = [[UILabel alloc] initWithFrame:funFactLabel.frame];
[funFactLabel.superview addSubview:oldLabel];
oldLabel.text = funFactLabel.text;
// TODO: Configure oldLabel to match funFactLabel's font, colors, etc.

funFactLabel.text = [self.factBook randomFact];
funFactLabel.frame = CGRectOffset(funFactLabel.frame, funFactLabel.superview.bounds.size.width, 0);

[UIView animateWithDuration:0.5 animations:^{
    funFactLabel.frame = oldLabel.frame;
    oldLabel.frame = CGRectOffset(oldLabel.frame, -oldLabel.superview.bounds.size.width, 0);
} completion:^(BOOL finished) {
    [oldLabel removeFromSuperview];
}];

如果你不需要滑动动画,你可以使用UIView的transitionWithView方法和Apple的内置动画之一来做类似的事情而不需要制作第二个标签:

[UIView transitionWithView:funFactLabel duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    funFactLabel.text = [self.factBook randomFact];
} completion:nil];