UIScrollView paging just for horizontal, but not vertical?
I'开发者_开发百科ve been messing with the UIScrollView. Essentially I've put 7 views into it that are all taller than the contentSize. I set paging enabled to yes, and it's working great horizontally. An unexpected side effect, however, was that it also has paging enabled vertically. My tall views get stopped in multiples of the height of the contentSize. I'd like it to scroll normally vertically, and be paged horizontally. Is this possible?
Thanks
Not AFAIK. You should subclass UIScrollView, as explained in the documentation:
Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.
A scrollview will allow scrolling only if the contentSize in one dimension is unequal to the frame size in the same dimension. Also the size needs to be even.
So in your example make sure that you set contentSize appropriately to make the frame height. This will prevent vertical scrolling, regardless of what contents you have in the scroll view.
"Cocoanetics" is right. try this code.
uiScollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 360, 340)];
view.scrollEnabled = YES;
view.pagingEnabled=YES;
int y=20;
for (int i=0; i UIButton *btn=[UIButton buttonWithType:UIButtonTyperoundRect];
[btn setTitle:[NSString stringwithFormate:@"%d",i] forState:UIControlStateNormal];
int x=0;
if (i%2==0) {
x=20;
}
else{
x=190;
}
[btn setFrame:CGRectMake(x, y,170*0.8,170*0.8)];
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
if (i%2==1) {
y=y+170;
} [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=i;
[view addSubview:btn];
}
[view setContentSize:CGSizeMake(360, y+150)];
[view scrollRectToVisible:CGRectMake(0, 0, 360, 340) animated:YES];
view.scrollEnabled=TRUE;
self.view = view;
self.view.backgroundColor = [UIColor clearColor];
[view release];
this works for me.
精彩评论