UIImageView animation not working correctly

时光怂恿深爱的人放手 提交于 2019-12-25 01:40:45

问题


I want to make the width of an UIImageView larger through an animation. I have an UIView with and UIImageView made in the story board. What I want to achieve is so that it looks like the view is scaling from left to right, and keeping its original position. I have tried the following code, but what it does is animating the imageview from the top left corner of its parent view and moving and scaling it until it reaches the position and scale it had from the beginning.

[UIView animateWithDuration:4.0f delay:1.0f options:UIViewAnimationCurveEaseOut animations:^{
    CGRect currentFrame = self.imageview1.frame;
    currentFrame.size.width = 150;
    self.imageview1.frame = currentFrame;
}completion:nil];

Any ideas why this behaviour?


回答1:


A bit late answer, but I solved the problem. It seems that you cannot change the frame of views in code if autolayout is enabled. Instead you need to animate the constraints of the view, which you can set up in the storyboard and then animate like this:

[self.view layoutIfNeeded];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

    self.bottomViewY.constant = -196;

    [self.view layoutIfNeeded];

} completion:nil];

self.bottomViewY is a constraint which is assigned as an IBOulet. You need to call layoutIfNeeded as it will update the constraints.



来源:https://stackoverflow.com/questions/12881513/uiimageview-animation-not-working-correctly

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