【问题标题】:UIimageView lose frame origin position after set transformUIimageView在设置变换后丢失帧原点位置
【发布时间】:2013-03-13 00:12:53
【问题描述】:

我有一个带有人脸图像的 UIImageView,然后我将旋转变换动画应用到该 UIImageView。

 self.transform = CGAffineTransformRotate(self.transform, M_PI / 16);

[UIView animateWithDuration:0.5
                  delay:0.0
                options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
             animations:^{

                 self.transform = CGAffineTransformRotate(self.transform, -1 * (M_PI/8));

             }
             completion:^(BOOL completed) {}];

然后我为同一张人脸图像制作了另一个动画,它在 y 轴上上下移动。

[UIView animateWithDuration:0.2
                      delay:0.0
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
                 animations:^{

                     self.transform = CGAffineTransformMakeTranslation(0, 10);
                 }
                 completion:^(BOOL completed){}];

执行第一个动画后,我需要更改 UIImageView 位置,然后启动第二个动画。

我有一个 stopAnimation 方法,它可以删除动画、重置变换并设置 UIImageView 的位置。

-(void)stopAnimation:(NSInteger)currentAnimation{

    [self.layer removeAllAnimations];

    [self setTransform:CGAffineTransformIdentity];
    [self setFrame:initFrame];

    CGPoint position = [self getNextPosition:currentAnimation];
    //[self setTransform:CGAffineTransformMakeTranslation(0, 0)];
    CGRect frame = self.frame;
    frame.origin.x = position.x;
    frame.origin.y = position.y;
    self.frame = frame;
}

-(CGPoint)getNextPosition:(NSInteger)currentAnimation{

    CGPoint position = [[positions objectAtIndex:(currentAnimation-1)] CGPointValue];
    NSLog(@"Current animation:%u %f:%f", currentAnimation,position.x,position.y);
    return position;
}

如果我在没有动画的情况下执行位置更改,则 UIImageView 位于屏幕的正确位置,但如果我使用动画运行,则动画效果很好,但 UIImageView 位置错误。

我尝试对 UIImageView 的中心进行相同的设置,但我遇到了同样的问题。

【问题讨论】:

    标签: ios uiview uiimageview


    【解决方案1】:

    尝试将第二个动画偏移第一个动画的持续时间。

    [UIView animateWithDuration:0.2
                          delay:0.51 //delay by the duration of the first animation
                        options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
                     animations:^{
                         self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeTranslation(0, 10));
                     }
                     completion:^(BOOL completed){}];
    

    您也可以尝试将第二个动画块放在第一个动画的完成块中。

    【讨论】:

    • 但是,我可以控制动画运行的时间,在执行动画后我也可以删除AllAnimations。
    • stopAnimation 在哪里被调用?因为我不知道你是如何工作的,所以一些一般注意事项: UIView 动画是异步的,因此动画块下方的代码将在动画完成之前被调用。此外,将变换设置为平移将删除旋转。
    • 应用程序从第一个动画开始,然后当用户点击 stopAnimation 时调用,然后调用第二个动画。
    • 查看这个post,它说调用 [self.layer removeAllAnimations] 在 iOS 5+ 中可能不会立即生效。另请参阅其他答案,即 UIViewAnimationOptionBeginFromCurrentState。我显然误解了你的代码,对不起。 :)
    猜你喜欢
    • 2011-10-06
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多