【问题标题】:Displaying a button at the bottom when scroll up向上滚动时在底部显示一个按钮
【发布时间】:2012-07-25 09:58:32
【问题描述】:
我是 iOS 新手,正在尝试开发基于新闻的应用程序。
在我的应用程序中,我有一个 Root View Controller,它是一个 Table View Controller,用于显示新闻标题和新闻图像。好吧,当用户点击表格单元格时,会显示另一个View Controller,它更详细且可滚动。
在View Controller中,用户可以在Facebook和Twitter上分享新闻,这样我至少有两个按钮。我想要做的是在用户向上滚动时显示这两个按钮。当用户向下滚动时,我想让它们消失。 Pulse News 中有类似的内容。我已经看过 Scroll View 委托方法,但没有成功。我怎样才能完成我想做的事情?
提前致谢。
【问题讨论】:
标签:
objective-c
uiviewcontroller
uiscrollview
uiscrollviewdelegate
【解决方案1】:
根据滚动位置在想要滚动的位置创建按钮。
- 滚动视图的原点 x:
scroll.frame.origin.x;
- 滚动视图的原点:
scroll.frame.origin.y;
- 滚动视图的高度:
scroll.frame.size.height;
- 滚动视图的宽度:
scroll.frame.size.width;
现在您可以根据需要将按钮放在滚动视图上:
button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,x1,y1);
【解决方案2】:
使用工作代码更新:除了让按钮滚出页面以使它们不可见
我知道您说过您查看了滚动视图代表,但这就是您解决此问题的方法。在查看 Pulse 时,唯一棘手的动作是向下滚动时,也就是看起来像 UIToolbars 的东西消失的时候。您需要拥有的最重要的东西是从 iPhone 可见屏幕底部和顶部的 UIView 继承的任何东西,因此当用户向下或向上滚动时,contentoffset 有一个值。
每一个其他的动作都会让它们看起来很漂亮,Pulse 可能做了类似的事情。
#import <UIKit/UIKit.h>
@interface C1ViewController : UIViewController
{
CGPoint _y;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@end
#import "C1ViewController.h"
@interface C1ViewController ()
@end
@implementation C1ViewController
@synthesize scroller = _scroller;
@synthesize toolbar = _toolbar;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width, self.scroller.frame.size.height + 100);
self.toolbar.hidden = TRUE;
_y = [self.scroller contentOffset];
}
// this method is getting deprecated, so don't worry about it to much
// but don't forget to dealloc...which I did not include.
- (void)viewDidUnload
{
[self setScroller:nil];
[self setToolbar:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"content offset %f", self.scroller.contentOffset);
if (_y.y < [self.scroller contentOffset].y){
self.toolbar.hidden = TRUE;
}
else {
self.toolbar.hidden = FALSE;
}
}
@end
我的视图层次结构在 Interface Builder 中的样子