[英]Trying to animate a UIView height from the bottom up, instead of the top down
[英]Animate UIView height from bottom to top
我正在做一個UIView高度的簡單動畫,以便它顯示出來。
默認情況下,它似乎從上到下顯示,我希望它從下到上顯示。
我把UIView固定在屏幕的底部。
我確定它很簡單,我很遺憾.....任何提示?
謝謝
我真的認為實現這一目標的最簡單方法是為視圖的高度和y屬性設置動畫。 如果它們沿着相同的曲線發生,它應該看起來完全無縫到用戶。 當您將高度設置為0時,也會將y分量設置為原始y +原始高度的動畫。
UIView *view = ...;
float originalY = view.frame.origin.y;
float originalH = view.bounds.size.height;
[UIView animateWithDuration:1.2f delay:1.0f options:UIViewAnimationCurveEaseInOut animations:^{
view.frame = CGRectMake(view.frame.origin.x, (originalY + originalH), view.bounds.size.width, 0);
}completion:^(BOOL finished) {
NSLog(@"Animation is complete");
}];
我相信這會給出一個倒塌視圖的外觀和感覺。 我沒有在代碼中嘗試過這個,但我認為沒有理由不這樣做。
隱藏在底部
[self animateViewHeight:myView withAnimationType:kCATransitionFromBottom];
用於反向動畫
[self animateViewHeight:myView withAnimationType:kCATransitionFromTop];
...
- (void)animateViewHeight:(UIView*)animateView withAnimationType:(NSString*)animType {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:animType];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[animateView layer] addAnimation:animation forKey:kCATransition];
animateView.hidden = !animateView.hidden;
}
就像一只帶骨頭的狗我想出來了......
我沒有動畫幀高度,而是將變換應用於視圖並設置圖層的錨點。
//set the anchor point to the bottom of the view
[self setAnchorPoint:CGPointMake(0.5, 1.0) forView:hostView];
//Scale the height to close to zero
hostView.transform = CGAffineTransformMakeScale(1, 0.00001);
如果我將0作為y比例,則視圖表現得很奇怪....在動畫結束時我只是將其設置為隱藏。
在備份的路上我只使用身份轉換(重置它)
hostView.transform = CGAffineTransformIdentity;
請注意,更改我的錨點會改變我的視圖位置。 有關setAnchorPoint方法的信息,請參閱此文章,該方法在設置anchorPoint后對視圖進行規范化
相反,您可以嘗試將其放在視圖中,使用clipsToBounds = YES
,然后從視圖的底部到中間設置動畫,如下所示:
viewToAnimate.frame = CGRectMake(viewToAnimate.frame.origin.x,
viewToAnimate.superview.frame.size.height,
viewToAnimate.frame.size.width,
viewToAnimate.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
viewToAnimate.center = viewToAnimate.superview.center;
}];
這樣,您不必將高度設置為0,並且它解決了視圖中自動調整的任何問題。
根據要求,這是我正在使用的代碼...我正在使用CAKeyFrameAnimation,這可能比您正在尋找的更多。 它可能與CABasicAnimation一樣,我只是向你展示這段代碼,因為我已經編寫了它。
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
springLayer = [[CALayer alloc] init];
springLayer.backgroundColor = [UIColor redColor].CGColor;
springLayer.anchorPoint = CGPointMake(0, 1);
springLayer.frame = CGRectMake(125, 285, 100, 115);
[springLayer setNeedsDisplay];
[self.layer addSublayer:springLayer];
[self test];
}
return self;
}
-(void)test {
CAKeyframeAnimation *heightAnim = [[CAKeyframeAnimation alloc] init];
heightAnim.duration = 3;
heightAnim.removedOnCompletion = NO;
heightAnim.fillMode = kCAFillModeForwards;
heightAnim.beginTime = CACurrentMediaTime() + 0.25;
NSMutableArray *v = [[NSMutableArray alloc] init];
NSMutableArray *t = [[NSMutableArray alloc] init];
float dest = 250;
float difference = 135;
while (difference > 1.0) {
[v addObject:[NSNumber numberWithFloat:dest-difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
[v addObject:[NSNumber numberWithFloat:dest+difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
}
heightAnim.values = v;
heightAnim.timingFunctions = t;
[springLayer addAnimation:heightAnim forKey:@"bounds.size.height"];
}
我使用AdWhirlView完成它的一種方法,將其隱藏在屏幕下方,然后將其設置為動畫;
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
adWhirlView.delegate = self;
adWhirlView.frame = CGRectMake(0, 430+kAdWhirlViewHeight, kAdWhirlViewWidth, kAdWhirlViewHeight);
[self.parentViewController.view insertSubview:adWhirlView belowSubview:self.view];
[UIView beginAnimations:@"AdWhirlIn" context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
adWhirlView.frame = CGRectMake(0, 430, kAdWhirlViewWidth, kAdWhirlViewHeight);
[UIView commitAnimations];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.