启用分页的 UIScrollView



>我在这个事件中尝试过我需要滚动事件,还需要按钮单击事件。在这里我使用了多个 uiview,并在其中放置了按钮(需要操作滚动和按钮操作)任何人都可以给出解决方案吗?

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
   if ([self pointInside:point withEvent:event]) {
           return _scrollView;
   }
   return nil;

}

在上面的代码中,滚动操作发生了,但按钮操作没有发生。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
   if ([self pointInside:point withEvent:event]) {
           return nil;
   }
   return nil;

}


在上面的代码中,按钮操作正在发生,但滚动仅针对其视图而无法滚动其他滚动视图。

简单的解决方案是在滚动视图中添加滚动视图应用分页并在滚动视图中添加您的 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];
}

最新更新