Is it possible to animate UIEdgeInsets?

…衆ロ難τιáo~ 提交于 2019-12-22 08:11:21

问题


I've been trying to animate a change in UIEdgeInsets. I've already tried using UIView.animateWithDuration and the insets just jump to the final values. Does anyone know if this a property that can't be animated? Or better yet, is anyone aware of a way to animate insets?

As an example:

// Starting inset values
self.searchField.placeHolderInset = UIEdgeInsets(top: 0, left: 110, bottom: 0, right: 10)

// Animation block
UIView.animateWithDuration(0.1, animations: {
        self.searchField.placeHolderInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
})

Instead of moving 92 pixels to the left over 0.1 seconds, the text jumps to the final value. searchField is a UITextField and .placeHolderInset is the UIEdgeInset being used to determine the insets for the UITextField's placeholder text.

EDIT: Another way to achieve the desired effect is by changing the UITextField's .textAlignment from .Center to .Left but that won't animate either.


回答1:


You need to call layoutIfNeeded() on the view, inside of the animation block.



来源:https://stackoverflow.com/questions/30136973/is-it-possible-to-animate-uiedgeinsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!