我有一个代表fav按钮的按钮。 我希望以1.0秒的延迟向上动画,然后在点击一下时以0.2秒的延迟向下动画。 我做了两种不同的方法来做向上和向下动画。 但是,当我实际运行代码时,两个动画同时提交,显示相反的顺序。我想首先动画向上动画然后向下动画动画。 这是代码。
-(void)starButtonUpAnimation{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGRect frame = self.starButton.frame;
frame.origin.y = frame.origin.y - frame.size.height;
self.starButton.frame = frame;
[UIView commitAnimations];
}
-(void)starButtonDownAnimation{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
CGRect frame1 = self.starButton.frame;
frame1.origin.y = frame1.origin.y + frame1.size.height;
self.starButton.frame = frame1;
[UIView commitAnimations];
}
按钮动作方法,我调用上面的方法。
- (IBAction)clickedStarButton:(id)sender {
[self starButtonUpAnimation];
[self changeStarButtonImage];
[self starButtonDownAnimation];
}
答案 0 :(得分:3)
-(void)starButtonUpAnimation{
CGRect frame = self.starButton.frame;
frame.origin.y = frame.origin.y - frame.size.height;
self.starButton.frame = frame;
}
-(void)starButtonDownAnimation{
CGRect frame1 = self.starButton.frame;
frame1.origin.y = frame1.origin.y + frame1.size.height;
self.starButton.frame = frame1;
}
- (IBAction)clickedStarButton:(id)sender {
[UIView animateWithDuration:2.0 animations:^{
[self starButtonUpAnimation];
} completion:^(BOOL finished) {
[self changeStarButtonImage];
[UIView animateWithDuration:0.1 animations:^{
[self starButtonDownAnimation];
} completion:^(BOOL finished) {
// clean up
}];
}];
}