我有多个UIVIew子视图的UIScrollView。我想更新每个UIView出现在UIScrollView可见部分时显示的数据。
触发的回调是什么?我试过了viewWillAppear,但它似乎没有被调用。
感谢。 :)
答案 0 :(得分:90)
您必须自己进行计算。在滚动视图委托中实施scrollViewDidScroll:
并手动计算哪些视图可见(例如,通过检查CGRectIntersectsRect(scrollView.bounds, subview.frame)
是否返回true。
答案 1 :(得分:8)
Swift 3解决方案
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let viewFrame = greenView.frame
let container = CGRect(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
// We may have received messages while this tableview is offscreen
if (viewFrame.intersects(container)) {
// Do work here
print("view is visible")
}
else{
print("nope view is not on the screen")
}
}
答案 2 :(得分:6)
如果您的滚动视图未处于放大状态,则上述答案是正确的。万一你的滚动视图可以放大以上计算,因为你需要考虑缩放
这是代码
auto animation = Animation::createWithSpriteFrames(animFrames, 0.2f);
auto animate = Animate::create(animation);
sprite->runAction(animate);
答案 3 :(得分:5)
略微改进。我想知道滚动视图中显示的视图数量:
editor.on("beforeEndOperation", function(e) {
if (editor.curOp.docChanged && editor.curOp.command.name == "insertstring") {
var pos = editor.getCursorPosition();
var token = editor.session.getTokenAt(pos.row, pos.column);
if (token && token.type == "keyword") {
alert(
"Hey there!",
"This is me, the most annoying editor feature evar.",
"Just wanted to let you know, that you have typed a keyword",
token.value
);
}
}
});
答案 4 :(得分:1)
Ole Begemann的迅速回答5,
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.bounds.intersects(subViewFrame){
// do something
}
}
注意:请确保subViewFrame是相对于scrollView的框架计算的。