Skip to content

Animate constraint change? #158

Description

@a2

I'm trying to animate constraint changes when the user taps on a segmented control. I'm using style_classes to achieve this, but it doesn't seem to be working. I checked and, at runtime, the style_classes of the two views are changing, but nothing is happening on screen. I thought maybe it would be an immediate change, but I'm not even getting that.

Here's the code I'm using:

# from app/styles/stylesheet.rb
Teacup::Stylesheet.new :main do
  PADDING = 20

  style :onscreen,
    constraints: [
      :center_x,
      constrain(:width).equals(:superview, :width).minus(2 * PADDING),
      constrain(:bottom).equals(:segmented_control, :top).minus(PADDING),
      constrain(:top).equals(:superview, :top).plus(PADDING),
    ]

  style :offscreen_right,
    constraints: [
      constrain(:right).equals(:superview, :right).plus(500),
    ]

  style :offscreen_left,
    constraints: [
      constrain(:left).equals(:superview, :left).plus(500),
    ]
end
# from app/controllers/view_controller.rb
segmented_control.on :value_changed do |sender|
  a_styles, b_styles =
    if sender.selectedSegmentIndex == 0
      [[:onscreen], [:offscreen_right]]
    else
      [[:offscreen_left], [:onscreen]]
    end

  UIView.animateWithDuration(0.3, animations:->{
    a_view.style_classes = a_styles
    b_view.style_classes = b_styles
  })
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions