【发布时间】:2016-09-23 07:05:29
【问题描述】:
【问题讨论】:
-
我还需要为上面提到的视图设置动态背景颜色,那就是你正在使用视图,你可以建议使用贝塞尔路径方法吗
-
我还没有投票,但是这里的人可能会投票给你,因为你的问题应该能够被复制,所以你可以获得可靠的答案。
【问题讨论】:
我已经在一个测试项目中尝试过了。这是我的方式:
打开 Photoshop 或类似工具,制作一张背景为半透明的图片。
使用 PS 工具以您想要的方式绘制图形。
将其保存为 PNG。打开 Xcode。将UIImageView 放入您的UIViewController。将 PDF 放入您的 Assets 文件夹并将此图像设置为 UIImageView 的图像。设置约束。
在 UIViewController.swift 文件中设置如下代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
}
func doubleTapped() {
let image = UIImage(named: "test")
testImageView.image = image?.maskWithColor(color: UIColor.blue)
}
}
extension UIImage {
func maskWithColor(color: UIColor) -> UIImage? {
let maskImage = self.cgImage
let width = self.size.width
let height = self.size.height
let bounds = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: width, height: height))
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let bitmapContext = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) //needs rawValue of bitmapInfo
bitmapContext!.clip(to: bounds, mask: maskImage!)
bitmapContext!.setFillColor(color.cgColor)
bitmapContext!.fill(bounds)
//is it nil?
if let cImage = bitmapContext!.makeImage() {
let coloredImage = UIImage(cgImage: cImage)
return coloredImage
} else {
return nil
}
}
}
启动应用程序并触摸显示屏。一旦点击,颜色就会从黑色变为蓝色。现在你应该拥有所有工具来做任何你想做的事了......代码是用 Swift 3 语言编写的。
您可以将此 UIImageView 设置到您的 UICollectionViewCell 中,并使用提供的功能设置 UIColor。
【讨论】: