bhlvoong / LBTATools

Set of tools to drastically improve development speed of UI in iOS applications
MIT License
728 stars 94 forks source link

Animation within stacks #26

Closed kalmicka closed 4 years ago

kalmicka commented 4 years ago

Dear Brian,

first of all thanks for these tools! They are quite awesome and give swift a bit of a swiftui touch :) I started using stacks and was wondering if it possible to change the width or height of elements within the stack using an animation. I tried it but I guess this does not play with all the constraints. Can you tell me if it is possible and if so, how? Thanks for the help and keep up the good work!

bhlvoong commented 4 years ago

Please provide specific code examples and use cases of issues where I can replicate the problem and find a solution.

kalmicka commented 4 years ago

Hey Brian, thanks for answering me. I'll try my best in giving an example: ` class SampleCell: UICollectionViewCell {

var labelToGrow = UIView(backgroundColor: .red)

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = .white

    self.layer.cornerRadius = 5
    self.layer.borderWidth = 1
    self.layer.borderColor = UIColor.lightGray.cgColor

    hstack(
        labelToGrow.withWidth(50).withHeight(50), UIView(),
        spacing: 5, alignment: .center).padLeft(16).padRight(16)
}

func setupCellForGrowing(width: CGFloat) {
    UIView.animate(withDuration: 2) {
        self.labelToGrow.withWidth(width)
    }
}

required init?(coder: NSCoder) {
    fatalError()
}

} `

When setupCellForGrowing(150) is being called, I want to animate the change in the width of the label (Label: labelToGrow). However, it is not being animated and constraints are being broken.

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "id: , constant: 50.0, mulitplier 1.0", "id: , constant: 150.0, mulitplier 1.0" ) Will attempt to recover by breaking constraint id: , constant: 150.0, mulitplier 1.0

Is there any way I can animate this and not break the constraints? Thanks for the help!

bhlvoong commented 4 years ago

Animation's in Swift require that you modify the constraint's constants, for more details I suggest following the lessons in the Appstore course here: https://www.letsbuildthatapp.com/course_video?id=4602