0

我试过在这个事件中我需要滚动事件并且还需要按钮单击事件。我在这里使用了多个 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;

}


在上面的代码中,按钮动作正在发生,但滚动只发生在它的视图中,不能滚动其他滚动视图。

4

1 回答 1

1

简单的解决方案是在滚动视图中添加滚动视图应用分页并将您的 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];
}
于 2012-12-08T08:45:54.997 回答