[英]Execute animation of subview subclass
我有一個名為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.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;
}];
}
現在在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;
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.