查看自定义设计
View with Custom design
需要像 swift 中的附加设计一样设计单个视图,并且这在我的所有 collectionview 单元格中都可见,如何实现这个任何人都知道这个
我在测试项目中尝试过。这是我的方式:
打开 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。
And here is a function to set a random UIColor.
需要像 swift 中的附加设计一样设计单个视图,并且这在我的所有 collectionview 单元格中都可见,如何实现这个任何人都知道这个
我在测试项目中尝试过。这是我的方式:
打开 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。
And here is a function to set a random UIColor.