2012-08-13 101 views
1

在我的应用程序中,我有一个滚动视图,当我按下按钮时,它被隐藏,当我再次按下它时出现。我想用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]; 

这解决了我的问题......

回答

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

变化框架,阿尔法和其他一些参数会自动动画。

+0

谢谢。这正是我正在寻找的。 – death7eater 2012-08-13 13:47:26

+0

如果我在代码中使用类似这样的东西,这个解决方案对我来说不适用于Textfield:'[_nameOfSandwich setText:@“”];'。由于该行,textfield不会移动。任何想法如何解决它? – Segev 2012-12-12 20:20:06

+0

看起来就像你解释你所看到的错误,或者在代码中有一些愚蠢的错误,上面的错误可以肯定地工作。 – 2012-12-13 06:16:21

3

你可以用动画像这样: -

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

如果你想滚动视图或关闭屏幕中将其y位置滑动 - 视图的底部,你想隐藏它,正常y位置,如果你想显示它。

2.0是动画长度,这可以更改为任何你需要它!

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秒内执行滚动视图的这些更改(在我的示例中)。

+0

它像一个魅力工作。谢谢,但唯一的想法,我没有弄清楚例如我有一个水平滚动视图在页面底部(x,y,w,h)(0,380,320,80)当我运行它scrollview幻灯片关闭从屏幕的底部(很棒!),但是当我再次按下我的按钮时,我希望它回到确切的位置,然后再将它移开。但是,在此代码中,我的滚动视图移至主视图的顶部。你有什么想法我该怎么做? – death7eater 2012-08-13 13:27:42

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