【发布时间】:2015-08-29 21:36:50
【问题描述】:
我是 IOS 应用程序开发的新手。我正在尝试实现滚动视图。我根本无法滚动。
以下是我的视图实现。 我创建了一个滚动视图并在其中添加了不同颜色的小块。 我在当前屏幕边界之外添加了一个块,以便我可以向下滚动 UIScrollView 并查看它。
@implementation CustomView
- (instancetype)init
{
self = [super init];
if (self) {
CGRect screenRect = CGRectMake(70, 100, 250, 800);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
scrollView.scrollEnabled = YES;
[scrollView setBackgroundColor:[UIColor yellowColor]];
[scrollView setAlpha:0.2];
[self addSubview:scrollView];
CGRect temp = CGRectMake(100, 150, 10, 10);
UIView *firstView = [[UIView alloc] initWithFrame:temp];
[firstView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:firstView];
temp = CGRectMake(150, 200, 10, 10);
UIView *secondView = [[UIView alloc] initWithFrame:temp];
[secondView setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:secondView];
temp = CGRectMake(200, 750, 10, 10);
UIView *thirdView = [[UIView alloc] initWithFrame:temp];
[thirdView setBackgroundColor:[UIColor orangeColor]];
[scrollView addSubview:thirdView];
scrollView.contentSize = screenRect.size;
}
return self;
}
@end
我正在使用我之前在视图控制器中创建的视图。我将它添加到我的视图控制器的视图中。
@interface ViewController ()
@property (nonatomic, readonly) CustomView *currentView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_currentView = [[CustomView alloc] init];
[self.view addSubview:_currentView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我根本无法使用此实现滚动。我错过了什么吗? 有人可以解释一下我的代码有什么问题吗?
谢谢
【问题讨论】:
标签: ios objective-c iphone uiscrollview