1

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

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

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

编辑:

[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

4 回答 4

5

你可以检查 UIView动画。这很容易。例如,要使翻译动画化,您可以使用以下内容:

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

于 2012-08-13T12:50:41.810 回答
3

您可以像这样使用动画:-

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

如果你想在屏幕上滑动滚动视图,设置它的 y 位置 - 视图的底部是你想隐藏它,如果你想显示它是正常的 y 位置。

2.0 是动画长度,可以随意更改!

于 2012-08-13T12:51:04.780 回答
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 秒内对滚动视图执行这些更改(在我的示例中)。

于 2012-08-13T12:54:12.697 回答
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;
}
于 2012-08-13T12:57:19.557 回答