使用动画隐藏和显示scrollview

时间:2012-08-13 12:46:41

标签: objective-c ios animation uikit scrollview

在我的应用程序中,我有一个滚动视图,当我按下按钮时,当我再次按下它时,它会被隐藏。

我使用scrollview.hidden = YES(或NO)来做。

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

编辑:

[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];

这解决了我的问题...

4 个答案:

答案 0 :(得分:5)

您可以查看UIView animations。这很容易。例如,为了翻译动画,您可以使用以下内容:

[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和其他一些参数的变化将自动生成动画。

答案 1 :(得分:3)

你可以使用这样的动画: -

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

如果要在屏幕上打开或关闭滚动视图,请设置其y位置 - 视图的底部是否要隐藏它,如果要显示,则为正常的y位置。

2.0是一个动画长度,可以改为你需要的任何东西!

答案 2 :(得分:1)

您可以使用简单的视图动画来完成此操作。以下是如何执行此操作的示例:

// 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位置将设置为父视图的底部。使用带有块的新学校UIView动画API,我们可以在1秒钟内对滚动视图执行这些更改(在我的示例中)。

答案 3 :(得分:1)

ScrollView通过动画从屏幕下方隐藏 。我认为这个动画就是你想要的。

// 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;
}