2013-07-06 61 views
0

情况:我有一个排序工具栏(不是UIToolbar,只是一个自定义的UIView),位于我的视图顶部。这个工具栏有一些按钮。紧接着它,我有一个窗体的滚动视图。当轻击工具栏上的按钮时,相应的窗体应该从右侧或左侧在屏幕上动画,具体取决于按下哪个按钮。我不希望工具栏在此上移动。不过,当用户在滚动视图上向上或向下滚动时,我确实希望工具栏向上滚动。我现在已经完成了它的方式是在scrollViewDidScroll委托方法(注意,我只允许水平滚动,如果从按下工具栏上的按钮的):UIScrollView策略水平和垂直滚动

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if(scrollView.contentOffset.x != 0 && _shouldScrollHorizontally == NO) 
    { 
     CGPoint offset = scrollView.contentOffset; 
     offset.x = 0; 
     scrollView.contentOffset = offset; 
    } 

    CGRect buttonFrame = [_buttons frame]; 

    buttonFrame.origin.y = -scrollView.contentOffset.y + 10; 

    [_buttons setFrame:buttonFrame]; 
} 

不过,我担心这是低效和会造成一些延迟。我想知道是否有更好的方法来实现我想要实现的目标。

在一天结束时,如果滚动视图水平滚动,我希望工具栏保持静止,但如果滚动视图是垂直滚动的,则使用滚动视图移动。

感谢您的任何建议!

+0

你的意思是你想有一个图形像老脉冲,或postpiks。 – rptwsthi

+0

你是什么意思? – Mason

回答

1

由于水平“滚动”只有在轻按按钮时才允许,然后根据需要将宽度设置为屏幕宽度及其高度。为了在按钮水平上“水平滚动”,只需对水平移动的视图进行动画处理即可。

视图层次结构将类似于outerView,它是您将UIScrollView contentSize设置为当前的宽度和高度。您的滚动视图是outerView的子视图,工具栏是滚动视图的子视图。

设置动画的水平运动,它是最简单的典型改变outerView.centertoolbarView.center,是这样的:

CGPoint newCenterPoint; 
CGPoint newToolbarCenterPoint; 

CGPoint centerPoint1 = // set center as appropriate 
CGPoint centerPoint2 = // set center as appropriate 
CGPoint centerPoint3 = // set center as appropriate 

CGPoint toolbarCenter1 = // set center as appropriate 
CGPoint toolbarCenter2 = // set center as appropriate 
CGPoint toolbarCenter3 = // set center as appropriate 

if (buttonTapped == button1) { 
    newCenterPoint = centerPoint1; 
    newToolbarCenterPoint = toolbarCenter1; 
} else if (buttonTapped == button2){ 
    newCenterPoint = centerPoint2; 
    newToolbarCenterPoint = toolbarCenter2; 
} else if (buttonTapped == button3){ 
    newCenterPoint = centerPoint3; 
    newToolbarCenterPoint = toolbarCenter3; 
} 
[UIView animateWithDuration:0.25 animations:^{ 
    outerView.center = newCenterPoint; 
    toolbarView.center = newToolbarCenterPoint; 
}];