【发布时间】:2019-07-02 16:28:31
【问题描述】:
我正在使用这种方法从背景视图中切出一个圆角矩形“窗口”:
override func draw(_ rect: CGRect) {
guard let rectsArray = rectsArray else {
return
}
for holeRect in rectsArray {
let holeRectIntersection = rect.intersection(holeRect)
if let context = UIGraphicsGetCurrentContext() {
let roundedWindow = UIBezierPath(roundedRect: holeRect, cornerRadius: 15.0)
if holeRectIntersection.intersects(rect) {
context.addPath(roundedWindow.cgPath)
context.clip()
context.clear(holeRectIntersection)
context.setFillColor(UIColor.clear.cgColor)
context.fill(holeRectIntersection)
}
}
}
}
在layoutSubviews()我更新背景颜色添加我的“窗口框架”矩形:
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = self.baseMoodColour
isOpaque = false
self.rectsArray?.removeAll()
self.rectsArray = [dragAreaView.frame]
}
我在这里添加矩形是因为layoutSubviews() 更新了“窗口框架”的大小(即,矩形在layoutSubviews() 运行后发生变化)。
基本机制按预期工作,但是,如果我更改背景颜色,剪切窗口将填充黑色。所以我想知道如何使用这种设置为背景颜色变化设置动画?也就是说,我想为剪切窗口外部区域的颜色设置动画(窗口保持清晰)。
我尝试直接更新backgroundColor,并在我的 UIView 子类的自定义颜色变量的访问器中使用didSet,但两者都会导致“窗口”的填充相同。
var baseMoodColour: UIColor {
didSet {
self.backgroundColor = baseMoodColour
self.setNeedsDisplay()
}
}
【问题讨论】:
-
抱歉,我没有看到任何“更改背景颜色”或“动画”代码。请说明你在说什么。
-
好的,我可以稍后更新我的问题,但我实际上只是在谈论在代码中的其他位置调用
self.backgroundColor = someNewColour。我有一个我调用的颜色变量,在 didSet 中我将它分配给视图的背景颜色。 -
但是
self.backgroundColor = someNewColour不是动画。动画部分是什么? -
没关系。我可以将颜色变化放在动画块中,但颜色仍然会破坏切口。当我有机会时,我会改写问题以使动画可选。更改背景颜色会破坏剪切“窗口”。这是主要问题。
-
不确定投反对票是否会影响其他用户找到此解决方案,但如果是,则不应再投反对票。我理解为什么它最初被否决,但问题已得到澄清并提供了解决方案。