- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有几个帖子回答了类似的情况,但没有一个向我解释为什么会这样。请看下面的代码,其中 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/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!