2012-12-08 39 views
0

我在这个事件中尝试过,我需要滚动事件,也需要按钮单击事件。在这里我使用了多个uiview,并且我放置了按钮(需要动作滚动和按钮动作)任何一个都可以给出解决方案?启用分页功能的UIScrollView?

- (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; 

}


在该上述代码按钮动作时发生,但滚动正在发生仅限于其视图和不能能滚动其他scrollviews。

回答

1

简单的解决方法是在scrollview中添加一个滚动视图应用分页,并在滚动视图中添加你的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]; 
} 
+0

我已经在视点与右和左视图展示中心在边框同样的观点需要的UIView重量230 .incX + = 320; –

+0

编辑我的答案,其过于简单,只是根据您的使用设置滚动视图的框架和incX的,它会工作正常。 – superGokuN