How animate properly height constraint

How animate properly height constraint



Inside my app I'm animating the height constraint of a view and when the height constraint changes the view does translate for a while a little bit down despite I set the constraint to the top as 0. Here's the code I used to animate the change:


UIView.animate(withDuration: 0.5)
if finalHeight >= mediumY
self.heightConstraintConstant = self.maxHeight
else
self.heightConstraintConstant = self.minHeight




And here's the one for creating all the expandable view:


//
// ExpandibleView.swift
// PhotoMapping
//
// Created by Lorenzo Santini on 04/09/18.
// Copyright © 2018 Lorenzo Santini. All rights reserved.
//

import Foundation
import UIKit

class ExpandableView: UIView
let panGestureRecognizer = UIPanGestureRecognizer()

var minHeight: CGFloat = 0
didSet
initialHeight = minHeight


var maxHeight: CGFloat = 0

var initialHeight: CGFloat = 0
var initialPoint: CGPoint? = nil

var heightConstraintConstant: CGFloat
get
if !self.constraints.filter( $0.firstAttribute == .height).isEmpty
return self.constraints.filter($0.firstAttribute == .height && $0.secondItem == nil).first!.constant
else
let constrain = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: initialHeight)
constrain.priority = .defaultHigh
self.addConstraint(constrain)
return constrain.constant



set(newConstant)
if !self.constraints.filter( $0.firstAttribute == .height).isEmpty
self.constraints.filter($0.firstAttribute == .height && $0.secondItem == nil).first?.constant = newConstant
else
let constrain = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: newConstant)
self.addConstraint(constrain)

//self.layoutIfNeeded()




func initialize(minHeight: CGFloat, maxHeight: CGFloat)
//Create panGestureRecognizer
panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))
panGestureRecognizer.minimumNumberOfTouches = 1
panGestureRecognizer.maximumNumberOfTouches = 1
self.addGestureRecognizer(panGestureRecognizer)

//Set min and max height
self.minHeight = minHeight
self.maxHeight = maxHeight

self.translatesAutoresizingMaskIntoConstraints = false

self.subviews.map$0.translatesAutoresizingMaskIntoConstraints = false


@objc func handlePan(_ sender: Any)
let translation = panGestureRecognizer.translation(in: self)

if initialPoint == nil
initialPoint = translation


let translationHeight = CGFloat(translation.y - initialPoint!.y)
let finalHeight = translationHeight + initialHeight

if panGestureRecognizer.state == .changed
if finalHeight <= maxHeight && finalHeight >= minHeight
heightConstraintConstant = finalHeight
else if finalHeight >= maxHeight
heightConstraintConstant = maxHeight
else if finalHeight <= minHeight
heightConstraintConstant = minHeight

else if panGestureRecognizer.state == .ended

let mediumY = maxHeight / 2 + minHeight / 2

if finalHeight >= mediumY
self.heightConstraintConstant = self.maxHeight
else
self.heightConstraintConstant = self.minHeight

UIView.animate(withDuration: 0.5)
self.layoutIfNeeded()


initialPoint = nil
initialHeight = heightConstraintConstant


//layoutIfNeeded()





The problem for me isn't how animate the height change because that works but instead is solve the fact that for a while the app seems to ignore the constraint to the top.
Here's the gif showing the animation






But that is not how to animate a constraint change.

– matt
Sep 14 '18 at 18:35






Possible duplicate of iOS: How does one animate to new autolayout constraint (height).

– rmaddy
Sep 14 '18 at 18:39







I’ve never done anything similar...how should I do?

– Lorenzo Santini
Sep 14 '18 at 18:40






Possible duplicate of iOS: How does one animate to new autolayout constraint (height)

– Rakesha Shastri
Sep 14 '18 at 18:40






@RakeshaShastri I did not find the explanation to animate a constraint change

– Lorenzo Santini
Sep 14 '18 at 18:57




1 Answer
1



I believe what you need to do is actually animate the layout being updated. More specifically something like this


if finalHeight >= mediumY
self.heightConstraintConstant = self.maxHeight
else
self.heightConstraintConstant = self.minHeight

UIView.animate(withDuration: 0.5)
viewThatTheConstraintBelongsTo.layoutIfNeeded()






Yeah that works...but the problem of the view going down remains...

– Lorenzo Santini
Sep 14 '18 at 19:38






viewThatTheConstraintBelongsTo would be the superview.

– Rakesha Shastri
Sep 16 '18 at 12:07



viewThatTheConstraintBelongsTo


superview



Thanks for contributing an answer to Stack Overflow!



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế