Swift - UIButton animation doesn't work when title has a variable - xcode

I'm trying to animate an UIButton. The UIButton is hidden and I want to slideUp it to show it.
At the same time, i'm changing its title with a counter "Number: (self.number)"
The title changes but the animation doesn't work. If I try to change the title to a normal string like "title", it works...
My code:
var number = 0
func moveBtn(){
self.number = self.number + 1
var yPos = 100
let myCaption: String = "Number: (\(self.number))"
self.button.setTitle(myCaption, forState: UIControlState.Normal)
UIView.animateWithDuration(0.4) {
self.button.frame.origin.y = yPos
}
}

if you've created the constraint in the interface builder you connect it to the code through an IBOutlet and try this:
var number = 0
#IBOutlet weak var BottomConstraint: NSLayoutConstraint!
func moveBtn(){
self.number = self.number + 1
var yPos = 100
let myCaption: String = "Number: (\(self.number))"
UIView.animateWithDuration(0.4, animations: {
self.button.frame.origin.y = yPos
}, completion: { finished in
self.BottomConstraint.constant = 0
self.button.setTitle(myCaption, forState: UIControlState.Normal)
})
}

Related

Continuously Redrawing a Path with Updated Data

I am developing an audio visualizer MacOS app, and I want to use Quartz/CoreGraphics to render the time-varying spectrum coordinated with the playing audio. My Renderer code is:
import Cocoa
class Renderer: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
NSColor.white.setFill()
bounds.fill()
guard let context = NSGraphicsContext.current?.cgContext else {return}
var x : CGFloat = 0.0
var y : CGFloat = 0.0
context.beginPath()
context.move(to: CGPoint(x: x, y: y))
for bin in 0 ..< 300 {
x = CGFloat(bin)
y = CGFloat(Global.spectrum[bin])
context.addLine(to: CGPoint(x: x, y: y))
}
context.setStrokeColor(CGColor( red: 1, green: 0, blue: 0, alpha: 1))
context.setLineWidth(1.0)
context.strokePath()
self.setNeedsDisplay(dirtyRect)
}
}
This draws the path once - using the initial all-zeroes values of the spectrum[] array - and then continues to draw that same all-zeroes line indefinitely. It does not update using the new values in the spectrum[] array. I used a print() statement to verify that the values themselves are being updated, but the draw function does not redraw the path using the updated spectrum values. What am I doing wrong?
The following demo shows how to update an NSView with random numbers created by a timer in a separate class to hopefully mimic your project. It may be run in Xcode by setting up a Swift project for MacOS, copy/pasting the source code into a new file called 'main.swift', and deleting the AppDelegate supplied by Apple. A draw function similar to what you posted is used.
import Cocoa
var view : NSView!
var data = [Int]()
public extension Array where Element == Int {
static func generateRandom(size: Int) -> [Int] {
guard size > 0 else {
return [Int]()
}
return Array(0..<size).shuffled()
}
}
class DataManager: NSObject {
var timer:Timer!
#objc func fireTimer() {
data = Array.generateRandom(size:500)
view.needsDisplay = true
}
func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}
func stopTimer() {
timer?.invalidate()
}
}
let dataMgr = DataManager()
class View: NSView {
override func draw(_ rect: NSRect) {
super.draw(rect)
NSColor.white.setFill()
bounds.fill()
guard let gc = NSGraphicsContext.current?.cgContext else {return}
var xOld : CGFloat = 0.0
var yOld : CGFloat = 0.0
var xNew : CGFloat = 0.0
var yNew : CGFloat = 0.0
var counter : Int = 0
gc.beginPath()
gc.move(to: CGPoint(x: xOld, y: yOld))
for i in 0 ..< data.count {
xNew = CGFloat(counter)
yNew = CGFloat(data[i])
gc.addLine(to: CGPoint(x: xNew, y: yNew))
xOld = xNew;
yOld = yNew;
counter = counter + 1
}
gc.setStrokeColor(CGColor( red: 1, green: 0, blue: 0, alpha: 1))
gc.setLineWidth(1.0)
gc.strokePath()
}
}
class ApplicationDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
#objc func myStartAction(_ sender:AnyObject ) {
dataMgr.startTimer()
}
#objc func myStopAction(_ sender:AnyObject ) {
dataMgr.stopTimer()
}
func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}
func buildWnd() {
data = Array.generateRandom(size: 500)
let _wndW : CGFloat = 800
let _wndH : CGFloat = 600
window = NSWindow(contentRect: NSMakeRect( 0, 0, _wndW, _wndH ), styleMask:[.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
window.center()
window.title = "Swift Test Window"
window.makeKeyAndOrderFront(window)
// **** Start Button **** //
let startBtn = NSButton (frame:NSMakeRect( 30, 20, 95, 30 ))
startBtn.bezelStyle = .rounded
startBtn.title = "Start"
startBtn.action = #selector(self.myStartAction(_:))
window.contentView!.addSubview (startBtn)
// **** Stop Button **** //
let stopBtn = NSButton (frame:NSMakeRect( 230, 20, 95, 30 ))
stopBtn.bezelStyle = .rounded
stopBtn.title = "Stop"
stopBtn.action = #selector(self.myStopAction(_:))
window.contentView!.addSubview (stopBtn)
// **** Custom view **** //
view = View( frame:NSMakeRect(20, 60, _wndW - 40, _wndH - 80))
view.autoresizingMask = [.width, .height]
window.contentView!.addSubview (view)
// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
quitBtn.bezelStyle = .circular
quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
quitBtn.title = "Q"
quitBtn.action = #selector(NSApplication.terminate)
window.contentView!.addSubview(quitBtn)
}
func applicationDidFinishLaunching(_ notification: Notification) {
buildMenu()
buildWnd()
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
let applicationDelegate = ApplicationDelegate()
// **** main.swift **** //
let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = applicationDelegate
application.activate(ignoringOtherApps:true)
application.run()

Swift 2 how to expand and collapse a view?

I have scrollView in which if there is no data, I need to hide (collapse the space occupied by it) so that the lower view can come upwards.
I tried this code with no luck. Please help me.
if(dataForScroll==0){
var newFrame:CGRect = self.myScroll.frame;
newFrame.size.height = 0;
self.myScroll.frame = newFrame
}
A simple example with Swift 3 version, i think it might be solves your problem. Let me know if have any queries
#IBOutlet var mainScrollView: UIScrollView!
#IBOutlet var view1: UIView!
#IBOutlet var view2: UIView!
#IBOutlet var view3: UIView!
var data1 = NSArray()
var data2 = NSArray()
var data3 = NSArray()
func setFrame()
{
// data = Something you need to get
if data1.count == 0
{
self.view1.frame.size.height = 0
}
else
{
// return its actual height you need
}
if data2.count == 0
{
self.view2.frame.size.height = 0
}
else
{
// return its actual height you need
}
if data3.count == 0
{
self.view3.frame.size.height = 0
}
else
{
// return its actual height you need
}
var frame = self.view1.frame
frame.origin.y = 5
self.view1.frame = frame
frame = self.view2.frame
frame.origin.y = self.view1.frame.origin.y + self.view1.frame.size.height + 5
self.view2.frame = frame
frame = self.view3.frame
frame.origin.y = self.view2.frame.origin.y + self.view2.frame.size.height + 5
self.view3.frame = frame
var scrollHeight:CGFloat = 0.0
var contentRect:CGRect = CGRect.zero
for views in self.mainScrollView.subviews
{
let subView:UIView = views
contentRect = contentRect.union(subView.frame)
print(subView.frame.size.height)
scrollHeight = scrollHeight + subView.frame.size.height
}
self.mainScrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: scrollHeight + 20) // the "+ 20" reamains the spaces between the views, you can change as per your need
}

How to programmatically create several NSTextFields?

I have an array of CGPoints. I need a personal label for every fourth point in array, so I need to create several NSTextFields programmatically. I can add points with mouse clicks and can create as many points as I wish. Labels for these points must be all active to show text for user simultaneously. How can I do it?
(macOS, Xcode 7, Swift 2)
Here's my code:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var drawView: DrawView!
#IBOutlet weak var view: NSView!
let label = NSTextField(frame: NSMakeRect(0,0,100,50))
var pointsArray: [CGPoint] = []
func applicationWillUpdate(aNotification: NSNotification) {
label.backgroundColor = NSColor.clearColor()
label.bezeled = false
label.stringValue = "\(pointsArray.count/4)"
var multiple = (1...25).map { _ in label }
for index in 0..<(pointsArray.count/4) {
let point = CGPoint(x: pointsArray[index*4].x, y: pointsArray[index*4].y)
label.frame = CGRect(origin: point, size: CGSize(width: label.bounds.width, height: label.bounds.height))
let sticker = multiple[index]
view.addSubview(sticker)
}
}
}
At runtime I see only one label but I need to see several labels simultaneously (on every fourth CGPoint). If I have 100 CGPoints I must have 25 labels.
I see only one label
Now that I've straightened out your curly braces and indentation, it's easy to see why. Your loop is incorrectly constructed, so that you create one label and change its frame four times. You need to create four separate labels with four separate frames.
Code for creating several NSTextFields:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var drawView: DrawView!
var pointsArray: [CGPoint] = []
var label1 = NSTextField(frame: NSMakeRect(0,0,100,50))
var label2 = NSTextField(frame: NSMakeRect(0,0,100,50))
var label3 = NSTextField(frame: NSMakeRect(0,0,100,50))
// ....................................................
var label25 = NSTextField(frame: NSMakeRect(0,0,100,50))
override func awakeFromNib() {
super.awakeFromNib()
var labelArray = [label1, label2, label3, ....., label25]
for i in 0 ..< (pointsArray.count / 4) {
labelArray[i].backgroundColor = NSColor.clearColor()
labelArray[i].bezeled = false
labelArray[i].stringValue = "\(i + 1)"
let point = CGPoint(x: (pointsArray[i * 4].x),
y: (pointsArray[i * 4].y))
var originPoint: [CGPoint] = []
originPoint.append(point)
labelArray[i].frame = .init(origin: originPoint[0],
size: CGSize(width: labelArray[i].bounds.width,
height: labelArray[i].bounds.height))
self.view.addSubview(labelArray[i])
}
}
}

Resizing the window according to a variable swift

I have a NSViewController and a variable num. I want to change the size of the window dynamically according to that variable. Is there any way to do that in swift?
Let's say your window has an IBOutlet named "window", and your dynamic number is named "myDynamicNumber":
func resize() {
var windowFrame = window.frame
let oldWidth = windowFrame.size.width
let oldHeight = windowFrame.size.height
let toAdd = CGFloat(myDynamicNumber)
let newWidth = oldWidth + toAdd
let newHeight = oldHeight + toAdd
windowFrame.size = NSMakeSize(newWidth, newHeight)
window.setFrame(windowFrame, display: true)
}
In Swift 3 to resize the window you use setFrame.
An example from the ViewController:
func resizeWin(size:(CGFloat,CGFloat)){
self.view.window?.setFrame(NSRect(x:0,y:0,width:size.0,height:size.1), display: true)
}
I needed to toggle viewing a text view so I overlaid the window an invisible view - hideRect just short of the text view; in this way I can resize to the smaller (hideRect) and restore later to the original size - origRect. Hide and original rect captured at viewDidLoad(). Swift 3/Xcode 8.3.3
// class global contants
let kTitleUtility = 16
let kTitleNormal = 22
#IBOutlet var hideView: NSView!
var hideRect: NSRect?
var origRect: NSRect?
#IBAction func toggleContent(_ sender: Any) {
// Toggle content visibility
if let window = self.view.window {
let oldSize = window.contentView?.bounds.size
var frame = window.frame
if toggleButton.state == NSOffState {
frame.origin.y += ((oldSize?.height)! - (hideRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((hideRect?.size)!)
window.showsResizeIndicator = false
window.minSize = NSMakeSize((hideRect?.size.width)!,(hideRect?.size.height)!+CGFloat(kTitleNormal))
creditScroll.isHidden = true
}
else
{
let hugeSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
frame.origin.y += ((oldSize?.height)! - (origRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((origRect?.size)!)
window.showsResizeIndicator = true
window.minSize = NSMakeSize((origRect?.size.width)!,(origRect?.size.height)!+CGFloat(kTitleNormal))
window.maxSize = hugeSize
creditScroll.isHidden = false
}
}
}
This also preserved the widow's visual origin, and sizing minimum.

Node not moving after changing scenes

I made a (basic) game, works perfect, goes to GameOverScene, and when it comes back from GameOverScene to GameScene, the player(spritenode) is not moving anymore..
I commented in the GameScene code which functions its about
I get no error from Xcode!
the bug is in the gamescene.swift file(functions: swipedRight + swipedLeft + swipedUp + swipedDown):
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var kikker:SKSpriteNode = SKSpriteNode()
var auto1:SKSpriteNode = SKSpriteNode()
var lastYieldTimeInterval:NSTimeInterval = NSTimeInterval()
var lastUpdateTimerInterval:NSTimeInterval = NSTimeInterval()
let playerCategory:UInt32 = 0x1 << 1
let auto1Category:UInt32 = 0x1 << 0
required init(coder aDecoder:NSCoder) {
fatalError("NSCoder not supported")
}
override init(size:CGSize) {
super.init(size:size)
anchorPoint = CGPoint(x:0, y:1.0)
let background = SKSpriteNode(imageNamed: "bg5")
var auto1:SKSpriteNode = SKSpriteNode(imageNamed: "auto1")
background.position = CGPoint(x:0, y:0)
background.anchorPoint=CGPoint(x:0,y:1.0)
addChild(background)
kikker = SKSpriteNode(imageNamed:"kikker5")
kikker.anchorPoint = CGPoint(x:0.5, y:0.5)
kikker.xScale = 0.22
kikker.yScale = 0.22
self.physicsWorld.gravity = CGVectorMake(0,0)
self.physicsWorld.contactDelegate = self
kikker.physicsBody?.categoryBitMask = playerCategory
kikker.physicsBody?.contactTestBitMask = auto1Category
kikker.physicsBody?.collisionBitMask = 0
kikker.physicsBody?.usesPreciseCollisionDetection = true
kikker.physicsBody = SKPhysicsBody(circleOfRadius: kikker.size.width/2)
kikker.physicsBody?.dynamic = false
kikker.position = CGPointMake(self.frame.size.width/2, -610)
addChild(kikker)
println(kikker.position)
}
func didBeginContact(contact: SKPhysicsContact!) {
var firstBody:SKPhysicsBody
var secondBody:SKPhysicsBody
if(contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){
firstBody = contact.bodyA
secondBody = contact.bodyB
}else{
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if((firstBody.categoryBitMask & auto1Category) != 0 && (secondBody.categoryBitMask & playerCategory) != 0)
{
println("aasda")
aangereden(contact.bodyB.node as SKSpriteNode, player: contact.bodyA.node as SKSpriteNode)
}
}
func addCar(){
var auto1:SKSpriteNode = SKSpriteNode(imageNamed: "auto2")
auto1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(auto1.size.width/2, auto1.size.height/2) )
auto1.physicsBody?.dynamic = true
auto1.physicsBody?.categoryBitMask = auto1Category
auto1.physicsBody?.contactTestBitMask = playerCategory
auto1.physicsBody?.collisionBitMask = 0
auto1.physicsBody?.usesPreciseCollisionDetection = true
let position : CGFloat = 51 + (CGFloat(arc4random_uniform(4)+2)*(-111))
auto1.anchorPoint = CGPoint(x:0.5, y:0.5)
auto1.position = CGPointMake(-auto1.size.width/2, position)
self.addChild(auto1)
let minDuration = 2.5
let maxDuration = 4.0
let rangeDuration = maxDuration - minDuration
let duration = Int(arc4random()) % Int(rangeDuration) + Int(minDuration)
var actionArray:NSMutableArray = NSMutableArray()
actionArray.addObject(SKAction.moveTo(CGPointMake(375 + auto1.size.width/2, position), duration: NSTimeInterval(duration)))
actionArray.addObject(SKAction.removeFromParent())
auto1.runAction(SKAction.sequence(actionArray))
}
println("DOOD")
player.removeFromParent()
}
func updateWithTimeSinceLastUpdate(timeSinceLastUpdate:CFTimeInterval){
lastYieldTimeInterval += timeSinceLastUpdate
if (lastYieldTimeInterval > 2.5){
lastYieldTimeInterval = 0
addCar()
}
}
//#1 function not working after changing scenes: the function is executing, the println works
but for some reason the runAction doesnt do its job, the node(kikker) is not moving as it should
func swipedRight1(sender:UISwipeGestureRecognizer){
var naarRechts = SKAction()
var positionX = kikker.position.x
println("right2")
if(kikker.position.x<200){
println(positionX)
positionX = kikker.position.x + 125
println(positionX)
naarRechts = SKAction.moveToX(positionX , duration: 0.25)
kikker.zRotation=(-1.570)
kikker.runAction(naarRechts)
}
}
//#2 function not working, same story
func swipedLeft1(sender:UISwipeGestureRecognizer){
var naarLinks = SKAction()
var positionX = kikker.position.x
if(kikker.position.x>150){
positionX = kikker.position.x - 125
naarLinks = SKAction.moveToX(positionX , duration: 0.25)
kikker.zRotation=(1.570)
kikker.runAction(naarLinks)
}
}
//#3 function not working, same story
func swipedDown1(sender:UISwipeGestureRecognizer){
var naarBeneden = SKAction()
var positionY = kikker.position.y
if(kikker.position.y>(-600)){
positionY = kikker.position.y - 111
naarBeneden = SKAction.moveToY(positionY , duration: 0.25)
kikker.zRotation=3.141
kikker.runAction(naarBeneden)
}
}
//#4 function not working, same story
func swipedUp1(sender:UISwipeGestureRecognizer){
var naarBoven = SKAction()
var positionY = kikker.position.y
if(kikker.position.y < (-60)){
positionY = kikker.position.y + 111
naarBoven = SKAction.moveToY(positionY, duration: 0.25)
kikker.zRotation=0
kikker.runAction(naarBoven)
}
if(positionY > (-60)){
var gameOverScene:SKScene = GameOverScene(size: self.size)
self.view?.presentScene(gameOverScene)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
var timeSinceLastUpdate = currentTime - lastUpdateTimerInterval
lastUpdateTimerInterval = currentTime
if (timeSinceLastUpdate > 1){
timeSinceLastUpdate = 1/60
lastUpdateTimerInterval = currentTime
}
updateWithTimeSinceLastUpdate(timeSinceLastUpdate)
}
}
here the GameOverScene file:
import UIKit
import SpriteKit
class GameOverScene: SKScene {
override init(size:CGSize){
super.init(size:size)
self.backgroundColor = SKColor.whiteColor()
var message:NSString = NSString()
message = "Game Over"
var label:SKLabelNode = SKLabelNode(fontNamed:"DamascusBold")
label.text = message
label.fontColor = SKColor.blackColor()
label.position = CGPointMake(self.size.width/2, self.size.height/2)
self.addChild(label)
var scene:GameScene!
self.runAction(SKAction.sequence([SKAction.waitForDuration(3.0),
SKAction.runBlock({
// var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
var scene1:SKScene = GameScene(size: self.size)
self.view?.presentScene(scene1)
})
] ))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
gameviewcontroller file:
import UIKit
import SpriteKit
import AVFoundation
class GameViewController: UIViewController, UITextFieldDelegate{
var scene:GameScene!
func swipedRight(sender: UISwipeGestureRecognizer){
scene.swipedRight1(sender)
}
func swipedLeft(sender: UISwipeGestureRecognizer){
scene.swipedLeft1(sender)
}
func swipedDown(sender: UISwipeGestureRecognizer){
scene.swipedDown1(sender)
}
func swipedUp(sender: UISwipeGestureRecognizer){
scene.swipedUp1(sender)
}
override func viewDidLoad() {
super.viewDidLoad()
let skView = view as SKView
skView.multipleTouchEnabled = false
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene)
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
You're calling your swipedUp1 (etc) methods on the wrong scene. Here's what's happening:
Your view controller has a reference to the GameScene instance that you start with. Its gesture action methods call to that, and all is well.
When you start a new game from GameOverScene, that creates a new instance of GameScene and presents it in the view. Now you have two instances of GameScene: the one your view controller is still pointing to, and the one the view is now rendering.
When you're gesture actions fire, they're still talking to the first GameScene. So your log lines get printed, but you don't see anything happen because the second GameScene is the one being displayed.
You probably don't want two scenes sticking around, anyway.
You can fix both problems by eliminatimg the scene property in your view controller and having your gesture actions call through to view.scene instead (after appropriate casting):
func swipedUp(sender: UISwipeGestureRecognizer) {
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedUp1(sender)
}
This way, the swipe goes to whichever scene the view controller's view is currently presenting.
Alternatively, you could keep your original swipedUp (etc) code, and change scene from a stored property to a read-only computed one that always gets you the currently presented scene:
var scene: GameScene {
let skView = view as SKView
return skView.scene as GameScene
}

Resources