Reputation: 4719
The following code (in a UIView class) shows a red square at the top of the screen. The code allows the user to scroll the screen down, so that the red square disappears at the top, and the screen stays at the place the user scrolled to.
The problem is this: The user can also scroll UP, so that the red square temporarily goes towards the middle of the page (before snapping back into place). This should not be happening because the red square has been placed at the top of the view. How does one code correctly to avoid this? Thanks a lot!
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.canCancelContentTouches=NO;
UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1280)];
CGRect rectForBigRedSquare = CGRectMake(10, 10, 200, 200);
UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
[redSquare setBackgroundColor:[UIColor redColor]];
[contentView addSubview:redSquare];
[redSquare release];
CGSize contentViewSize = contentView.frame.size;
[scrollView addSubview:contentView];
[scrollView setContentSize:contentViewSize];
[contentView release];
[self addSubview:scrollView];
}
return self;
}
Upvotes: 0
Views: 385
Reputation: 20187
scrollView.bounces = NO;
Your use of 'up' and 'down' to describe the scrolling is the opposite of how these would normally be described in this context, by the way. In iOS when the user 'scrolls up' that means they are pushing the content up, so a square at the top would disappear off the top, not scroll down to the middle. :)
Upvotes: 2