What is UITableView separatorEffect property for?

2019-03-18 15:59发布

问题:

New in iOS 8 is a separatorEffect property, to which you are allowed to assign a UIVisualEffect. Has anyone figured out what this is for? I've tried it and I don't see it as having any, uh, visual effect.

回答1:

I was wondering the exact same thing so I put a Github project together for anyone facing the same issue.

The basic idea is that if your tableView's backgroundView consists of a UIVisualEffectView with a blur effect, then setting the seperatorEffect to a Vibrant Effect with the same blur as the UIVisualEffectView will produce the effect we see in Notification Center where the separators seem transparent.

Something like this:

tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: blurredBackgroundView.blurView.effect as UIBlurEffect)

will produce a table view like this:



回答2:

Start by watching session 419 from this year's WWDC: "Advanced Graphics and Animations for iOS Apps", they explain how the new visual effect classes work.

I have a UITableViewController in my app that I use as a modal popover. The parent view controller gets blurred by a UIVisualEffectView with a UIBlurEffect, while the table view separators have a UIVibrancyEffect set as effect. On my iPhone 5, it looks like this:

This is what that same view looks like if separatorEffect is nil:

You could, of course, apply a UIBlurEffect to the separators, but that would most likely just be a waste of resources.

Note: Don't actually do what I did in this example. UIVibrancyEffect is very expensive. Just applying a UIVibrancyEffect to this table view's separators caused my app to miss the 60 FPS target on an iPhone 5.

Also note that the Reduce Transparency option under the Accessibility section in Settings.app is a thing and causes UIBlurEffects to be rendered as a solid color. Always check before instancing any UIVisualEffects. Here's some keywords for you to google: UIAccessibilityIsReduceTransparencyEnabled() and UIAccessibilityReduceTransparencyStatusDidChangeNotification

Hope I could help you.



回答3:

As both answers have said, the only effect this is intended for is a vibrancy effect when the background of the table is a blur effect. The difference with and without the effect can be subtle: