简体   繁体   中英

Execute animation of subview subclass

I have a UIViewController called ParentViewController. And i have a UIView custom class called CustomView. It including some ImageView and the function to execute animation.

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:

-(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

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

This code is OK. I could init and addSubview to ParentViewController. But whenever i would like to execute animation about CustomView. I call the following coding in ParentViewController.m:

[customView executeAnimation];

There is nothing changed at Parent View. Is anybody know the way to execute this animation at ParentViewController?

Thank in advance.

If you really want to use +[UIView animateKeyframesWithDuration:delay:options:animations:completion:] , you should add keyframes to your animations block:

-(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:] :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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