2014-10-17 40 views
0

我有一個名爲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 AVE 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]; 

這段代碼沒問題。我可以init和addSubview ParentViewController。 但是,每當我想執行有關CustomView的動畫。我在ParentViewController.m中調用以下代碼:

[customView executeAnimation]; 

在父視圖中沒有任何更改。 有人知道在ParentViewController上執行這個動畫的方法嗎?

提前致謝。

回答

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; 
    }]; 
} 
+0

感謝的快速反應。我試過了,ParentView可以看到執行的動畫。非常感謝你。不過,請你爲我解釋一下addKeyframeWithRelativeStartTime和relativeDuration嗎? – 2014-10-17 10:04:42

+0

另外,在這種情況下,執行動畫完成後。 human.frame被複位到原始位置。它應該是self.human.frame.origin.y + 300.但現在不是 – 2014-10-17 10:14:46

+0

當你更新'動畫'塊內的動畫屬性時,你實際上更新動畫層,而不是表示層。您需要再次將'self.human.frame'設置爲正確的值,但不能在'動畫'塊內。查看這篇文章的詳細信息:[動畫解釋](http://www.objc.io/issue-12/animations-explained.html) – 2014-10-17 10:47:38