简体   繁体   English

核心动画和UIImageView返回起始位置

[英]Core Animation and UIImageView returning to starting position

I have a UIImageView that I'm moving across the screen based on a user swiping. 我有一个UIImageView,可以根据用户滑动在屏幕上移动。 I accomplish this using Core Animation: 我使用Core Animation完成此操作:

 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1.0];

 movingView.frame = theRect;

 [UIView commitAnimations];

It always starts out fine, but partway through going from its original location to the "theRect" location, it stops and the UIImageView jumps back to its starting point. 它总是从开始就很好,但是在从其原始位置到“ theRect”位置的途中,它停止了,UIImageView跳回到了它的起点。 It's never at the same point. 永远不会在同一时间。 Sometimes gets almost all the way through before jumping back, sometimes only barely moving then jumping back. 有时候,在跳回去之前,几乎会一直走完,有时只是勉强移动然后跳回去。 No other code is running after this. 此后没有其他代码在运行。 I've tried changing the duration and it doesn't seem to matter. 我尝试更改持续时间,但这似乎无关紧要。

If I add in: 如果我添加:

 [UIView setAnimationDidStopSelector:@selector(completeAnimation:finished:context:)];
 [UIView setAnimationDelegate:self];

Then it actually delays until the Duration is complete and runs the selector, even if the image itself stops and returns to its starting point almost immediately. 然后,它实际上会延迟到“持续时间”完成并运行选择器,即使图像本身停止并几乎立即返回其起点也是如此。

Hopefully someone can give me some hints at what might be going on here. 希望有人可以给我一些提示,说明这里可能会发生什么。

If using autolayout, you can animate the changing of the constraint, not animating the change of the frame (because when constraints are reapplied, the original location will be restored). 如果使用自动布局,则可以对约束的变化进行动画处理,而不是对框架的变化进行动画处理(因为重新应用约束时,将恢复原始位置)。 See Animating an image view to slide upwards for an example. 有关示例,请参见动画化图像视图以向上滑动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM