我有一个名为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中,我执行了如下动画:
-(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];
这段代码还可以。我可以初始化并将Subview添加到ParentViewController中。但每当我想执行有关CustomView的动画时。我在ParentViewController.m:中调用以下代码
[customView executeAnimation];
父视图中没有任何更改。有人知道在ParentViewController上执行这个动画的方法吗?
提前感谢。
如果您真的想使用+[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;
}];
}