【问题标题】:Animation not working on UIImageView动画在 UIImageView 上不起作用
【发布时间】:2015-12-18 20:12:00
【问题描述】:

我添加了一个 UIImageView 作为子视图。我正在尝试使用变换对其进行动画处理。动画没有,但是图像视图被正确缩放,它不是自动反转。我做错了吗?

UIImageView * iv_tap = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gesture_tap"]];
    iv_tap.center = point;
    iv_tap.tag = 779;
    iv_tap.hidden = true;
    [UIView animateWithDuration:2.0
                          delay:.05
                        options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear
                     animations:^{
                         iv_tap.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.8, 1.8);
                     }
                     completion:NULL];


    [self addSubview:iv_tap];

【问题讨论】:

    标签: ios objective-c animation uiimageview uianimation


    【解决方案1】:

    您需要在将imageView 添加到view 后进行动画处理。如果你在 addSubView 之前做动画,那么图像视图不在视图层次结构中,你看不到任何动画。

     UIImageView * iv_tap = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gesture_tap"]];
    [self.view addSubview:iv_tap];   //this could be self in your case
    iv_tap.tag = 779; 
    
    [UIView animateWithDuration:2.0 delay:0.05  options:UIViewAnimationOptionCurveEaseOut animations:^{
    
        iv_tap.center=self.view.center;
        iv_tap.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.8, 1.8);
    
        [self.view layoutIfNeeded];    //this could be self in your case
    
    } completion:^(BOOL finished) {
            //do your stuff when animation is done
    }];
    

    如果你是一个 UIView 的子类,那么你甚至可以把它放在 drawRect 方法中:

    UIImageView * iv_tap = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gesture_tap"]];
    [self addSubview:iv_tap];   
    iv_tap.tag = 779;
    
    [UIView animateWithDuration:2.0 delay:0.05  options:UIViewAnimationOptionCurveEaseOut animations:^{
    
        iv_tap.center=self.center;
        iv_tap.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.8, 1.8);
    
        [self layoutIfNeeded];   
    
    } completion:^(BOOL finished) {
            //do your stuff when animation is done
     }];
    

    【讨论】:

    • 其实什么也没做。
    • 动画后调用layoutifneed
    • 我打电话给 [iv_taplayOUTIfNeeded] 也没用。
    • 更新了@Firemarble 的答案。这对我有用
    • 不,不是图像视图,如果需要,您需要更新 self.viewq 布局
    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 2019-08-06
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多