使用animation隐藏和显示滚动视图

在我的应用程序,我有一个滚动视图,当我按下一个button时,它是隐藏的,当我再次按下它出现。

我使用scrollview.hidden = YES (或NO)来做到这一点。

但我想用animation做。 例如,它可能会通过移动而从屏幕底部消失,并以相同的方式显示出来。 我怎样才能做到这一点?

编辑:

 [UIView beginAnimations:@"myAnimation" context:nil]; CGRect Frame = bottomScroller.frame; if(Frame.origin.y == 380){ Frame.origin.y = 460; }else{ Frame.origin.y = 380; } bottomScroller.frame = Frame; [UIView commitAnimations]; 

这解决了我的问题…

你可以检查UIView animation 。 这很容易。 例如,为了翻译animation,你可以使用如下的东西:

 [UIView beginAnimations:@"myAnimation" context:nil]; CGRect Frame = yourView.frame; Frame.origin.y = 0; yourView.frame = Frame; [UIView commitAnimations]; 

然后将其移回:

 [UIView beginAnimations:@"myAnimation" context:nil]; CGRect Frame = yourView.frame; Frame.origin.y = 100; yourView.frame = Frame; [UIView commitAnimations]; 

帧,alpha和其他一些参数的变化将自动生成animation。

你可以像这样使用animation:

 [UIView animateWithDuration:2.0 animations:^{ [scrollview setframe:CGRectMake(xpos, ypos, width, height)]; }]; 

如果要在屏幕上或屏幕上滑动滚动视图,请设置其y位置 – 视图的底部是要隐藏的位置,如果要显示,则显示正常的y位置。

2.0是一个animation长度,这可以改变为任何你需要它!

你可以通过使用简单的视图animation来做到这一点。 以下是您如何做到这一点的例子:

 // myScrollView is your scroll view -(void)toggleShow{ CGFloat targetAlpha = myScrollView.alpha == 1 ? 0 : 1; CGFloat yPosition = targetAlpha == 1 ? 0 : self.view.frame.size.height; [UIView animateWithDuration:1.0 animations:^{ myScrollView.frame = CGRectMake(myScrollView.frame.origin.x, yPosition, myScrollView.frame.size.width, myScrollView.frame.size.height); myScrollView.alpha = targetAlpha; }]; } 

targetAlpha将始终与当前状态(1或0)相反,如果为0,则y位置将设置为父视图的底部。 使用带有块的新派UIViewanimationAPI,我们可以在1秒内执行滚动视图的更改(在我的示例中)。

ScrollView 从屏幕下方用animation呈现 。 我认为这个animation是你想要的。

 // ScrollView appearing animation - (void)ScrollViewUpAnimation { // put the scroll view out of screen CGRect frame = ScrollView.frame; [self.view addSubview:ScrollView]; ScrollView.frame = CGRectMake(0, 460, frame.size.width, frame.size.height); // setting for animation CGPoint fromPt = ScrollView.layer.position; CGPoint toPt = CGPointMake(fromPt.x, fromPt.y - frame.size.height - 44); CABasicAnimation* anime = [CABasicAnimation animationWithKeyPath:@"position"]; anime.duration = 0.2; anime.fromValue = [NSValue valueWithCGPoint:fromPt]; anime.toValue = [NSValue valueWithCGPoint:toPt]; // change the position of Scroll View when animation start [ScrollView.layer addAnimation:anime forKey:@"animatePosition"]; ScrollView.layer.position = toPt; }