简体   繁体   English

执行子视图子类的动画

[英]Execute animation of subview subclass

I have a UIViewController called ParentViewController. 我有一个名为ParentViewController的UIViewController。 And i have a UIView custom class called CustomView. 我有一个名为CustomView的UIView自定义类。 It including some ImageView and the function to execute animation. 它包括一些ImageView和执行动画的功能。

CustomView.h CustomView.h

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

And in CustomView.mi ave executeAnimation as the following: 在CustomView.mi中,如下所示执行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;
    }];
}

Now in ParentViewController.m, i add CustomView without any animation 现在在ParentViewController.m中,我添加没有任何动画的CustomView

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

This code is OK. 这段代码可以。 I could init and addSubview to ParentViewController. 我可以初始化并添加Subview到ParentViewController。 But whenever i would like to execute animation about CustomView. 但是每当我想执行有关CustomView的动画时。 I call the following coding in ParentViewController.m: 我在ParentViewController.m中调用以下代码:

[customView executeAnimation];

There is nothing changed at Parent View. 父视图没有任何更改。 Is anybody know the way to execute this animation at ParentViewController? 有人知道在ParentViewController上执行此动画的方法吗?

Thank in advance. 预先感谢。

If you really want to use +[UIView animateKeyframesWithDuration:delay:options:animations:completion:] , you should add keyframes to your animations block: 如果您确实想使用+[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;
    }];
}

Otherwise, just use [UIView animateWithDuration:animations:completion:] : 否则,只需使用[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;
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM