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

时间:2011-08-16 15:51:26

标签: iphone objective-c animation uiimageview scaling

我正在缩放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);}}];

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

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

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

2 个答案:

答案 0 :(得分:4)

如果不使用UIViewAnimationOptionAutoreverse选项,

UIViewAnimationOptionRepeat什么都不做。写入的动画会将图像缩小到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];}}];

答案 1 :(得分:0)

.h文件declerd。

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;
                         }];
    }
}