简单的解决方案是在滚动视图中添加滚动视图应用分页并将您的 Uiview 添加到滚动视图中,假设您必须显示 5 个视图然后尝试跟随。
yourScroll = [[UIScrollView alloc]init];
yourScroll.frame = CGRectMake(45, 45, 230, 300);
yourScroll.backgroundColor = [UIColor clearColor];
yourScroll.contentSize = CGSizeMake(yourScroll.frame.size.width * 5, yourScroll.frame.size.height);
[yourScroll setPagingEnabled : YES];
[yourScroll setDelegate:self];
int incX = 0 ;
for( int i = 0; i < 2; i++)
{
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake (incX,0,yourScroll.frame.size.width,yourScroll.frame.size.height)];
//Create a button and give it the tag like
button.tag = i;
//Now add a selector to the button and add the button to your view;
[myView addSubview:button]
[yourScroll addSubview:myView];
incX+= 230;
}
现在这是您通过单击按钮进行滚动的选择器:
- (IBAction)changePage:(id)sender
{
int page = [sender tag];
NSLog(@"the page is = %i",page);
CGRect frame = yourScroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[yourScroll scrollRectToVisible:frame animated:YES];
}