Swift Animation behaves weird after changing Label










0















I am currently working on an animation for my ios application. When it gets triggered three blocks (UIView with borders) move down and a new block comes in from the left.



Working Version



It works perfectly until I change the text of the label (Block Number) before the animation starts. The blocks spawn in completely different positions and move back to the start position. They also change their order.



enter image description here



The following code shows my block class. For the animation I use the getBlockView and "view.frame = GCRect(...)". For changing the label text I use the setBlockName(number: String) function.



At the moment I don´t have any idea what could cause that and would be thankful for every suggestion what could cause this weird behaviour.



class myBlock 
var positionID: Int
var blockNumber: String
let blockView: UIView
let blockNumberLabel: UILabel

init(blockNumber: String, heightScreen: CGFloat, widthScreen: CGFloat, positionID: Int)
self.positionID = positionID
self.blockNumber = blockNumber
blockView =
let view = UIView()
view.autoSetDimension(.height, toSize: heightScreen / 6)
view.autoSetDimension(.width, toSize: widthScreen - 80)
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.MyTheme.primaryColor1.cgColor
return view
()
blockNumberLabel =
let label = UILabel()
label.textColor = UIColor.MyTheme.primaryColor1
label.font = UIFont(name: "ArialMT", size: 20)
label.numberOfLines = 2
label.textAlignment = .left
label.adjustsFontForContentSizeCategory = true
label.text = "Block Number: (blockNumber)"
label.isUserInteractionEnabled = false
return label
()

blockView.addSubview(blockNumberLabel)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .left, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .right, withInset: 5.0)



func getBlockView() -> UIView
return blockView


func setBlockName(number: String)
self.blockNumberLabel.text = "Block Number: (number)"


func getBlockLabel() -> UILabel
return blockNumberLabel


func getID() -> Int
return positionID


func setID(id: Int)
self.positionID = id
print("(blockNumberLabel.text!): (id)")





Animation Code:



 private func moveBlocksDown(blockNumber: String) 
var resetBlock: UIView!
let animationHeight = (self.heightScreen / 6) + (self.blockDistance)



UIView.animate(withDuration: 2.0, animations:
for var block in self.blockList
let id = block.getID()
let blockView = block.getBlockView()
if block.getID() == 0
block.setBlockName(number: blockNumber)

print(id)
switch id
case 0:
blockView.frame = CGRect(x: self.topX, y: self.topY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 1:
blockView.frame = CGRect(x: self.topX, y: self.middleY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 2:
blockView.frame = CGRect(x: self.topX, y: self.bottomY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 3:
blockView.frame = CGRect(x: self.topX, y: (self.bottomY + animationHeight), width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
resetBlock = blockView
default:
print("Unknown ID")

print("NewID ((id + 1) % 4)")
block.setID(id: (id + 1) % 4)

, completion: finish in
resetBlock.frame = CGRect(x: self.newX, y: self.newY, width: resetBlock.frame.width, height: resetBlock.frame.height)


)












share|improve this question
























  • show the animation code to, this is more likely the issue

    – AngryDuck
    Nov 14 '18 at 13:53











  • I added the animation code.

    – F.Fabian
    Nov 21 '18 at 13:48















0















I am currently working on an animation for my ios application. When it gets triggered three blocks (UIView with borders) move down and a new block comes in from the left.



Working Version



It works perfectly until I change the text of the label (Block Number) before the animation starts. The blocks spawn in completely different positions and move back to the start position. They also change their order.



enter image description here



The following code shows my block class. For the animation I use the getBlockView and "view.frame = GCRect(...)". For changing the label text I use the setBlockName(number: String) function.



At the moment I don´t have any idea what could cause that and would be thankful for every suggestion what could cause this weird behaviour.



class myBlock 
var positionID: Int
var blockNumber: String
let blockView: UIView
let blockNumberLabel: UILabel

init(blockNumber: String, heightScreen: CGFloat, widthScreen: CGFloat, positionID: Int)
self.positionID = positionID
self.blockNumber = blockNumber
blockView =
let view = UIView()
view.autoSetDimension(.height, toSize: heightScreen / 6)
view.autoSetDimension(.width, toSize: widthScreen - 80)
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.MyTheme.primaryColor1.cgColor
return view
()
blockNumberLabel =
let label = UILabel()
label.textColor = UIColor.MyTheme.primaryColor1
label.font = UIFont(name: "ArialMT", size: 20)
label.numberOfLines = 2
label.textAlignment = .left
label.adjustsFontForContentSizeCategory = true
label.text = "Block Number: (blockNumber)"
label.isUserInteractionEnabled = false
return label
()

blockView.addSubview(blockNumberLabel)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .left, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .right, withInset: 5.0)



func getBlockView() -> UIView
return blockView


func setBlockName(number: String)
self.blockNumberLabel.text = "Block Number: (number)"


func getBlockLabel() -> UILabel
return blockNumberLabel


func getID() -> Int
return positionID


func setID(id: Int)
self.positionID = id
print("(blockNumberLabel.text!): (id)")





Animation Code:



 private func moveBlocksDown(blockNumber: String) 
var resetBlock: UIView!
let animationHeight = (self.heightScreen / 6) + (self.blockDistance)



UIView.animate(withDuration: 2.0, animations:
for var block in self.blockList
let id = block.getID()
let blockView = block.getBlockView()
if block.getID() == 0
block.setBlockName(number: blockNumber)

print(id)
switch id
case 0:
blockView.frame = CGRect(x: self.topX, y: self.topY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 1:
blockView.frame = CGRect(x: self.topX, y: self.middleY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 2:
blockView.frame = CGRect(x: self.topX, y: self.bottomY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 3:
blockView.frame = CGRect(x: self.topX, y: (self.bottomY + animationHeight), width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
resetBlock = blockView
default:
print("Unknown ID")

print("NewID ((id + 1) % 4)")
block.setID(id: (id + 1) % 4)

, completion: finish in
resetBlock.frame = CGRect(x: self.newX, y: self.newY, width: resetBlock.frame.width, height: resetBlock.frame.height)


)












share|improve this question
























  • show the animation code to, this is more likely the issue

    – AngryDuck
    Nov 14 '18 at 13:53











  • I added the animation code.

    – F.Fabian
    Nov 21 '18 at 13:48













0












0








0


0






I am currently working on an animation for my ios application. When it gets triggered three blocks (UIView with borders) move down and a new block comes in from the left.



Working Version



It works perfectly until I change the text of the label (Block Number) before the animation starts. The blocks spawn in completely different positions and move back to the start position. They also change their order.



enter image description here



The following code shows my block class. For the animation I use the getBlockView and "view.frame = GCRect(...)". For changing the label text I use the setBlockName(number: String) function.



At the moment I don´t have any idea what could cause that and would be thankful for every suggestion what could cause this weird behaviour.



class myBlock 
var positionID: Int
var blockNumber: String
let blockView: UIView
let blockNumberLabel: UILabel

init(blockNumber: String, heightScreen: CGFloat, widthScreen: CGFloat, positionID: Int)
self.positionID = positionID
self.blockNumber = blockNumber
blockView =
let view = UIView()
view.autoSetDimension(.height, toSize: heightScreen / 6)
view.autoSetDimension(.width, toSize: widthScreen - 80)
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.MyTheme.primaryColor1.cgColor
return view
()
blockNumberLabel =
let label = UILabel()
label.textColor = UIColor.MyTheme.primaryColor1
label.font = UIFont(name: "ArialMT", size: 20)
label.numberOfLines = 2
label.textAlignment = .left
label.adjustsFontForContentSizeCategory = true
label.text = "Block Number: (blockNumber)"
label.isUserInteractionEnabled = false
return label
()

blockView.addSubview(blockNumberLabel)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .left, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .right, withInset: 5.0)



func getBlockView() -> UIView
return blockView


func setBlockName(number: String)
self.blockNumberLabel.text = "Block Number: (number)"


func getBlockLabel() -> UILabel
return blockNumberLabel


func getID() -> Int
return positionID


func setID(id: Int)
self.positionID = id
print("(blockNumberLabel.text!): (id)")





Animation Code:



 private func moveBlocksDown(blockNumber: String) 
var resetBlock: UIView!
let animationHeight = (self.heightScreen / 6) + (self.blockDistance)



UIView.animate(withDuration: 2.0, animations:
for var block in self.blockList
let id = block.getID()
let blockView = block.getBlockView()
if block.getID() == 0
block.setBlockName(number: blockNumber)

print(id)
switch id
case 0:
blockView.frame = CGRect(x: self.topX, y: self.topY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 1:
blockView.frame = CGRect(x: self.topX, y: self.middleY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 2:
blockView.frame = CGRect(x: self.topX, y: self.bottomY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 3:
blockView.frame = CGRect(x: self.topX, y: (self.bottomY + animationHeight), width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
resetBlock = blockView
default:
print("Unknown ID")

print("NewID ((id + 1) % 4)")
block.setID(id: (id + 1) % 4)

, completion: finish in
resetBlock.frame = CGRect(x: self.newX, y: self.newY, width: resetBlock.frame.width, height: resetBlock.frame.height)


)












share|improve this question
















I am currently working on an animation for my ios application. When it gets triggered three blocks (UIView with borders) move down and a new block comes in from the left.



Working Version



It works perfectly until I change the text of the label (Block Number) before the animation starts. The blocks spawn in completely different positions and move back to the start position. They also change their order.



enter image description here



The following code shows my block class. For the animation I use the getBlockView and "view.frame = GCRect(...)". For changing the label text I use the setBlockName(number: String) function.



At the moment I don´t have any idea what could cause that and would be thankful for every suggestion what could cause this weird behaviour.



class myBlock 
var positionID: Int
var blockNumber: String
let blockView: UIView
let blockNumberLabel: UILabel

init(blockNumber: String, heightScreen: CGFloat, widthScreen: CGFloat, positionID: Int)
self.positionID = positionID
self.blockNumber = blockNumber
blockView =
let view = UIView()
view.autoSetDimension(.height, toSize: heightScreen / 6)
view.autoSetDimension(.width, toSize: widthScreen - 80)
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.MyTheme.primaryColor1.cgColor
return view
()
blockNumberLabel =
let label = UILabel()
label.textColor = UIColor.MyTheme.primaryColor1
label.font = UIFont(name: "ArialMT", size: 20)
label.numberOfLines = 2
label.textAlignment = .left
label.adjustsFontForContentSizeCategory = true
label.text = "Block Number: (blockNumber)"
label.isUserInteractionEnabled = false
return label
()

blockView.addSubview(blockNumberLabel)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .left, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .right, withInset: 5.0)



func getBlockView() -> UIView
return blockView


func setBlockName(number: String)
self.blockNumberLabel.text = "Block Number: (number)"


func getBlockLabel() -> UILabel
return blockNumberLabel


func getID() -> Int
return positionID


func setID(id: Int)
self.positionID = id
print("(blockNumberLabel.text!): (id)")





Animation Code:



 private func moveBlocksDown(blockNumber: String) 
var resetBlock: UIView!
let animationHeight = (self.heightScreen / 6) + (self.blockDistance)



UIView.animate(withDuration: 2.0, animations:
for var block in self.blockList
let id = block.getID()
let blockView = block.getBlockView()
if block.getID() == 0
block.setBlockName(number: blockNumber)

print(id)
switch id
case 0:
blockView.frame = CGRect(x: self.topX, y: self.topY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 1:
blockView.frame = CGRect(x: self.topX, y: self.middleY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 2:
blockView.frame = CGRect(x: self.topX, y: self.bottomY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 3:
blockView.frame = CGRect(x: self.topX, y: (self.bottomY + animationHeight), width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
resetBlock = blockView
default:
print("Unknown ID")

print("NewID ((id + 1) % 4)")
block.setID(id: (id + 1) % 4)

, completion: finish in
resetBlock.frame = CGRect(x: self.newX, y: self.newY, width: resetBlock.frame.width, height: resetBlock.frame.height)


)









ios swift constraints ios-animations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:09







F.Fabian

















asked Nov 14 '18 at 9:09









F.FabianF.Fabian

124




124












  • show the animation code to, this is more likely the issue

    – AngryDuck
    Nov 14 '18 at 13:53











  • I added the animation code.

    – F.Fabian
    Nov 21 '18 at 13:48

















  • show the animation code to, this is more likely the issue

    – AngryDuck
    Nov 14 '18 at 13:53











  • I added the animation code.

    – F.Fabian
    Nov 21 '18 at 13:48
















show the animation code to, this is more likely the issue

– AngryDuck
Nov 14 '18 at 13:53





show the animation code to, this is more likely the issue

– AngryDuck
Nov 14 '18 at 13:53













I added the animation code.

– F.Fabian
Nov 21 '18 at 13:48





I added the animation code.

– F.Fabian
Nov 21 '18 at 13:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296489%2fswift-animation-behaves-weird-after-changing-label%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296489%2fswift-animation-behaves-weird-after-changing-label%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo