简体   繁体   中英

Detect when label appears on screen in SWIFT

I have a scroll view with several elements (textview, label, image view...), I need to display an uiview when my label appears on the screen when I'm scrolling.

How can I do ?

Make sure your view controller is the scroll view delegate. Implement the scrollViewDidScroll method, and check whether the scroll view's frame, offset by the contentOffset, overlaps the label's frame.

func scrollViewDidScroll(scrollView: UIScrollView) {
    let offset = self.scrollView.contentOffset
    let onScreen = CGRectOffset(self.scrollView.frame, offset.x, offset.y)
    if CGRectIntersectsRect(onScreen, self.label.frame) {
        NSLog("Overlap")
    }
}

If you want to detect when the label is fully on the screen, use CGRectContainsRect instead of CGRectIntersectsRect .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM