swift - Swift:视野黑色
问题描述
您好我正在创建一个裁剪照片的功能,我想裁剪照片的容器外部是黑色的,不透明度为0.5,请看下图更好地理解:
我用黑色画的只是我希望它是黑色,不透明度为 0.5,如何做到这一点?
用户可以移动和改变框架的大小,我使用这个大小改变CGRect。所以需要黑色背景随着边框大小和位移的变化而动态变化,不知道怎么做
我的 UIIvire 框架的类:
class ResizableView: UIView {
enum Edge {
case topLeft, topRight, bottomLeft, bottomRight, none
}
static var edgeSize: CGFloat = 44.0
private typealias `Self` = ResizableView
var currentEdge: Edge = .none
var touchStart = CGPoint.zero
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
touchStart = touch.location(in: self)
currentEdge = {
if self.bounds.size.width - touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
return .bottomRight
} else if touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
return .topLeft
} else if self.bounds.size.width - touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
return .topRight
} else if touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
return .bottomLeft
}
return .none
}()
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self)
let previous = touch.previousLocation(in: self)
let originX = self.frame.origin.x
let originY = self.frame.origin.y
let width = self.frame.size.width
let height = self.frame.size.height
let deltaWidth = currentPoint.x - previous.x
let deltaHeight = currentPoint.y - previous.y
switch currentEdge {
case .topLeft:
self.frame = CGRect(x: originX + deltaWidth, y: originY + deltaHeight, width: width - deltaWidth, height: height - deltaHeight)
case .topRight:
self.frame = CGRect(x: originX, y: originY + deltaHeight, width: width + deltaWidth, height: height - deltaHeight)
case .bottomRight:
self.frame = CGRect(x: originX, y: originY, width: currentPoint.x + deltaWidth, height: currentPoint.y + deltaWidth)
case .bottomLeft:
self.frame = CGRect(x: originX + deltaWidth, y: originY, width: width - deltaWidth, height: height + deltaHeight)
default:
// Moving
self.center = CGPoint(x: self.center.x + currentPoint.x - touchStart.x,
y: self.center.y + currentPoint.y - touchStart.y)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
currentEdge = .none
}
// MARK: - Border
@IBInspectable public var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable public var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
}
解决方案
推荐阅读
- asp.net - ASP.net VB 字符串生成器双双引号
- macos - 在带有 xcode 10 的 mac os 10.13.6 上安装 graphviz 2.40.1 应用程序时出错
- c# - C# Azure MongoDB Linq Near 查询
- node.js - 如何只允许特定用户在 Firebase 中创建新用户?
- selenium - RobotFramework:如何检索与单元格值对应的数组行
- c# - 退出后停止并关闭所有进程
- android - 如何打开 Android 摄像机仅高清模式?
- linux-kernel - 编译 linux 内核时,“make”何时会提示用户选择 Kconfig 选项?
- javascript - 具有增强功能的 Highcharts 无法以小间隔正确渲染
- excel - 如何在 VBA 中的两个变量点之间创建一个范围?