ios兩個子視圖同一時間進入和離開動畫
在一個視圖控制器中,加入了兩個視圖targetSubview和sourceSubview。targetSubview默認是隱藏的,sourceSubview默認是可見的。
現在,我有這樣一個需求。通過手指下滑,將targetSubview從上向下拉入到屏幕中,同時,sourceSubview將隨着從屏幕下方離開。兩個視圖同步進入和離開是同步操作,用動畫來實現。
第一種方法,使用UIView的類方法transition來實現,
[UIView transitionFromView:sourceSubview toView:targetSubview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
但,只能實現上下或左右翻轉的效果,沒有找到上下滑入滑出的效果。而且,該方法還將sourceSubview給remove掉了。
因此,這種方法不能滿足要求。
第二種方法,使用使用UIView的類方法Animations來實現,是用delegate做一些前置或後置操作。
targetSubview.frame = CGRectMake(0, targetSubview.frame.size.height + 50, targetSubview.frame.size.width, targetSubview.frame.size.height);
[UIView beginAnimations:@"Anim1" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
targetSubview.hidden = NO;
targetSubview.frame = CGRectMake(0, 0, targetSubview.frame.size.width, targetSubview.frame.size.height);
[UIView commitAnimations];
[UIView beginAnimations:@"Anim2" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
sourceSubview.frame = CGRectMake(0, -50, sourceSubview.frame.size.width, sourceSubview.frame.size.height);
sourceSubview.hidden = YES;
[UIView commitAnimations];
//動畫結束後執行
//sourceSubview.hidden = YES;
這個方法實現了targetSubview視圖的動畫方式緩緩進入,但sourceSubview立即消失,沒有動畫效果。
因此在第二個動畫設置中,我將sourceSubview.hidden設置爲了yes,如果不設置會有動畫效果,但是sourceSubview視圖因爲過高會有部分遺留在屏幕上。
即使,在動畫結束後設置也該屬性,也不行。
這個動畫的效果,實際上是在後臺執行的,類似多線程方式在運行。動畫運行時間設置爲1秒,但最後程序不會等待的,因此,這裏需要設置delegate才能實現這個效果了。在動畫結束後,delegate方法+ (void)setAnimationDidStopSelector:(SEL)selector必須實現,在該方法中調用sourceSubview.hidden = YES;才行。
在動畫開始前,也有一個delegate方法+ (void)setAnimationWillStartSelector:(SEL)selector,可以做一些設置。
第三種方法,還是使用使用UIView的類方法Animations來實現,但是是用block實現,需要在ios4.0以上才能執行。現在蘋果都將ios版本都升級到7.0,老版本的用戶越來越少了。
targetSubview.alpha=0.0;
targetSubview.hidden = NO;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
targetSubview.frame = CGRectMake(0, -50, targetSubview.frame.size.width, targetSubview.frame.size.height);
targetSubview.alpha=1.0;
targetSubview.frame = CGRectMake(0, 0, targetSubview.frame.size.width, targetSubview.frame.size.height);
sourceSubview.frame = CGRectMake(0, sourceSubview.frame.size.height, sourceSubview.frame.size.width, sourceSubview.frame.size.height);
sourceSubview.alpha=0.0;
} completion:^(BOOL finished) {
[self printSubviews];
sourceSubview.hidden = YES;
sourceSubview.alpha=1.0;
}];
最後,使用這個簡單點的方法實現了需求。
在動畫結束後,將sourceSubview設置爲隱藏。
UIView中可以設置動畫效果的屬性:
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
現在,我有這樣一個需求。通過手指下滑,將targetSubview從上向下拉入到屏幕中,同時,sourceSubview將隨着從屏幕下方離開。兩個視圖同步進入和離開是同步操作,用動畫來實現。
第一種方法,使用UIView的類方法transition來實現,
[UIView transitionFromView:sourceSubview toView:targetSubview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
但,只能實現上下或左右翻轉的效果,沒有找到上下滑入滑出的效果。而且,該方法還將sourceSubview給remove掉了。
因此,這種方法不能滿足要求。
第二種方法,使用使用UIView的類方法Animations來實現,是用delegate做一些前置或後置操作。
targetSubview.frame = CGRectMake(0, targetSubview.frame.size.height + 50, targetSubview.frame.size.width, targetSubview.frame.size.height);
[UIView beginAnimations:@"Anim1" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
targetSubview.hidden = NO;
targetSubview.frame = CGRectMake(0, 0, targetSubview.frame.size.width, targetSubview.frame.size.height);
[UIView commitAnimations];
[UIView beginAnimations:@"Anim2" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
sourceSubview.frame = CGRectMake(0, -50, sourceSubview.frame.size.width, sourceSubview.frame.size.height);
sourceSubview.hidden = YES;
[UIView commitAnimations];
//動畫結束後執行
//sourceSubview.hidden = YES;
這個方法實現了targetSubview視圖的動畫方式緩緩進入,但sourceSubview立即消失,沒有動畫效果。
因此在第二個動畫設置中,我將sourceSubview.hidden設置爲了yes,如果不設置會有動畫效果,但是sourceSubview視圖因爲過高會有部分遺留在屏幕上。
即使,在動畫結束後設置也該屬性,也不行。
這個動畫的效果,實際上是在後臺執行的,類似多線程方式在運行。動畫運行時間設置爲1秒,但最後程序不會等待的,因此,這裏需要設置delegate才能實現這個效果了。在動畫結束後,delegate方法+ (void)setAnimationDidStopSelector:(SEL)selector必須實現,在該方法中調用sourceSubview.hidden = YES;才行。
在動畫開始前,也有一個delegate方法+ (void)setAnimationWillStartSelector:(SEL)selector,可以做一些設置。
第三種方法,還是使用使用UIView的類方法Animations來實現,但是是用block實現,需要在ios4.0以上才能執行。現在蘋果都將ios版本都升級到7.0,老版本的用戶越來越少了。
targetSubview.alpha=0.0;
targetSubview.hidden = NO;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
targetSubview.frame = CGRectMake(0, -50, targetSubview.frame.size.width, targetSubview.frame.size.height);
targetSubview.alpha=1.0;
targetSubview.frame = CGRectMake(0, 0, targetSubview.frame.size.width, targetSubview.frame.size.height);
sourceSubview.frame = CGRectMake(0, sourceSubview.frame.size.height, sourceSubview.frame.size.width, sourceSubview.frame.size.height);
sourceSubview.alpha=0.0;
} completion:^(BOOL finished) {
[self printSubviews];
sourceSubview.hidden = YES;
sourceSubview.alpha=1.0;
}];
最後,使用這個簡單點的方法實現了需求。
在動畫結束後,將sourceSubview設置爲隱藏。
UIView中可以設置動畫效果的屬性:
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.