执行子视图子类的动画

时间:2014-10-17 09:33:43

标签: ios objective-c animation uiview

我有一个名为ParentViewController的UIViewController。 我有一个名为CustomView的UIView自定义类。它包括一些ImageView和执行动画的功能。

CustomView.h

@interface CustomView : UIView
@property (weak, nonatomic) IBOutlet UIImageView *human;
@property (weak, nonatomic) IBOutlet UIImageView *shadow;
+ (id)CustomView;
- (void)executeAnimation;
@end

在CustomView.m中,我按照以下方式执行executeAnimation:

-(void)executeAnimation{
    self.animation1InProgress = YES;
    [UIView animateKeyframesWithDuration:3.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
        self.human.frame = CGRectMake(self.human.frame.origin.x, self.human.frame.origin.y + 300, self.human.frame.size.width, self.human.frame.size.height);
    } completion:^(BOOL finished){
        self.animation1InProgress = NO;
    }];
}

现在在ParentViewController.m中,我添加了没有任何动画的CustomView

//init custom
customView = [CustomView initCustomView];
[self.view addSubview:centerLocationView];

这段代码没问题。我可以初始化并将子视图添加到ParentViewController。 但每当我想执行关于CustomView的动画时。我在ParentViewController.m中调用以下编码:

[customView executeAnimation];

父视图中没有任何变化。 有人知道在ParentViewController上执行这个动画的方法吗?

提前感谢。

1 个答案:

答案 0 :(得分:1)

如果您确实想使用+[UIView animateKeyframesWithDuration:delay:options:animations:completion:],则应将关键帧添加到animations块:

-(void)executeAnimation{
    self.animation1InProgress = YES;
    [UIView animateKeyframesWithDuration:3.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:1.0 animations:^{
            self.human.frame = CGRectMake(self.human.frame.origin.x, self.human.frame.origin.y + 300, self.human.frame.size.width, self.human.frame.size.height);
        }];
    } completion:^(BOOL finished){
        self.animation1InProgress = NO;
    }];
}

否则,只需使用[UIView animateWithDuration:animations:completion:]

-(void)executeAnimation{
    self.animation1InProgress = YES;
    [UIView animateWithDuration:3.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
        self.human.frame = CGRectMake(self.human.frame.origin.x, self.human.frame.origin.y + 300, self.human.frame.size.width, self.human.frame.size.height);
    } completion:^(BOOL finished){
        self.animation1InProgress = NO;
    }];
}