How to create gradient colour look like following image programatically.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
This is the best approach, working for me as requirement
When you say "apply it over the image as a gradient", do you mean as a mask (revealing the image at the top, having it fade the image to transparent at the bottom)? If that's the case, you can apply that gradient as a mask, using
CAGradientLayer
:The above does a simple vertical gradient (because the default is vertical, linear gradient). But you asked about
startPoint
,endPoint
, andlocations
. If for example, you wanted your mask applied horizontally, you would do:If you wanted to have two gradients, one at the first 10% and another at the last 10%, you'd do:
If you want a simple gradient by itself (not as a mask), you'd create a
view
and then add the gradient layer to it:See the
CAGradientLayer
class reference.I just wrote an UIImage extension for Swift 2.0. Maybe it's of some use. You call it with an array of UIColor (any number) and a frame where the gradient should be drawn.
Call it like this:
and apply with:
or
And the input to this method are bounds, colorArray:
percentageArray: