2012-12-08 40 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