gpt4 book ai didi

ios - 在一个流畅的动画中将 UISCrollView 居中并缩放到 1.0

转载 作者:行者123 更新时间:2023-11-28 22:23:19 32 4
gpt4 key购买 nike

有几个帖子回答了类似的情况,但没有一个向我解释为什么会这样。请看下面的代码,其中 slider 是一个 UIScrollView,centerRect 是我们想要在动画完成时查看的 CGRect。目标是让 slider 缩小到 slider.zoomScale 1.0 并在点击时居中。为此,我尝试了多种方法,并取得了一些成功,但我目前的解决方案看起来很糟糕。最好的情况是一个流畅的动画,它总是最终显示相同的最终矩形,在这种情况下它恰好居中。

我想知道为什么 zoomToRect:animated 没有达到我的预期,如果你能提供有关如何使它更平滑的想法,那将是一个很大的好处。

方法一:如果放大并向左滚动到某处,则会缩小但不会居中。实际上,您只需要调用 zoomToRect:animated: 即可,效果完全相同。如果 scrollView 没有居中,它不会在动画之后居中。第二次点击使 ScrollView 居中。

[slider zoomToRect:centerRect animated:YES];
[slider scrollRectToVisible:centerRect animated:YES];

方法二:这以相同的方式表现。

-(void) scrollToCenter{
CGRect centerRect = slider.frame;
centerRect.origin.x = (slider.contentSize.width / 2) - (centerRect.size.width / 2);
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[slider scrollRectToVisible:centerRect animated:NO];
}
completion:^(BOOL finished){

[slider zoomToRect:centerRect animated:YES];
}];
}

方法三:这可行,但创建了一个笨拙的两部分动画,坦率地说看起来很糟糕。

-(void) scrollToCenter{
CGRect centerRect = slider.frame;
centerRect.origin.x = (slider.contentSize.width / 2) - (centerRect.size.width / 2);
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[slider setContentOffset:centerRect.origin animated:NO];
}
completion:^(BOOL finished){

[slider setZoomScale:1.0 animated:YES];
}];
}

最佳答案

证明在 Stack Overflow 上发帖是有帮助的。我一发布这个我就尝试了一些我认为我以前尝试过的东西。这有效:

我还是想知道为什么 zoomToRect:animated 没有达到我的预期

-(void) scrollToCenter{
CGRect centerRect = slider.frame;
centerRect.origin.x = (slider.contentSize.width / 2) - (centerRect.size.width / 2);
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[slider setContentOffset:centerRect.origin animated:NO];
[slider setZoomScale:1.0 animated:NO];
}
completion:NULL];

}

关于ios - 在一个流畅的动画中将 UISCrollView 居中并缩放到 1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690682/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com