cgfloat

Swift: Check which value in NSArray is closest to another given value

有些话、适合烂在心里 提交于 2020-05-16 05:01:28
问题 Lets say I have an array var values:[CGFloat] = [-12.0, 450, 300] I need to find out which of these numbers is closest to a given value, say var givenValue:CGFloat = 64 Is there an efficient way to find out which object in the array is closest to 64? I know you can do something like this: if abs(values[0] - 64) < abs(values[1] - 64) && abs(values[0] - 64) < abs(values[2] - 64) { println("values[0] is the closest to 64) } But this will result in several if-statements and seems inefficient.

Swift enum raw value: not working with CGFloat = -1.0

為{幸葍}努か 提交于 2020-02-12 04:23:18
问题 What does this not work? enum Aspect : CGFloat { case Clockwise = 1.0 case Anticlockwise = -1.0 } On Anticlockwise line I'm told that 'raw value for enum case must be a literal' 回答1: That sounds like a bug. However it seems to work if you omit the decimal part: enum Aspect : CGFloat { case Clockwise = 1 case Anticlockwise = -1 } 回答2: The weird thing is that a float with a minus is not a literal, but an expression. So the error message is correct. From the Swift programming language: Unlike

Confusion due to Swift lacking implicit conversion of CGFloat

社会主义新天地 提交于 2020-01-09 19:51:10
问题 Trying to do arithmetic in a function that returns `CGFloat, I get an error: Couldn't find overload for '/' that accepts supplied arguments func kDCControlDegreesToRadians(x : CGFloat) -> CGFloat { return (M_PI * (x) / 180.0) // error is here. } Has anyone else seen this type of issue? 回答1: This is a problem with double to float conversion. On a 64-bit machine, CGFloat is defined as double and you will compile it without problems because M_PI and x are both doubles. On a 32-bit machine,

“Int is not convertible to CGFloat” error when subclassing UIImageView

☆樱花仙子☆ 提交于 2020-01-07 01:19:28
问题 I have a weird bug here. I'm subclassing UIImageView to pass some custom properties to my views. If I don't add an init method to my subclass, everything's fine. But if I do add the init (and I'm required to do so), then when I'm creating the frame view the compiler complains that an Int is not convertible to CGFloat. Le subclass with Le init : class CustomUIImageView : UIImageView { let someParameter : Bool init(someParameter: Bool) { self.someParameter = someParameter println("\

create a loop to space 2 different objects on a uiviewcontroller

不打扰是莪最后的温柔 提交于 2020-01-06 05:25:29
问题 My code below does exactly what I am looking for the problem is it just does this in a uibtton. I would like to do the same thing with 2 uitextfields and 2 uilabels. So textfield to uilabel to textfield to uilabel. I assume you would just have to change "button in" but I dont know what to change it with. I want the objects spaced 40 between each other just like below. func setConstraints() { var yPosition: CGFloat = 0 [undoButton, clearButton, color].forEach { button in NSLayoutConstraint

Round up a CGFloat in Swift

半城伤御伤魂 提交于 2019-12-28 05:06:39
问题 How can I round up a CGFloat in Swift? I've tried ceil(CDouble(myCGFloat)) but that only works on iPad Air & iPhone 5S. When running on another simulated device I get an error saying 'NSNumber' is not a subtype of 'CGFloat' 回答1: Update : Apple have now defined some CGFloat-specific versions of common functions like ceil : func ceil(x: CGFloat) -> CGFloat ...specifically to cope with the 32/64-bit difference. If you simply use ceil with a CGFloat argument it should now work on all

Swift random float between 0 and 1

妖精的绣舞 提交于 2019-12-27 16:48:53
问题 In Swift, I'm trying to get a random float between 0 and 1 but I can't seem to get the type conversions to work. func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX } I'm getting a 'CGFloat' is not convertible to 'UInt8' error Running Xcode 6. 回答1: Try initializing the divisor as a float as well, a la: CGFloat(Float(arc4random()) / Float(UINT32_MAX)) 回答2: This is extension for random numbers of Int, Double, Float, CGFloat Swift 3 & 4 & 5 syntax import Foundation import

Converting Int to CGFloat results in 0.000000

谁说我不能喝 提交于 2019-12-25 02:26:10
问题 I am attempting to take Int values from an Array and convert them into CGFloat s so that I may create a UIColor from those red, green, and blue values. Simple enough, right? I can successfully get the Int values out of the array, but when I attempt to convert to CGFloat , the result is 0.000000. Why is this? let colorArray = NSUserDefaults.standardUserDefaults().arrayForKey("colors") let redValue = colorArray[0] as Int let greenValue = colorArray[1] as Int let blueValue = colorArray[2] as Int

Cast CGFloat to Int in extension BinaryFloatingPoint

梦想与她 提交于 2019-12-22 08:11:41
问题 FIXED IN SWIFT 4.2 Details Xcode 9.2, Swift 4 Code extension BinaryFloatingPoint { func toInt() -> Int { // Eror if remove this block (I do not know why) // if let value = self as? CGFloat { // return Int(value) // } return Int(self) } } Error occurrence let float: Float = 2.38945 print("Float: \(float.toInt())") // No Error let double = Double(float) print("Double: \(double.toInt())") // No Error let cgfloat = CGFloat(float) print("CGFloat(): \(Int(cgfloat))") // No Error print(".toInt(): \

Binary operator '+' cannot be applied to two CGFloat operands?

跟風遠走 提交于 2019-12-21 03:14:20
问题 Coding in Swift and get the above error... Is the message masking something else OR can you really not add two CGFloat operands? If not, why (on earth) not? EDIT There is nothing special in the code I am trying to do; what is interesting is that the above error message, VERBATIM, is what the Swift assistant compiler tells me (underlining my code with red squiggly lines). Running Xcode 6.3 (Swift 1.2) 回答1: It's absolutely possible, adding two CGFloat variables using the binary operator '+'.