如何使用 UIView animateWithDuration 正确缩放 UIImageView?

发布于 2024-11-30 00:42:54 字数 647 浏览 1 评论 0原文

我正在缩放 UIImageView,然后使用 UIView animateWithDuration 和 UIViewAnimationOptionAutoreverse 选项恢复它。问题是图像动画在动画结束时有点锯齿(抽搐)。这是我的代码:

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionAutoreverse 
  animations:^{

    myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}  

  completion:^(BOOL finished){if (finished){

    myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}}];

我知道我错过了一些东西,但不知道从哪里开始:(

更新1:我正在执行嵌套的 6 个动画,其中每个下一个动画在上一个动画之后执行。为此,我使用块动画并执行完整块中的每个下一个动画

:我已尝试使用 UIViewAnimationOptionRepeat 选项,但每个缩放动画后仍然有一些闪光效果。

I'm scaling UIImageView and then reverting it using UIView animateWithDuration with UIViewAnimationOptionAutoreverse option. The problem is the image animation is a little bit jaggy (twitching) at the end of the animation. Here's my code:

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionAutoreverse 
  animations:^{

    myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}  

  completion:^(BOOL finished){if (finished){

    myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}}];

I know that I'm missing something but don't know where to start :(

UPDATE1: I'm performing nested 6 animations where each next animation performs after previous one. For that I'm using block animations and performing each next animation in complete block.

UPDATE2: I have tried with UIViewAnimationOptionRepeat option but there's still some flash effect after each scale animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-12-07 00:42:54

如果不使用 UIViewAnimationOptionRepeat 选项,UIViewAnimationOptionAutoreverse 不会执行任何操作。您编写的动画会将图像缩小到 90%,然后在完成块中恢复到 100%。也许您可以在这个动画之后播放另一个在四分之一秒内缩小到 100% 的动画。像这样的东西:

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
                 animations:^{
                     myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}  
                 completion:^(BOOL finished){if (finished){

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
                     animations:^{
                         myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}  
                     completion:NULL];}}];

UIViewAnimationOptionAutoreverse does nothing without also using the UIViewAnimationOptionRepeat option. Your animation as written will shrink the image to 90%, then snap back to 100% in the completion block. Maybe you can follow this animation with another that scales back to 100% over a quarter second. Something like this:

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
                 animations:^{
                     myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}  
                 completion:^(BOOL finished){if (finished){

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
                     animations:^{
                         myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}  
                     completion:NULL];}}];
耳根太软 2024-12-07 00:42:54

.h 文件解密。

NSInteger imgint;

代码中的 .m 文件并尝试。

-(void) 
{
    [NSTimer scheduledTimerWithTimeInterval:(0.85f)target:self selector:@selector(play_btn_animation) userInfo:nil repeats:YES];   
}

-(void)play_btn_animation
{
    if(imgint == 0)
    {
        [UIView animateWithDuration:0.8
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
                         }
                         completion:^(BOOL finished) {
                             img = 1;
                         }];

    }
    else
    {
        [UIView animateWithDuration:0.8
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
                         }
                         completion:^(BOOL finished) {
                             imgint = 0;
                         }];
    }
}

.h file declerd.

NSInteger imgint;

.m file in code and try.

-(void) 
{
    [NSTimer scheduledTimerWithTimeInterval:(0.85f)target:self selector:@selector(play_btn_animation) userInfo:nil repeats:YES];   
}

-(void)play_btn_animation
{
    if(imgint == 0)
    {
        [UIView animateWithDuration:0.8
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
                         }
                         completion:^(BOOL finished) {
                             img = 1;
                         }];

    }
    else
    {
        [UIView animateWithDuration:0.8
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
                         }
                         completion:^(BOOL finished) {
                             imgint = 0;
                         }];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文