- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了这个自定义 UIView 类来绘制自定义形状
class ShapeView: UIView {
var color: UIColor?
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.color = color
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}
func drawShape(rect: CGRect,color: UIColor) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setFillColor(UIColor.clear.cgColor)
ctx.beginPath()
ctx.move(to: CGPoint(x: rect.width / 2, y: 0))
ctx.addLine(to: CGPoint(x: rect.width, y: rect.height / 2))
ctx.addLine(to: CGPoint(x: rect.width / 2 , y: rect.height))
ctx.addLine(to: CGPoint(x: 0, y: rect.height / 2))
ctx.closePath()
ctx.setFillColor(color.cgColor)
ctx.fillPath()
ctx.strokePath()
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let path = UIBezierPath(ovalIn: self.frame)
return path.contains(point)
}
当用户按下形状内部的某处时,我需要更改此形状的 fillColor,让我们用这段代码说黑色//虽然调试颜色没有改变,但看起来我做错了什么。
在一些 UIViewController 类中我写了这个方法
class SomeClass: UIViewController {
var shape1: ShapeView?
var frame: CGRect?
override func viewDidLoad() {
let x = view.frame.midX
let y = view.frame.midY
self.frame = CGRect(x: x, y: y, width: 100, height: 100)
super.viewDidLoad()
self.shape1 = ShapeView(frame: frame!, color: .red)
shape1?.backgroundColor = .clear
view.addSubview(shape1!)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self.view)
if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.drawShape(rect: frame!, color: .black)
} else {
print("outside shape1")
shape1?.drawShape(rect: frame!, color: .red)
}
}
任何想法!
最佳答案
不要直接调用您的 drawShape()
函数 - 每次对象绘制自身时都会调用它:
override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}
因此,它会立即将填充颜色更改回 shape1
的 color
属性。
相反,做这样的事情(未经测试,只需在此处输入):
if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.color = UIColor.black
} else {
print("outside shape1")
shape1?.color = UIColor.red
}
shape1?.setNeedsDisplay()
编辑:您还可以像这样修改您的 ShapeView
类:
var color: UIColor? {
didSet {
self.setNeedsDisplay()
}
}
这将消除在触摸处理程序中“手动”调用 .setNeedsDisplay()
的需要。
编辑 #2: 您可以将其粘贴到 Playground 页面中,它应该无需更改即可运行...
import UIKit
import PlaygroundSupport
class ShapeView: UIView {
var color: UIColor? {
didSet {
self.setNeedsDisplay()
}
}
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.color = color
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}
func drawShape(rect: CGRect,color: UIColor) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setFillColor(UIColor.clear.cgColor)
ctx.beginPath()
ctx.move(to: CGPoint(x: rect.width / 2, y: 0))
ctx.addLine(to: CGPoint(x: rect.width, y: rect.height / 2))
ctx.addLine(to: CGPoint(x: rect.width / 2 , y: rect.height))
ctx.addLine(to: CGPoint(x: 0, y: rect.height / 2))
ctx.closePath()
ctx.setFillColor(color.cgColor)
ctx.fillPath()
ctx.strokePath()
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let path = UIBezierPath(ovalIn: self.frame)
return path.contains(point)
}
}
class VCA : UIViewController {
var shape1: ShapeView?
var frame: CGRect?
override func viewDidLoad() {
let x = view.frame.midX
let y = view.frame.midY
self.frame = CGRect(x: x, y: y, width: 100, height: 100)
super.viewDidLoad()
self.shape1 = ShapeView(frame: frame!, color: .red)
shape1?.backgroundColor = .clear
view.addSubview(shape1!)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self.view)
if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.color = UIColor.black
} else {
print("outside shape1")
shape1?.color = UIColor.red
}
}
}
let vcA = VCA()
vcA.view.backgroundColor = .yellow
PlaygroundPage.current.liveView = vcA.view
在 playground 页面中运行结果的屏幕录像:
关于ios - 如何更改 UIGraphicsGetCurrentContext 的 fillColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101814/
我希望能够设置箱线图系列的填充颜色,然后在某些状态下覆盖该颜色(悬停并选择)。 问题是,每当我设置默认填充颜色时,悬停和选择状态都不再起作用,并且仅使用默认填充颜色。 如果未设置默认填充颜色,则状态
我正在尝试对这样的折线应用fillColor。 但我做不到。这里出了什么问题? poly = new google.maps.Polyline({ strokeColor: '#0
我通过将 fillColor 设置为与背景相同来“伪造”一个清晰的圆圈作为标记。它工作得很好。现在,悬停时,我试图让圆圈填充系列颜色。我不能将它设置为 null,因为那样它仍然采用背景颜色。 有什么想
CircleOptions circleOptions = new CircleOptions() .center(new LatLng(latitude, longitude
我有一个接受 UIBezierPath 作为参数并相应地绘制形状的方法。如果我用纯色填充形状,它会很好地呈现,但如果我尝试使它成为带有 alpha 的颜色,我首先会收到 ARC 警告,要求更改代码并使
我编写了这个自定义 UIView 类来绘制自定义形状 class ShapeView: UIView { var color: UIColor? init(frame: CGRect, color:
我想旋转黑白图像。我尝试按如下方式使用旋转功能: image.rotate(angle, fillcolor=255) 我需要旧版本的 Python 和 Pillow,它们不支持“fillcolor”
我想在 Canvas 上创建随机粒子。但是,我无法为每个粒子提供随机确定的 RGB 颜色值,它不会读取我的 fillStyle。 有人可以帮忙吗? 编辑:JSBIN 示例 http://jsbin.c
但是,我无法更改十字的背景(如三 Angular 形符号)!有解决办法吗? marker: { symbol: 'cross', fillColor: 'white', lin
我刚刚浪费了很多时间来寻找为什么下面的代码给出不相等结果的答案: let originalColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha:
因此,如果值在一定范围内,我想更改各个条形的颜色。我如何在 JavaScript 中执行此操作?我真的不想弄乱我的 JSON 数据。 var chart = AmCharts.makeChart( "
我正在尝试使用 plotly 在 R 中构建 map 。我将在对这篇文章的回复中分享构建数据框所需的代码。这是我用来绘制图表的代码: florida %>% group_by(group) %>%
我按照 graphviz 文档中的确切说明进行操作,但无法向节点添加填充颜色。 这是我的图表: digraph hello_world { node [href="javascript:void(c
我们正在开发一个 iOS 应用程序并且在设置 MGLFillStyleLayer (MapBox iOS SDK) 的填充颜色时遇到问题。 我们有一个大的 .geojson 文件,它被解析并添加到标准
是否可以从 didTapOverlay GMSMapViewDelegate 委托(delegate)方法更改 GMSPolygon .fillColor 属性?这是委托(delegate)方法: f
我想在我的网站上使用 jquery.fillcolor.js。 我想知道如何将图像主色设置为子 div、文本或按钮。 插件演示网址:https://codepen.io/farreal/pen/KRB
我正在设置两个定义,我希望 fillColor 从 drawBar 中读取。程序未读取与该值对应的正确颜色。 import turtle wn = turtle.Screen()
如何将多个PANTONE 颜色列表 传递给PDF 库 set_graphics_option 函数? optList = "fillcolor={spotname {PANTONE 281 U} 1}
是否有任何选项可以用 fillColor: '#dedede' 填充列背景色?fillColor 在 tablecell 中工作得很好,同时它不适用于列 :( function print(
我真的搞不懂,我需要在图像上叠加一个椭圆。问题是用 matplotlib.patches 绘制椭圆,但最终得到一个白色图形或一个填充椭圆。代码非常原始,我不确定该怎么做,因为我从未使用过 matplo
我是一名优秀的程序员,十分优秀!