Moving UIButton position, animated

2019-08-11 09:35发布

I have a UIButton, which when it's pressed I want to move the position of it.

I've managed to do this by this code:

ans2.frame = CGRectOffset(ans2.frame, 0, 20);

However this just moves the button instantly.

I've also tried just moving it by doing ++ to the position until the new one is reached, however this makes it look like the "animation" lags.

Would it be possible to move the button with a smooth animation?

1条回答
看我几分像从前
2楼-- · 2019-08-11 10:28

UIKit provides several ways to perform animations. The simplest way in your case would be this:

[UIView animateWithDuration:1.0 animations:^{
   ans2.frame = CGRectOffset(ans2.frame, 0, 20);
}];

However, there are plenty of other possibilities to animate. Checkout Apple's Animation Guide.

查看更多
登录 后发表回答