2013-06-24 23 views
1

嗨惡魔我是新來的iOS應用程序開發。我遇到了一個簡單的問題,在我的應用程序中添加了橫向滾動的UIScrollView。我已經添加了按鈕到該滾動視圖,將動態更改。代碼如何指向基於水平上的標籤值的按鈕位置UIScrollView

Step 1: 

scroll = [[UIScrollView alloc]init]; 
    scroll.delegate=self; 
    scroll.frame = CGRectMake(0, 0, 320, (height+25)); 
    [scroll setContentSize:CGSizeMake((width-30)*n, height+25)]; 
    scroll.backgroundColor=[UIColor clearColor]; 
    scroll.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GP-BG.png"]]; 
    scroll.showsHorizontalScrollIndicator=NO; 
    scroll.alwaysBounceHorizontal=NO; 
    for(int i=0;i<n;i++) 
    { 
     MODELRoom *room = [self.resultSet.dataObjectList objectAtIndex:i]; 
     CGRect rectForTitleButton = CGRectMake(i*(width-30), 0, width-30, height+25); 
     int buttonTag =10000+i; 
     UIButton *titleButton = [self getRoomButton:room tag:buttonTag]; 
     titleButton.tag=buttonTag; 
     [titleButton setFrame:rectForTitleButton]; 

     titleButton.backgroundColor=[UIColor clearColor]; 
     [scroll addSubview:titleButton]; 
     [controlButtons addObject:titleButton]; 
} 
} 

step 2: 

- (UIButton*)getRoomButton:(MODELRoom *)currenRoom tag:(int)tagValue{ 
    UIButton *button=[[UIButton alloc] init]; 
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    UIImage *buttonImageNormal; 
    if(currenRoom.currentlySelected){ 
     buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@"GP-BG-Slected Green.png"]]; 
    }else{ 
     buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@""]]; 
    } 
    [button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal]; 
    [gridViewObjects addObject:button]; 
    return button; 
} 

step 3: 

- (void)buttonClicked:(UIButton *)sender{ 
    if((int)[sender tag]>=10000 && (int)[sender tag]<20000){ 
     currentRoom=((MODELRoom*)[resultSet.dataObjectList objectAtIndex:[sender tag]-10000]); 
     [self roomSelectionChanged:currentRoom]; 
     currentRoom.currentlySelected=true; 
     scrollStrech=(width-30)*([sender tag]-10000); 
     scroll.contentOffset=CGPointMake(scrollStrech, 0); 
     NSLog(@"Scroll Strech=%d",scrollStrech); 
     for(int i=0;i<[resultSet.dataObjectList count];i++){ 
      if(![[resultSet.dataObjectList objectAtIndex:i] isEqual:currentRoom]){ 
       ((MODELRoom*)[resultSet.dataObjectList objectAtIndex:i]).currentlySelected=false; 
      } 
     } 
     [self refresh]; 
} 

step 4: 

-(void)refresh{ 
if([self.scroll isDescendantOfView:self.view]){ 
    [self.scroll removeFromSuperview]; 
} 

} 

每一件事工作正常,但是當我選擇第五或第六個按鈕是滾動之後,如果我看到選擇按鈕滾動視圖rebouncing到第一的位置。我發現這是從超級視圖中刪除並重新加載它的好處。如果是這種情況,我還需要僅在該選定按鈕區域顯示滾動視圖區域。

好心建議我如何克服這個....

+1

嘗試使用'scroll.contentOffset = CGPointMake(sender.frame.origin.x,0); '在按鈕動作 – Venkat

+0

嘗試,但它直接移動到該位置,這意味着選定的區域將成爲第一個按鈕消失以前的按鈕......你可以建議我通過使用按鈕標籤....因爲我們目前選擇按鈕。 ...? – user2515185

+0

我不明白你想要什麼..你可以解釋一點嗎? – Venkat

回答

0

上buttonClicked試試這個

CGRect rect= CGRectMake(sender.frame.origin.x, sender.frame.origin.y , sender.frame.size.width, sender.frame.size.height); 
    [scrollView scrollRectToVisible:rect animated:YES]; 
+0

謝謝......但是當我選擇下一個按鈕時,上一個按鈕被隱藏,直到滾動視圖結束... – user2515185

+0

在步驟2上,當按鈕不是當前選定時,您沒有給出背景圖像。我猜可能是因爲那 – George

相關問題