简体   繁体   English

在UIView上进行平滑的帧动画

[英]Smooth frame animating on UIView

I am trying to move a UIView around the screen by incrementing the UIView's x property in an animation block. 我试图通过在动画块中增加UIView的x属性来在屏幕上移动UIView。 I want the element to move continuously so I cannot just specify an ending x and up the duration. 我希望元素连续移动,所以我不能仅指定结尾x和持续时间。

This code works but it is very choppy. 这段代码有效,但是非常混乱。 Looks great in the simulator but choppy on the device. 在模拟器中看起来不错,但设备上却断断续续。

-(void)moveGreyDocumentRight:(UIImageView*)greyFolderView
{
[UIView animateWithDuration:0.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    NSInteger newX = greyFolderView.frame.origin.x + 5.0;
    greyFolderView.frame = CGRectMake(newX, greyFolderView.frame.origin.y, greyFolderView.frame.size.width, greyFolderView.frame.size.height);
    }
} completion:^(BOOL finished) {
    [self moveGreyDocumentRight:greyFolderView];
}];

} }

You're fighting the view animation here. 您在这里与视图动画作斗争。 Each one of your animations includes a UIViewAnimationOptionCurveEaseInOut timing curve. 您的每个动画都包含一个UIViewAnimationOptionCurveEaseInOut时序曲线。 That means that every 0.05 seconds you try to ramp up your speed then slow down your speed then change to somewhere else. 这意味着您每隔0.05秒就会尝试提高速度,然后放慢速度,然后再切换到其他位置。

The first and simplest solution is likely to change to a linear timing by passing the option UIViewAnimationOptionCurveLinear . 第一个也是最简单的解决方案可能是通过传递选项UIViewAnimationOptionCurveLinear更改为线性时序。

That said, making a new animation every 5ms really fights the point of Core Animation, complicating the code and hurting performance. 也就是说,每隔5ms制作一个新动画确实符合Core Animation的观点,使代码复杂化并损害了性能。 Send the frame it to the place you currently want it to go. 将其发送到您当前要放置的位置。 Whenever you want it to go somewhere else (even if it's still animating), send it to the new place passing the option UIViewAnimationOptionBeginFromCurrentState . 每当您希望它移到其他地方(即使它仍在设置动画)时,请通过选项UIViewAnimationOptionBeginFromCurrentState将其发送到新位置。 It will automatically adjust to the new target. 它将自动调整为新目标。 If you want it to repeat the animation or bounce back and forth, use the repeating options ( UIViewAnimationOptionRepeat and UIViewAnimationOptionAutoreverse ). 如果要重复动画或来回弹跳,请使用重复选项( UIViewAnimationOptionRepeatUIViewAnimationOptionAutoreverse )。

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

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