I've been working on an app for over a month and have many variables defined at the top of the Swift document that came with my tabbed interface template in XCODE. I've declared various UIColors to be used globally and have made a few classes where I draw a few different icon shapes. All of a sudden today Xcode is throwing up red errors of "Use of unresolved identifier [one of my UIColor variabls]" for all of my colors used throughout my drawing classes. I've tried to clean the project and restart Xcode Any idea what could be going on?
Here are my global variables:
import Foundation
import UIKit
import QuartzCore
//----Global Variables----
var timerMode = "start"
var arrowButtonTapped = false
var timerButtonTouched = false
var currentView = "brew it"
//--------My Colors -----------
let colorGreen = UIColor(red: 0.310, green: 0.725, blue: 0.624, alpha: 1.000)
let colorLightGreen = UIColor(red: 211/255, green: 217/255, blue: 195/255, alpha: 100)
var colorRed = UIColor(red: 241/255, green: 93/255, blue: 79/255, alpha: 100)
let colorDarkBlue = UIColor(red: 27/255, green: 29/255, blue: 38/255, alpha: 1.0)
let colorOrange = UIColor(red: 241/255, green: 162/255, blue: 128/255, alpha: 1.0)
let colorTransparent = UIColor(red: 241/255, green: 162/255, blue: 128/255, alpha: 0.0)
//----Countdown times----
var countTime : NSTimeInterval = 6
var halfCountTime : NSTimeInterval = countTime * 0.5
var okCountTime : NSTimeInterval = 2
//---- End Global Variables ----
//let cup1Graphic = coffeeMugVector()
//let cup2Graphic = coffeeMugVector()
//let cup3Graphic = coffeeMugVector()
//let cup4Graphic = coffeeMugVector()
//let cup5Graphic = coffeeMugVector()
//let cup6Graphic = coffeeMugVector()
//Coffe Cups x Coffee Strength returns amount of coffee as an integer
class coffeeCalculator {
var selectedCups = 0
var cupSelect = false
let coffeeStronger = 20
var coffeeStrength = 15
let coffeeWeaker = 15
var water = 226
//var cupAnimation = CSAnimationView(type:"morph", duration:0.5, delay:0)
// What to do when a cup is selected
func cupSelected() {
if cupSelect == true{
selectedCups++
}
else if selectedCups > 0{
selectedCups--
}
println("\(selectedCups)")
}
//take coffee cups multiplied by coffee strength and return the amount as a string
//Calcualte coffee and return an attributed string
func coffeeTextOnly() ->String {
var calculatedCoffee = selectedCups * coffeeStrength
var coffeeToString = "\(calculatedCoffee)"
return coffeeToString
}
func calculateCoffee() -> (NSMutableAttributedString) {
var calculatedCoffee = selectedCups * coffeeStrength
var coffeeToString = "\(calculatedCoffee)"
//Convert the CoffeeCalculator output to an attributed string
var coffeeText = NSMutableAttributedString(string:coffeeToString)
//Part 2 set the font attributes for the lower case g
var coffeeTypeFaceAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(18)]
//Part 3 create the "g" character and give it the attributes that you set up
var coffeeG = NSMutableAttributedString(string:"g", attributes:coffeeTypeFaceAttributes)
coffeeText.appendAttributedString(coffeeG)
return (coffeeText)
}
//Calculate teh amount of water needed and return it as a string
func calculateWater() -> (NSMutableAttributedString) {
var calculatedWater = water * selectedCups
var waterToString = "\(calculatedWater)"
var waterText = NSMutableAttributedString(string:waterToString)
//Part 2 set the font attributes for the lower case g
var waterTypeFaceAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(18)]
//Part 3 create the "g" character and give it the attributes that you set up
var waterG = NSMutableAttributedString(string:"g", attributes:waterTypeFaceAttributes)
waterText.appendAttributedString(waterG)
return (waterText)
}
}
////----BEGIN GRAPHICS ----////
//----Main Timer (circle) Button----//
#IBDesignable
class timerButtonGraphics: UIView {
override func drawRect(rect: CGRect) {
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 = 31
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
path.strokeWithBlendMode(kCGBlendModeNormal, alpha: 0)
path.lineWidth = 1
if timerMode == "reset" || timerMode == "ok" {
colorRed.setStroke()
colorRed.setFill()
}
else {
colorGreen.setStroke()
colorGreen.setFill()
}
if timerButtonTouched == true {
path.lineWidth = 2
path.fill()
}
path.stroke()
}
}
//------Arrow Button------//
#IBDesignable
class arrowButtonGraphic: UIView {
override func drawRect(rect: CGRect) {
var bezierPath = UIBezierPath()
bezierPath.moveToPoint(CGPointMake(20.36, 2.68))
bezierPath.addLineToPoint(CGPointMake(2.69, 20.23))
bezierPath.addCurveToPoint(CGPointMake(2.69, 28.32), controlPoint1: CGPointMake(0.44, 22.46), controlPoint2: CGPointMake(0.44, 26.09))
bezierPath.addCurveToPoint(CGPointMake(10.84, 28.32), controlPoint1: CGPointMake(4.94, 30.56), controlPoint2: CGPointMake(8.59, 30.56))
bezierPath.addLineToPoint(CGPointMake(22.71, 16.53))
bezierPath.addCurveToPoint(CGPointMake(26.29, 16.53), controlPoint1: CGPointMake(23.7, 15.55), controlPoint2: CGPointMake(25.3, 15.56))
bezierPath.addLineToPoint(CGPointMake(38.16, 28.32))
bezierPath.addCurveToPoint(CGPointMake(46.31, 28.32), controlPoint1: CGPointMake(40.41, 30.56), controlPoint2: CGPointMake(44.06, 30.56))
bezierPath.addCurveToPoint(CGPointMake(46.31, 20.23), controlPoint1: CGPointMake(48.56, 26.09), controlPoint2: CGPointMake(48.56, 22.46))
bezierPath.addLineToPoint(CGPointMake(28.64, 2.68))
bezierPath.addCurveToPoint(CGPointMake(24.48, 1), controlPoint1: CGPointMake(27.49, 1.54), controlPoint2: CGPointMake(25.98, 0.98))
bezierPath.addCurveToPoint(CGPointMake(20.36, 2.68), controlPoint1: CGPointMake(22.99, 0.99), controlPoint2: CGPointMake(21.5, 1.55))
bezierPath.closePath()
bezierPath.miterLimit = 4;
colorGreen.setStroke()
bezierPath.lineWidth = 1
bezierPath.stroke()
colorGreen.setFill()
//Rotate arrow if on "weigh it" page
if arrowButtonTapped == true {
bezierPath.lineWidth = 2
bezierPath.stroke()
}
}
}
//----Timer Baground Circle ----//
#IBDesignable
class timerBackgroundCircle: UIView {
override func drawRect(rect: CGRect) {
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 238, 238))
colorLightGreen.setFill()
ovalPath.fill()
}
}
//----Timer Bagkround Mask ----//
#IBDesignable
class timerMask: 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)
let colorLightGreen = UIColor(red: 211/255, green: 217/255, blue: 195/255, alpha: 100)
//The shape to mask out
/*var ovalPath = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: 238, height: 238))
colorGreen.setFill()
ovalPath.fill()*/
//The rectangle
var bounds : CGRect = self.bounds
var maskRect = CAShapeLayer()
maskRect.frame = bounds
//maskRect.fillColor = colorLightGreen.CGColor
//The circle mask
var maskCirclePath = UIBezierPath(ovalInRect: CGRect(x: 40, y: 150, width: 238, height: 238))
maskCirclePath.appendPath(UIBezierPath(rect: bounds))
//combine appended path to rectangle path
maskRect.path = maskCirclePath.CGPath
colorRed.setFill()
maskCirclePath.fill()
maskRect.fillRule = kCAFillRuleEvenOdd
self.layer.mask = maskRect
}
}
I'm not sure if my classes are being declared correctly. I made a group called "Vectors" where I planned to make a separate class for each drawing. I'm not sure if i'm supposed to select file -> new -> file and select a Cocoa Touch Class, or if i can just create a blank Swift file and write out my class code from scratch there. This is the option I was trying to figure out when my globial variables "went bad".
What's more strange is that the app compiles just fine in the simulator, my colors appear correctly for the classes applied to my icons that use the global color variables. But Xcode is throwing a fit so my #IBDesignables won't compile in storyboard and I have a bunch of red errors.
UPDATE
Somewhere along the way here, the Swift file that I posted in my question was removed from my projects "tests" target membership. The little checkbox was unchecked. I have no idea how I could have done this, but everything was back to normal until i got an xcode error whiting out all of my text and saying that editing is currently disabled. I ended up removing all of my swift files from "tests" (unchecking the "tests" target membership for all of the files"), then creating a new Objective C file which prompted Xcode to ask me to configure a new bridging header I transferred my old bridging header code to the new file. This seems to have fixed the problem. I'm not submitting this an an answer because I don't know what the actual problem was.
Related
I have the function Track.getShadowPath() that is returning a CGMutablePath.
Function returning CGMutablePath
static func getShadowPath() -> CGMutablePath{
let trackPath = CGMutablePath()
let initPoint = Track.tracks.first?.initPoint
let heigthShift = CGPoint(x: 0, y: 100)
trackPath.move(to: initPoint! - heigthShift)
var yShift = CGPoint(x: 0, y: 0)
for track:Track in Track.tracks{
let q = track.getQuadrant()
if(q == 2 || q == 5){
yShift.y = yShift.y + track.endPoint.y - track.initPoint.y
}
else {
trackPath.addLine(to: track.endPoint - heigthShift - yShift)
}
}
return trackPath
}
This path is then added to a CAShapeLayer and the layer added to my view as a sublayer. I want this path to have a shadow aspect. But when it renders, it produces a shadow as if the path was closed. How can I achieve my purpose?
NSView
class DemoView: NSView {
var trackShape:CAShapeLayer!
var trackShadowShape:CAShapeLayer!
override init(frame: CGRect){
super.init(frame: frame)
trackShape = CAShapeLayer()
trackShape.strokeColor = NSColor.black.cgColor
trackShape.lineWidth = 3
trackShape.lineJoin = kCALineJoinBevel
trackShape.lineCap = kCALineCapRound
trackShape.fillColor = nil
trackShadowShape = CAShapeLayer()
trackShadowShape.lineWidth = 14
trackShadowShape.lineJoin = kCALineJoinBevel
trackShadowShape.lineCap = kCALineCapRound
trackShadowShape.fillColor = nil
trackShadowShape.shadowColor = NSColor.blue.cgColor
trackShadowShape.shadowRadius = 10
self.layer?.addSublayer(trackShadowShape)
self.layer?.addSublayer(trackShape)
}
func redrawTrack(){
trackShape.path = Track.getCompletePath()
trackShadowShape.shadowPath = Track.getShadowPath()
trackShadowShape.shadowRadius = 20
trackShadowShape.shadowOpacity = 1
trackShadowShape.fillColor = NSColor.clear.cgColor
trackShadowShape.lineWidth = 10
}
}
The non-expected result
I solved this using the:
copy(strokingWithWidth lineWidth: CGFloat, lineCap: CGLineCap, lineJoin: CGLineJoin, miterLimit: CGFloat, transform: CGAffineTransform = default)
According to the docs
Returns a new path equivalent to the results of drawing the path with
a solid stroke.
So basically it creates a new CGPath which is the stroke's outline of the path you provide, with the desired width, cap, etc... I made my getShadowPath function return this new calculated path:
return trackPath.copy(strokingWithWidth: 6, lineCap: CGLineCap.round, lineJoin: CGLineJoin.round, miterLimit: 10) as! CGMutablePath
Then I used that path as shadowPath for my layer. The results are now the expected ones:
A similar answer in Objective-c can be found here
I have a popup view, that I would like to populate with ~15 UISwitches. Now it would be stupid to do all of that manually, so in Android I have set up a for loop to create said UISwitches from an array and you are able to toggle each one on and off and so on, each having their own value. I have created one switch, but when trying to create multiple from an array, none show up and I have no idea how I would be able to position them one after another.
let mySwitch = UISwitch()
mySwitch.isOn = false
mySwitch.center = self.view.center
mySwitch.thumbTintColor = UIColor(red: 23.0/255, green: 145.0/255, blue: 255.0/255, alpha: 1.0)
mySwitch.tintColor = UIColor(red: 23.0/255, green: 145.0/255, blue: 255.0/255, alpha: 1.0)
mySwitch.onTintColor = UIColor.black
mySwitch.backgroundColor = UIColor.white
self.view.addSubview(mySwitch)
var scrol = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
scrol.frame = CGRect(x: 10, y: 0, width: self.view.frame.size.width, height: 200)
var yPos = 0.0
for i in 0...10 {
let mySwitch = UISwitch()
mySwitch.frame = CGRect(x: 10, y: yPos + 10, width: 50, height: 25)
mySwitch.isOn = false
// mySwitch.center = self.view.center
mySwitch.tag = i
mySwitch.thumbTintColor = UIColor(red: 23.0/255, green: 145.0/255, blue: 255.0/255, alpha: 1.0)
mySwitch.tintColor = UIColor(red: 23.0/255, green: 145.0/255, blue: 255.0/255, alpha: 1.0)
mySwitch.onTintColor = UIColor.black
mySwitch.backgroundColor = UIColor.white
yPos = Double(mySwitch.frame.origin.y + mySwitch.frame.size.height)
mySwitch.addTarget(self, action: #selector(onSwitchChange(_:)), for: .touchUpInside)
scrol.addSubview(mySwitch)
}
scrol.backgroundColor = .yellow
scrol.isScrollEnabled = true
self.view.addSubview(scrol)
}
func onSwitchChange(_ sender: UISwitch) {
print(" switch tapped \(sender.tag)")
}
Check out this, i have added a scrollView inside a UIView and have added multiple UISwitch as you have given in code with Frame set for UISwitch.
I'd recommend looking at UIStackView.
This allows you to easily add multiple UI elements to the screen at consistent positions without any hassle.
First you'd want to create a stack view:
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 10
add it and constrain it:
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.topAnchor),
// etc, etc
])
Then you'd simply add a series of switches to the stack view:
for i in 0..<10 {
let switch = UISwitch()
// Configure switch
stackView.addArrangedSubview(switch)
}
Im new with Xcode and swift.
Im doing this game from this tutorial ( https://www.youtube.com/watch?v=hLOhirsXx9c&list=PL1YTxp2xLtqSiBhoAiaImXFcjAAxvo1es&index=1 )
The game is working good, but i wanna do that game with pictures, not code.
I can add background but i can't get pictures top of the background and get them moving.
example : this is the code for the ground that its moving to the right, but i wanna put 2 images instead of code.
import Foundation
import SpriteKit
class KBMovingGround: SKSpriteNode {
let NUMBER_OF_SEGMENTS = 20
let COLOR_ONE = UIColor(red: 88.0/255.0, green: 148.0/255.0, blue: 87.0/255.0, alpha: 1.0)
let COLOR_TWO = UIColor(red: 120.0/255.0, green: 195.0/255.0, blue: 118.0/225.0, alpha: 1.0)
init(size: CGSize) {
super.init(texture: nil, color: UIColor.brownColor(), size: CGSizeMake(size.width*2, size.height))
anchorPoint = CGPointMake(0, 0.5)
for var i = 0; i < NUMBER_OF_SEGMENTS; i++ {
var segmentColor: UIColor!
if i % 2 == 0 {
segmentColor = COLOR_ONE
} else {
segmentColor = COLOR_TWO
}
let segment = SKSpriteNode(color: segmentColor, size: CGSizeMake(self.size.width / CGFloat (NUMBER_OF_SEGMENTS), self.size.height))
segment.anchorPoint = CGPointMake(0.0, 0.5)
segment.position = CGPointMake(CGFloat(i)*segment.size.width, 0)
addChild(segment)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func start(){
let moveLeft = SKAction.moveByX(-frame.size.width/2, y: 0, duration: 1.0)
let resetPosition = SKAction.moveToX(0, duration: 0)
let moveSequence = SKAction.sequence([moveLeft, resetPosition])
runAction(SKAction.repeatActionForever(moveSequence))
}
}
And i wanna do same thing with that "ninja" moving top on the ground.
Then i wanna make the ground and the objective thats moving on it go for the same speed so how do i make that?
And how i can make it speeding up when the player gets further at the game.
Thank you for the help beforehand and sorry for my bad english!
I have created a rectangular image in the form of an SKSpriteNode in Swift with the following code:
var screenImage = SKSpriteNode(texture: SKTexture(imageNamed: "\(imageChoices[randomImageChoice].0)"))
screenImage.position = CGPointMake(screen1.position.x, screen1.position.y)
screenImage.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height)
self.addChild(screenImage)
I proceed to move the image with the following code:
func swipedTrue(sender: UISwipeGestureRecognizer) {
if gameOver == false && tutorial == false {
//if you swipe, it checks if you were right, then moves on or GameOver()
if (wordChoices[randomWordChoice]).1 == true {
//reset time
timePerQuestion = 1.0
//randomize word
randomWordChoice = Int(arc4random() % 3)
newImage = SKSpriteNode(texture: SKTexture(imageNamed: "\(wordChoices[randomWordChoice].0)"))
//randomize color of screens, mid-swipe
newScreen.fillColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)
//replace timeBar
decreaseTimeBlock.fillColor = newScreen.fillColor
decreaseTimeBlock.position = CGPointMake(self.frame.size.width * 1.5, self.frame.size.height * 0.985)
timeBarRedValue = 0.0; timeBarGreenValue = 1.0
newTimeBar.fillColor = UIColor(red: CGFloat(timeBarRedValue), green: CGFloat(timeBarGreenValue), blue: 0.0, alpha: 1.0)
//actions caused by swipe: it's "bringNewScreen" because if you swipeFalse, the newScreen comes from bottom. If you swipeTrue, it comes from the top.
var swipeTrueCurrentScreen = SKAction.moveToX(self.frame.size.width * 2, duration: 0.5)
var bringNewScreen = SKAction.moveToY(self.frame.size.height * 0.5, duration: 0.5)
var bringNewTimeBar = SKAction.moveToY(self.frame.size.height * 0.985, duration: 0.5)
//reset the newScreen and word to the top of the screen, to be dropped again
newScreen.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1)
newImage.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1)
newTimeBar.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1.58)
//swipe word and screen
currentImage.runAction(swipeTrueCurrentScreen)
currentTimeBar.runAction(swipeTrueCurrentScreen)
currentScreen.runAction(swipeTrueCurrentScreen)
//make swiping noise
runAction(SKAction.playSoundFileNamed("Swoosh 3-SoundBible.com-1573211927.mp3", waitForCompletion: false))
//bring in the newScreen
newScreen.runAction(bringNewScreen)
newImage.runAction(bringNewScreen)
newTimeBar.runAction(bringNewTimeBar)
//increase score
++score
scoreLabel.text = "\(score)"
//here, switch the currentScreen with the newScreen so that the process can be repeated
if newScreen == screen1 {
newScreen = screen2
newImage = screenImage2
newTimeBar = timeBar2
currentScreen = screen1
currentImage = screenImage1
currentTimeBar = timeBar1
} else {
newScreen = screen1
newImage = screenImage1
newTimeBar = timeBar1
currentScreen = screen2
currentImage = screenImage2
currentTimeBar = timeBar2
}
} else {
GameOver()
}
}
}
However, for some reason, the image will not move, and when I try to move it in other situations at well, it refuses. How can I fix this?
Except one missing parenthesis here (but I guess it's not the case in your code) the code have no particular reason to not work. The problem is most likely on how you use it.
My guess is that you are doing something like so :
import SpriteKit
class GameScene: SKScene {
var sprite : SKSpriteNode = SKSpriteNode() // A
override func didMoveToView(view: SKView) {
// You are creating another `sprite` variable
// and not using the (A) sprite you declare above
var sprite = SKSpriteNode(imageNamed:"Spaceship") // B
// Here you set the (B) sprite you just created
sprite.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height)
sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
// Here it's still the (B) sprite you just created
// that you add to the scene
self.addChild(sprite)
// You are calling your action from somewhere else
self.applyAction()
}
func applyAction() {
// You create an action, OK
let action = SKAction.moveToX(self.frame.size.width * 2, duration: 0.5)
// You apply the action to the (A) sprite property you have in your class
// Same as : self.sprite.runAction(action)
sprite.runAction(action)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
In that case, you just have to not create another sprite variable. By removing the var keyword on the // B line.
Let met know if it helped.
If it's not the case, please give more details (code, ...).
I'm trying to get a Live View to show a simple animation in a Swift Playground. Whenever I import the XCPlayground framework to execute the XCPShowView function i get this error:
Playground execution failed: error: Couldn't lookup symbols:_CGPointMake
The error changes for a few other "symbols" as well, including CGRectMake.
After being advised to modifying my code to remove the "make" from methods such as CGRectMake I still get an error from Xcode when I try to animate my view. The error message is really long, but basically it says
"Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want."
Here is the code I am trying to work with:
//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: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
path.strokeWithBlendMode(kCGBlendModeNormal, alpha: 100)
path.lineWidth = 2
if strokeRed == true {
colorRed.setStroke()
}
else {
colorGreen.setStroke()
}
path.stroke()
}
var strokeRed = true
}
var test = timerButtonGraphics(frame: CGRect(x: 0, y: 400, width: 400, height: 400))
UIView.animateWithDuration(2.0, delay: 2, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: nil, animations: {
test.transform = CGAffineTransformMakeTranslation(0.5, 0)
}, completion: nil)
XCPShowView("Circle Animation", test)
try with this, maybe it can help you
test.setTranslatesAutoresizingMaskIntoConstraints(false)