Where to put grand central dispatch code? - xcode

I have set up some code that looks like this
let queue = DispatchQueue(label: "queue.1", qos: .utility, attributes: .concurrent)
queue.async {
AppUtility().run(X: self.BitCount)
}
Here is the function that is called:
public func run(X: UILabel) {
var placeHolder = 0
while placeHolder == 0{
if globalValues.bitsPerSecond != 0 {
globalValues.Bits = globalValues.Bits + 1
//globalValues.bitsPerSecond
print("Ran")
}
UserDefaults.standard.set(globalValues.Bits, forKey: "bits")
X.text = "\(globalValues.Bits)"
sleep(1)
}
}
This code will update a label everysecond through a counter while the user is looking at the screen. From what I have read I believe my code to be correct however I have no Idea where to put it so it can always run in the background. Any help?

I have no Idea where to put it so it can always run in the background
In iOS, code doesn't run in the background. When your app is backgrounded, it is suspended.
If the idea is that the label should have the right value when the app comes back into the foreground, as if the counting had gone on while in the background, then just use the activation event as a signal to calculate how long we were backgrounded and adjust the label value accordingly.

Related

Showing more than one line of the notification description in the notification tray using an extension

I am currently designing an extension to make the notifications in the notification section of the calendar expendable. The goal is to make the noficiation expand like the initial notification on the desktop does. I have changed the type of notification added to the noficiation tray to class NotificationBanner from class NotificationMessage. I am currently using a work-around to make this work, this is what my expand function looks like:
expand(animate) {
this.expanded = true;
this._actionBin.visible = this._actionBin.get_n_children() > 0;
if (this._bodyStack.get_n_children() < 2) {
this._expandedLabel = new MessageList.URLHighlighter(this._bodyText,
true, this._useBodyMarkup);
this.setExpandedBody(this._expandedLabel);
}
if (animate) {
if (!this.clickedByButton && !this.forceExpansion) {
// This is the usual way notifications are expanded, using the layout manager
this._bodyStack.ease_property('#layout.expansion', 1, {
progress_mode: Clutter.AnimationMode.EASE_OUT_QUAD,
duration: MessageTray.ANIMATION_TIME,
});
}
else if (this.forceExpansion || this.clickedByButton) {
// When auto expanding or clicked by button, change height of body
oldHeight = this.bodyLabel.get_height();
const lines = Math.ceil(this._bodyText.length / 54);
this.bodyLabel.set_height(lines * this.bodyLabel.get_height());
}
this._actionBin.scale_y = 0;
this._actionBin.ease({
scale_y: 1,
duration: MessageTray.ANIMATION_TIME,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
});
} else {
this._bodyStack.layout_manager.expansion = 1;
this._actionBin.scale_y = 1;
}
this.emit('expanded');
}
As you can see, I have 2 options for this extension: Force expand all notifications or make the user use a button to expand. The current solution is not elegant, it simply changes the height of the notification label which manages the body. Furhermore, the notification body still shows the three dots, implying that the body is still not expanded. I believe this to be an issue with the layout manager, since the proper way to expand is to set message._bodyStack.layout_manager.expansion to 1. That does not work in the case of expanding a message in the notification tray. Is anyone familiar with the layout manager or can help me find a different solution? Here is an image of what my current solution looks like:
Image of an automatically expanded notification in the notification tray due to the extension (note the three dots at the end of the first line being still there)
Okay I have found a solution, it is not related to the layout manager. The value of the message message.bodyLabel.clutter_text.ellipsize is set to 3, which is the main cause of the dots appearing on the notification. Setting this value to 0 solves this problem. I would have still loved to find a more elegant approach to displaying the body, but this will do.

How do I skip to a position in the animation with an animationplayer in Godot?

I'm making a game in godot and have to store the animation position because the animation gets suspended by another one. I want to continue the animation where it was before and that's why I have to store the animation position and then set it to the stored value.
I've tried setting it (didn't work) and in the documentation and other places in the internet I haven't found anything helpful.
This is the script on it:
extends KinematicBody2D
onready var animation_player = $AnimationPlayer
var hurt_anim_playing: bool = false
var hurt_anim_progress: float = 0
func _ready():
animation_player.play("idle")
pass
func _physics_process(delta):
# for each touching heart, get hurt
for body in hitbox.get_overlapping_bodies():
if body.has_method("heart"):
G.health -= 1
hurt_anim_playing = true
hurt_anim_progress = animation_player.current_animation_position
animation_player.play("hurt")
update_sprite()
body.queue_free()
func die():
dieLayer.visible = true
get_tree().paused = true
func update_sprite():
sprite.frame = G.max_health - G.health
if G.health == 0:
die()
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "hurt":
hurt_anim_playing = false
animation_player.play("idle")
animation_player.current_animation_position = hurt_anim_progress
Actually I wanted to set the animation position and let the animation continue where it stopped, but instead I got an error
The problem is that current_animation_position has only a getter, not a setter:
https://docs.godotengine.org/en/3.1/classes/class_animationplayer.html#class-animationplayer-property-current-animation-position
To set the animation to a specific point, you can use advance(float delta), if you want to handle everything between start and the resuming point or seek(float seconds, bool update=false), if you just want to jump to the new position. You may test out, if you need to set update to true. The documentation says "If update is true, the animation updates too, otherwise it updates at process time."
Your code would simply look like this:
func _physics_process(delta):
# for each touching heart, get hurt
for body in hitbox.get_overlapping_bodies():
if body.has_method("heart"):
G.health -= 1
hurt_anim_playing = true
hurt_anim_progress = animation_player.current_animation_position
animation_player.play("hurt")
update_sprite()
body.queue_free()
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "hurt":
hurt_anim_playing = false
animation_player.play("idle")
animation_player.seek(hurt_anim_progress) #maybe (hurt_anim_progress, true)

SceneKit - Stop continuously looping Collada animation

I am using SceneKit to load a Collada (.dae) file.
Whatever I have tried, the animation repeats in a continuous loop whereas I only want it play once and then stop.
Here's the loading code where sceneView is my SCNView:
//Load the scene and play
let scene = SCNScene(named: "Scenes.scnassets/test4.dae")
sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
sceneView.allowsCameraControl = true
The scene loads correctly and the animation plays on loading as required, but in a continuous loop.
I have tried removing all animations as follows:
sceneView.scene?.rootNode.removeAllAnimations()
but this seems to have no effect.
I have also tried retrieving all animations and setting the repeatCount = 1 or even to 0
let url = NSBundle.mainBundle().URLForResource("Scenes.scnassets/test4", withExtension: "dae")
let sceneSource = SCNSceneSource(URL:url!, options: nil)
let animationIndentifiers = sceneSource?.identifiersOfEntriesWithClass(CAAnimation)
if let ids = animationIndentifiers
{
for id in ids {
let animation: CAAnimation = sceneSource!.entryWithIdentifier(id as! String, withClass: CAAnimation.self)! as! CAAnimation
animation.repeatCount = 1
}
}
I have also tried this another way:
let keys = sceneView.scene!.rootNode.animationKeys() //get all the animation keys
if let theKeys = keys {
for key in theKeys {
let animation = sceneView.scene?.rootNode.animationForKey(key as! String)
animation?.repeatCount = 1
}
}
but again no luck.
I can find nothing in the Collada file itself that is obviously causing the animation to repeat. However, I notice that the .dae file icon on my Mac disk has a play button and clicking this also plays the animation within the icon on a continuous loop.
Update:
I now notice that in the code above I am setting the constant 'animation' attributes and this is not copied back to the actual scene nodes. Also, the only animation in the .dae file is in a child node. So here's my next attempt:
//Get the child nodes
let children = scene?.rootNode.childNodes
//Initialise a childNode counter
var childCount = 0
//Iterate through the child nodes
if let theChildren = children {
for child in theChildren {
let keys = child.animationKeys() //get all the animation keys
//Iterate through the animations
if let theKeys = keys {
for key in theKeys {
let animation = child.animationForKey(key as! String)
println("Initial Repeat count: \(animation.repeatCount)")
animation.repeatCount = 1
//Remove existing animation
scene?.rootNode.childNodes[childCount].removeAnimationForKey(key as! String)
//Add amended animation
scene?.rootNode.childNodes[childCount].addAnimation(animation, forKey: key as! String)
}
}
childCount++
}
}
There is actually only one animation attached to one child node. (I have also tried setting this using the the actual animation id string in place of 'key' above.)
The above shows Initial Repeat count: inf
and on checking afterwards it is indeed set to 1.
However, the animation still runs in an infinite loop :-(
Any help to resolve this would be much appreciated.
Further update
I have now created a new Collada file with simple animation using Maya and for some reason one of the trials attempted above actually works:
func sceneSetup() {
let scene = SCNScene(named: "Scenes.scnassets/test10.dae")
let children = scene?.rootNode.childNodes
var childCount = 0
if let theChildren = children {
for child in theChildren {
let keys = child.animationKeys() //get all the animation keys
if let theKeys = keys {
for key in theKeys {
let animation = child.animationForKey(key as! String)
animation.repeatCount = 1
scene?.rootNode.childNodes[childCount].removeAnimationForKey(key as! String)
scene?.rootNode.childNodes[childCount].addAnimation(animation, forKey: key as! String)
}
}
childCount++
}
}
sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
}
if anybody can explain that would be great!
Ah, but there's another problem!
Here's the start frame of the animation:
and here's the end frame where we want to end up:
But at the end of the animation the scene jumps back to the start view.
I have 'fixed' this by amending the animation so that frame 1 is copy of the final frame. This works and isn't noticeable but doesn't seem a very elegant solution.
For the animation jumping back to the first frame, the isAppliedOnCompletion value is what you are looking for.
animation.repeatCount = 1
animation.isAppliedOnCompletion = true
This will make sure that the animation pauses on the final frame.
I know this is an old question, but I came across this same problem and found a solution for the jumping back to the beginning issue. If you set the animation to not be removed on completion, the object should stay at the end location:
if let animation = child.animationForKey(child.animationKeys.first!) {
animation.repeatCount = 1
animation.removedOnCompletion = false
...
This works for me. It's based off your answer but will go through the node's entire tree and limit all of their animation counts to one.
Personally I found that if I missed any of the descendant nodes when manually traversing and setting the node animation counts to one, I would have a problem. This ensures that the node passed in itself, and all child nodes will only animate once, and then hold the model in place after finishing.
Use like this animateEntireNodeTreeOnce(mostRootNode: nodeYouImportedFromCollada).
func animateEntireNodeTreeOnce(mostRootNode node: SCNNode){
onlyAnimateThisNodeOnce(node)
for childNode in node.childNodes {
animateEntireNodeTreeOnce(mostRootNode: childNode)
}
}
func onlyAnimateThisNodeOnce(_ node: SCNNode) {
if node.animationKeys.count > 0 {
for key in node.animationKeys {
let animation = node.animation(forKey: key)!
animation.repeatCount = 1
animation.isRemovedOnCompletion = false
node.removeAllAnimations()
node.addAnimation(animation, forKey: key)
}
}
}
I Agree with #Phil Dudas and I like to add also this workaround to Prevent The Animation from Start By Default By Reducing Speed to Zero, When you set Speed to Zero it will remain at First state,
nodeAnimation.speed = 0
animation.repeatCount = 1
nodeAnimation.animation.isRemovedOnCompletion = false

Swift: Trying to update NSTextField in a loop, but it only gets the last value

My very simple program is looping through an array to export several files.
While it's in the loop, I'd like it to update a text field to tell the user which file is currently exporting.
Code looks like this:
for item in filesArray {
var fileName = item["fileName"]
fileNameExportLabel.stringValue = "Exporting \(fileName).ext"
println("Exporting \(fileName).ext")
//--code to save the stuff goes here--
}
What happens is: println works correctly, throwing out a message for each file, but the label called fileNameExportLabel is only updated when the last file has been exported, so it's blank during the whole loop and gets the last file name once the loop reaches the end.
Any Idea? I'm a total noob here, I'm wondering if the NSTextField needs a command to be updated, similarly to a table view.
Thanks in advance!
Your loop is running on the main thread. The UI updates won't happen until your function finishes. Since this is taking a long time, you should do this on a background thread and then update the textfield on the main thread.
Try this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
for item in filesArray {
var fileName = item["fileName"]
// Update the text field on the main queue
dispatch_async(dispatch_get_main_queue()) {
fileNameExportLabel.stringValue = "Exporting \(fileName).ext"
}
println("Exporting \(fileName).ext")
//--code to save the stuff goes here--
}
}
This workt for me on Swift 4
DispatchQueue.global(qos: .default).async {
for item in filesArray {
var fileName = item["fileName"]
// Update the text field on the main queue
DispatchQueue.main.async {
fileNameExportLabel.stringValue = "Exporting \(fileName).ext"
}
print("Exporting \(fileName).ext")
//--code to save the stuff goes here--
}
}

'accept_position' signal not getting called in Ruby/Gtk2

I'm creating a application with panes in Ruby/Gtk. When the panes are resized, I need to do some stuff. But I cannot figure out which signal to use. I think it is 'accept_position' - but it don't seem to work. This is a sample app that illustrates my problem...
#!/usr/bin/env ruby
require 'gtk2'
window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.set_title "Panes"
window.border_width = 10
window.set_size_request(225, 150)
window.signal_connect('destroy') { Gtk.main_quit }
window.signal_connect('delete_event') { false }
button1 = Gtk::Button.new("Resize")
button2 = Gtk::Button.new("Me")
button1.signal_connect( "clicked" ) { window.destroy }
button2.signal_connect( "clicked" ) { window.destroy }
paned = Gtk::VPaned.new
paned.add1(button1)
paned.add2(button2)
paned.signal_connect( "accept_position" ) {
puts "hello"
false
}
window.add(paned)
window.show_all
Gtk.main
The 'accept_position' call is never fired. Any idea what is causing this/how to fix it?
'accept_position' is a keybinding signal, only emitted when the pane is resized using the keyboard.
I don't know Ruby, but in C the signal you want is 'notify::position'. notify is a GObject signal which fires whenever a property changes its value. If you add a detail, such as position, to the signal name when connecting it, then it only fires when that specific property changes its value.

Resources