Learn-iOS-Swift-by-Examples/Quartz2D/QuartzDemo/QuartzBlendingView.swift

76 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
Demonstrates Quartz Blend modes.
*/
import UIKit
class QuartzBlendingView: QuartzView {
var sourceColor: UIColor = UIColor.white {
didSet(prevSourceColor) {
if prevSourceColor != sourceColor {
setNeedsDisplay()
}
}
}
var destinationColor: UIColor = UIColor.black {
didSet(prevDestinationColor) {
if prevDestinationColor != destinationColor {
setNeedsDisplay()
}
}
}
var blendMode: CGBlendMode = .copy {
didSet(prevBlendMode) {
if prevBlendMode != blendMode {
setNeedsDisplay()
}
}
}
override func drawInContext(_ context: CGContext) {
// Start with a background whose color we don't use in the demo
context.setFillColor(gray: 0.2, alpha: 1.0)
context.fill(bounds)
centerDrawing(inContext: context, drawingExtent: CGRect(x:0.0, y:0.0, width:270.0, height:255.0))
// We want to just lay down the background without any blending so we use the Copy mode rather than Normal
context.setBlendMode(.copy)
// Draw a rect with the "background" color - this is the "Destination" for the blending formulas
context.setFillColor(destinationColor.cgColor)
context.fill(CGRect(x: 110.0, y: 20.0, width: 100.0, height: 100.0))
// Set up our blend mode
context.setBlendMode(blendMode)
// And draw a rect with the "foreground" color - this is the "Source" for the blending formulas
context.setFillColor(sourceColor.cgColor)
context.fill(CGRect(x: 60.0, y: 45.0, width: 200.0, height: 200.0))
}
}