我想制作一个简单的咖啡计算器.我需要以克为单位显示咖啡量.克的"g"符号需要附加到我用来显示金额的UILabel上.UILabel中的数字正在根据用户输入动态变化,但我需要在字符串末尾添加小写"g",其格式与更新数字不同.需要将"g"附加到数字上,以便随着数字大小和位置的变化,"g"随着数字"移动".我确信这个问题已经解决了,所以正确的方向链接会有所帮助,因为我已经搜索了我的小心脏.
我在文档中搜索了一个属性字符串,我甚至从应用程序商店下载了一个"Attributed String Creator",但结果代码是在Objective-C中,我使用的是Swift.什么是令人敬畏的,并且可能对其他开发人员学习这种语言有帮助,这是使用Swift中的属性字符串创建具有自定义属性的自定义字体的明显示例.这方面的文档非常令人困惑,因为没有非常明确的方法来解决这个问题.我的计划是创建属性字符串并将其添加到coffeeAmount字符串的末尾.
var coffeeAmount: String = calculatedCoffee + attributedText
Run Code Online (Sandbox Code Playgroud)
其中calculateCoffee是一个Int转换为字符串,"attributedText"是小写的"g",其中包含我想要创建的自定义字体.也许我会以错误的方式解决这个问题.任何帮助表示赞赏!
当我到达以下行时,我正在尝试绘制一个简单的圆圈我得到错误"Double是不可转换为CGFloat下的 startAngle = 0.0
path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)
Run Code Online (Sandbox Code Playgroud)
如何"转换"0.0以使其在Swift中成为CGFloat?
我写的完整功能:
func drawCircle() {
// Drawing code
var bounds:CGRect = secondView.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: radius, startAngle: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true)
path.stroke()
}
Run Code Online (Sandbox Code Playgroud) 我希望我的按钮在用户点击后保持突出显示.如果用户再次点击该按钮,我希望它变为取消选择/不突出显示.我不确定如何快速做到这一点.我目前正在使用界面构建器将按钮高亮图像和所选图像设置为相同的.png.
当我运行应用程序并点击按钮时,只要我的手指保持在按钮上,它就会变为我的高亮图像.
在WWDC"Swift Playgrounds"视频中试图在Swift Playground中获得动画的实时视图.我试着UIView.animateWithDuration没有运气,所以我现在正在尝试做一个CABasicaAnimation,因为这似乎是演示中使用的内容.我已经摆脱了许多令人困惑的错误,并且已经导入了适当的框架,但我仍然没有运气让我的小圈子做任何事情,但是静坐在我的中心XCPShowView.这是我的代码:
import Foundation
import XCPlayground
import UIKit
import QuartzCore
//Circle Button
class timerButtonGraphics: UIView {
override func drawRect(rect: CGRect) {
let colorGreen = UIColor(red: 0.310, green: 0.725, blue: 0.624, alpha: 1.000)
let colorRed = UIColor(red: 241/255, green: 93/255, blue: 79/255, alpha: 100)
var bounds = self.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2
center.y = bounds.origin.y + bounds.size.height / 2
var radius = 61
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: …Run Code Online (Sandbox Code Playgroud) 我创建了一个使用从PaintCode导入的代码绘制咖啡杯的类,并将此类应用于视图.使用@IBDesignable,我可以在我的故事板中看到杯子正在视图中绘制,但整体形状太大了.我可以在代码中重新绘制形状,使其适合视图的当前大小,但是在绘制后无法缩放形状,以便在我的视图在不同设备上更改大小时,形状是否正确缩放?
我已经调查过,CGContextScaleCTM(aRef, <#sx: CGFloat#>, <#sy: CGFloat#>)但我不确定如何将CGRect我的视图转换bounds为正确的比例因子
我不想发布所有内容,但我的绘图代码就是这样开始的
bezierPath.moveToPoint(CGPointMake(64.8, 52.81))
bezierPath.addCurveToPoint(CGPointMake(58.89, 43.44), controlPoint1: CGPointMake(64.21, 48.28), controlPoint2: CGPointMake(62.11, 44.95))
bezierPath.addCurveToPoint(CGPointMake(56.82, 42.76), controlPoint1: CGPointMake(58.24, 43.13), controlPoint2: CGPointMake(57.55, 42.9))
Run Code Online (Sandbox Code Playgroud)
这样下去了
bezierPath.closePath()
bezierPath.miterLimit = 4
bezierPath.usesEvenOddFillRule = true;
Run Code Online (Sandbox Code Playgroud)
然后还有另外两个绘图代码块,用于绘制咖啡蒸汽的两条小线.我将这两条路径附加到原始路径bezierPath,然后设置填充颜色并填充整个形状.
我正在创建一个简单的动画,其中有两个笔画从一杯咖啡中冒出蒸汽。我已经设置了2个形状层的动画。我将这两个图层添加到2个子视图中,并UIView.animateWithDuration用于垂直设置它们的动画并逐渐淡出,且偏移很小。
我希望重复此动画,但是仅在选择我的按钮时才可以。使用UIViewAnimationOptionsi可以将选项设置为.Repeat,但是它们将永远持续下去。我已经尽力想尽一切办法停止动画,就像您在代码结尾看到的那样,但是动画不会消失。
我var buttonSelected = false在班级顶部的绘图代码之前有,我正在使用它检查并查看我的Steam动画是否应该运行。我还使用该布尔值来运行返回.Repeat或返回nilUIView动画选项的函数,具体取决于是否选择了按钮。
回到我buttonSelected在我的Steam动画类实例中设置我的视图控制器的位置,我有一个告诉该类的函数.setNeedsDisplay()。我的理解是,此函数将导致我的类实例刷新自己。这似乎可以更新从类中修改的我的Graphic的颜色,但是也许我需要调用其他内容来适当地刷新动画的删除?
//Turn steam elements into Shape Layers then into Views
colorRed.setFill()
shapePath.fill()
//Turn steam shapes into shape layers
var steam1 = CAShapeLayer()
var steam2 = CAShapeLayer()
steam1.path = shape2Path.CGPath
steam2.path = shape3Path.CGPath
steam1.fillColor = transparent.CGColor
steam2.fillColor = transparent.CGColor
//Create the views for the steam
var steam1View = UIView()
var steam2View = UIView()
self.addSubview(steam1View)
self.addSubview(steam2View)
steam1View.layer.addSublayer(steam1)
steam2View.layer.addSublayer(steam2)
func shouldRepeat()->UIViewAnimationOptions{ …Run Code Online (Sandbox Code Playgroud) 我是编程新手,之前没有在我的应用程序中使用'while'语句.我有一个cocoapod动画圆圈计时器图形.完成动画后,它会将'didFinish'BOOL设置为'true'.我能够成功检查这个变量,但是当我告诉我的应用程序将我的一个按钮标签设置为特定文本'而'didFinish为false时,它会冻结我的应用程序.
//start the timer and change the label to "Reset"
else {
brewingTimer.start()
buttonSelect = 1
startTimer.setTitle("Reset", forState: .Selected)
//Start the circle counter graphic
circleCounterOuter.startWithSeconds(5)
circleCounterInner.startWithSeconds(2)
while circleCounterInner.didFinish == false {
startTimer.setTitle("Reset!", forState: .Selected)
}
startTimer.setTitle("Ok!", forState: .Selected)
}
Run Code Online (Sandbox Code Playgroud)
我点击"开始"的那一刻,应用程序冻结了.当我在while循环中为de-bugger设置一个断点时,应用程序似乎在我的语句中循环,因为我可以点击"跳过",因为它循环通过'while-loop'.但是当我解决问题时,我当然看不到我的动画在屏幕上运行,所以我不确定实际发生了什么导致模拟迟缓.
swift ×6
animation ×2
ios8 ×2
casting ×1
cgfloat ×1
control-flow ×1
fonts ×1
ios ×1
repeat ×1
scale ×1
uibezierpath ×1
uibutton ×1
uilabel ×1
while-loop ×1