- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个图形类来向用户显示根据用户信息进行相应选择的图形点。
如果我使用graphicView对象并在viewDidLoad处添加点,则图形会正确显示,如果不存在,则图形不会显示任何数据。
请参阅下面的图形代码以及我请求标记点时的代码。
class GraphView: UIView {
private struct Constants {
static let cornerRadiusSize = CGSize(width: 8.0, height: 8.0)
static let margin: CGFloat = 40.0
static let topBorder: CGFloat = 30
static let bottomBorder: CGFloat = 40
static let colorAlpha: CGFloat = 0.3
static let circleDiameter: CGFloat = 5.0
}
//1 - the properties for the gradient
var startColor: UIColor = UIColor.rgb(red: 14, green: 40, blue: 80)
var endColor: UIColor = UIColor.rgb(red: 14, green: 40, blue: 80)
//Weekly sample data
var graphPoints: [Int] = [0]
override func draw(_ rect: CGRect) {
let width = rect.width
let height = rect.height
let path = UIBezierPath(roundedRect: rect,
byRoundingCorners: UIRectCorner.allCorners,
cornerRadii: Constants.cornerRadiusSize)
path.addClip()
//2 - get the current context
let context = UIGraphicsGetCurrentContext()!
let colors = [startColor.cgColor, endColor.cgColor]
//3 - set up the color space
let colorSpace = CGColorSpaceCreateDeviceRGB()
//4 - set up the color stops
let colorLocations: [CGFloat] = [0.0, 1.0]
//5 - create the gradient
let gradient = CGGradient(colorsSpace: colorSpace,
colors: colors as CFArray,
locations: colorLocations)!
//6 - draw the gradient
var startPoint = CGPoint.zero
var endPoint = CGPoint(x: 0, y: self.bounds.height)
context.drawLinearGradient(gradient,
start: startPoint,
end: endPoint,
options: CGGradientDrawingOptions(rawValue: 0))
//calculate the x point
let margin = Constants.margin
let columnXPoint = { (column:Int) -> CGFloat in
//Calculate gap between points
let spacer = (width - margin * 2 - 4) / CGFloat((self.graphPoints.count - 1))
var x: CGFloat = CGFloat(column) * spacer
x += margin + 2
return x
}
// calculate the y point
let topBorder: CGFloat = Constants.topBorder
let bottomBorder: CGFloat = Constants.bottomBorder
let graphHeight = height - topBorder - bottomBorder
let maxValue = graphPoints.max()!
let columnYPoint = { (graphPoint:Int) -> CGFloat in
var y:CGFloat = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
y = graphHeight + topBorder - y // Flip the graph
return y
}
// draw the line graph
UIColor.white.setFill()
UIColor.white.setStroke()
//set up the points line
let graphPath = UIBezierPath()
//go to start of line
graphPath.move(to: CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
//add points for each item in the graphPoints array
//at the correct (x, y) for the point
for i in 1..<graphPoints.count {
let nextPoint = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
graphPath.addLine(to: nextPoint)
}
//Create the clipping path for the graph gradient
//1 - save the state of the context (commented out for now)
context.saveGState()
//2 - make a copy of the path
let clippingPath = graphPath.copy() as! UIBezierPath
//3 - add lines to the copied path to complete the clip area
clippingPath.addLine(to: CGPoint(x: columnXPoint(graphPoints.count - 1), y:height))
clippingPath.addLine(to: CGPoint(x:columnXPoint(0), y:height))
clippingPath.close()
//4 - add the clipping path to the context
clippingPath.addClip()
let highestYPoint = columnYPoint(maxValue)
startPoint = CGPoint(x:margin, y: highestYPoint)
endPoint = CGPoint(x:margin, y:self.bounds.height)
context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: 0))
context.restoreGState()
//draw the line on top of the clipped gradient
graphPath.lineWidth = 3.0
graphPath.stroke()
//Draw the circles on top of graph stroke
for i in 0..<graphPoints.count {
var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
point.x -= Constants.circleDiameter / 2
point.y -= Constants.circleDiameter / 2
let circle = UIBezierPath(ovalIn: CGRect(origin: point, size: CGSize(width: Constants.circleDiameter, height: Constants.circleDiameter)))
circle.fill()
}
//Draw horizontal graph lines on the top of everything
let linePath = UIBezierPath()
//top line
linePath.move(to: CGPoint(x:margin, y: topBorder))
linePath.addLine(to: CGPoint(x: width - margin, y:topBorder))
//center line
linePath.move(to: CGPoint(x:margin, y: graphHeight/2 + topBorder))
linePath.addLine(to: CGPoint(x:width - margin, y:graphHeight/2 + topBorder))
//bottom line
linePath.move(to: CGPoint(x:margin, y:height - bottomBorder))
linePath.addLine(to: CGPoint(x:width - margin, y:height - bottomBorder))
let color = UIColor(white: 1.0, alpha: Constants.colorAlpha)
color.setStroke()
linePath.lineWidth = 1.0
linePath.stroke()
}
}
我试图根据用户输入相应地标记点 - 见下文:
func budgetAvailableCalculationFunction() {
let bankValue = (userSalary as NSString).integerValue
let bankPorcentage: Int = 100
let expenses = (userExpenses as NSString).integerValue
let calculation1: Int = expenses * bankPorcentage
let calculation2: Int = calculation1 / bankValue
let cashPorcentageAvailable = calculation2
let value: [Int] = [expenses]
self.setupGraphy(points: value)
progressView.progress = 0.0
progress.completedUnitCount = Int64(cashPorcentageAvailable)
progressView.setProgress(Float(self.progress.fractionCompleted), animated: true)
progressView.progressTintColor = UIColor.rgb(red: 239, green: 75, blue: 92)
progressView.backgroundColor = UIColor.rgb(red: 239, green: 75, blue: 92)
progressView.trackTintColor = .white
progressView.clipsToBounds = false
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.layer.cornerRadius = 0
porcentageLabelForMonth.text = "\(Int(self.progress.fractionCompleted * 100)) %"
}
setupGraphy 只是一个返回用户添加的索引数组的函数。
请注意,在 viewDidLoad 中使用相同的函数是有效的:
self.setupGraphy(点: [100, 400, 200, 250])
有人吗?
viewDidLoad 代码:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.rgb(red: 245, green: 246, blue: 250)
tableView.delegate = self
tableView.dataSource = self
tableView.register(LastTransactionsCell.self, forCellReuseIdentifier: "LastTransactionsCell")
tableView.backgroundColor = .clear
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy - LLLL"
let nameOfMonth = dateFormatter.string(from: now)
currentMonthLabel.text = nameOfMonth
setupUIXViews()
fetchUserInfo()
//static data for now
*self.setupGraphy(points: [100, 400, 200, 250])*
DispatchQueue.main.asyncAfter(deadline: .now() + 2){
self.budgetAvailableCalculationFunction()
}
}
最佳答案
我设法修复的方法(似乎是临时修复)是在项目顶部创建一个新的 Int 变量,并将从数据库获取的数据传递给这些变量。这样我就可以在 viewDidLoad 中使用它们,而不是在 fetch 函数中使用它们。
关于arrays - 图形点在 viewDidLoad 之外不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56135214/
我正在学习 objective-c 教程,并注意到 viewDidLoad 中的代码放在 super viewDidLoad 下,而不是第一次调用 viewDidLoad。 代码放在viewDidLo
在Apple的scrollView示例中,他们没有这样称呼。我一直认为这是必须的。我为什么要这样调用它? 最佳答案 如果您要重写该方法,您仍然应该在 super 中调用该方法。即使父类(super c
这个问题已经有答案了: Why/when do we have to call super.ViewDidLoad? (6 个回答) 已关闭 4 年前。 override func viewDidLo
大家好,我有一个双 View Controller 。 我的 firstviewcontroller 有一个按钮,这个按钮发送 NSNotification,secontviewController
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Do I always have to call [super viewDidLoad] in the -v
我有一个关于子类化的问题。 我从我的第一个 View 开始:在我的 .h 文件中: @interface viewAController : UIViewController 在我的 .m 文件中:
我读到一篇文章说,当创建 UIViewController 类时,属性需要为零,然后 Storyboard 或在调用 viewDidLoad 时将其添加到值中。 它表示在构造对象时也无法初始化属性。
这是我当前的代码。我什至没有通过 viewDidLoad 方法覆盖,因为在它说 super viewdidload 的那一行它抛出错误 @interface for uitableview 为选择器
import UIKit class SecondViewController: UIViewController { @IBOutlet weak var labelA: UILabel!
我需要设置和存储一些可用于 View 的 NSUserDefaults 数据。这不适用于 viewDidLoad。 这可以做到吗? 在 viewDidLoad 之前我可以使用什么方法? 你会推荐什么?
当我向主视图 Controller 中的 viewDidLoad 添加方法时,我的 iPhone 应用程序崩溃了: #import "dbQuestionGetterViewController.h"
当用户切换到另一个程序然后再次返回时,原始程序的 View 将被另一个程序的新 View 替换。那么当用户切换回原来的程序时,viewDidLoad会被第二次调用吗? 我问这个是因为如果是这种情况,那
全部, 我正在从我的 applicationDidFinishLaunching 方法创建一个 windowController ... (void)applicationDidFinishLaunc
这个问题已经有答案了: Looking to understand the iOS UIViewController lifecycle (11 个回答) iPhone SDK: what is th
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 5年前关闭。 Improve this
我知道viewDidLoad在 UIViewController 期间可能会多次调用方法的生命周期。但这怎么可能?如何让它多次调用而不是直接调用它?我试过这样做: UIView *view = [
我在使用 iOS 时遇到了一点问题。我使用协议(protocol)在两个 View Controller 之间来回传递数据并手动切换 View 。我的问题是,当我关闭顶 View 时,不再调用底 Vi
我正在开发一款新应用程序,该应用程序主要用于组织网络内容中的信息。它将是一款用于监控道琼斯指数股息 yield 的金融应用程序。为此,需要执行几个步骤,但我现在正在执行第一步。我想选取道琼斯指数中的
我需要采用 viewDidLoad 方法中填充的变量来显示在连接到自定义单元格的标签上。我想做的是: 查找数据库中存储的用户盒子中的 SKU 使用SKU查找数据库中存储的产品详细信息 将产品详细信息存
我正在 viewDidLoad 中使用异步任务函数,根据返回的响应,我正在更改 View (颜色、文本...),但我有“错误”: 有时,当我更改 View 并返回 View 时,我的 View 背景没
我是一名优秀的程序员,十分优秀!