2012-04-10 34 views
1

其標記爲重複或重複的問題之前,請閱讀我的問題...如何以編程方式繪製邊框並在UIImageview周圍的文字後移除它?

  1. 我有一個scroleView中,我把一些圖片以表格的形式。

  2. 當用戶點擊其中一個時,會顯示下一個視圖控制器,並且點擊圖像的uiview會顯示綠色邊框。

  3. 而當用戶導航回到此視圖時,單擊的圖像顯示爲帶有綠色邊框的 。這一切工作正常

但是,當用戶點擊其他圖像的問題開始:前一次單擊圖像沒有恢復正常,即,其邊界在那裏停留,即使我把它的寬度爲0.0和色彩clearColor

請指導我如何消除這些邊界

我的代碼如下:

for (int row = 0; row < r; ++row) 
    { 
     for (int col = 0; col < 2; ++col) 
     { 
      int index = (row * 2) + col; 
      if(index < [tempArr count]) 
      { 
       CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100); 
       UIView *fr = [[UIView alloc] initWithFrame:frame]; 
       CGRect imgFrame = CGRectMake(0, 0, 145, 100); 
       UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame]; 
       imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:@"add"]]; 
       UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; 
       fr.layer.borderWidth = 0.0f; 
       fr.layer.borderColor = [UIColor greenColor].CGColor; 
       if(selctedFrame == index)//Here i put border 
       { 
        fr.layer.borderWidth = 2.0f; 
        fr.layer.borderColor = [UIColor greenColor].CGColor; 
       } 
       else //here i remove them 
       { 
        fr.layer.borderWidth = 0.0f; 
        fr.layer.borderColor = [UIColor clearColor].CGColor; 
       } 
       tapGesture.numberOfTapsRequired = 1; 
       tapGesture.numberOfTouchesRequired = 1; 
       [fr addGestureRecognizer:tapGesture]; 
       [fr addSubview:imgView]; 
       fr.tag = index; 
       [self.scrollDisplay addSubview:fr]; 
       [self.scrollDisplay bringSubviewToFront:fr]; 
      } 
     } 
    } 
    [self.view addSubview:self.scrollDisplay]; 

這種方法被稱爲在viewWillAppear:animated:方法

編輯

enter image description here

後,一些導航來回

enter image description here

回答

0

我只需要做的是將邊框顏色設置爲視圖的背景顏色。任何其他選項沒有做任何事情邊界沒有被隱藏,通過任何其他事情我唯一的工作是這個

0

新的答案 -我認爲問題是,你又增加了新的觀點和再次。而不是做UIView *fr = [[UIView alloc] initWithFrame:frame];,找到已存在的視圖爲:UIView *fr = [self.scrollDisplay viewWithTag:index];並對其進行更改。實際上,除了造成上述問題外,新的圖像視圖正在被添加到舊的圖像視圖中,效率非常低。我假設您已將視圖添加到scrollDisplay。您也不應該再次創建新的imgView對象。爲他們設置標籤,使他們獨特且易於獲取。例如,爲每個imgView設置標籤999,並將其檢索爲:UIImageView *imgView = [fr viewWithTag:999];另外,在結束時擺脫[fr addSubview:imgView];,[self.scrollDisplay addSubview:fr];[self.view addSubview:self.scrollDisplay];。所以你的代碼應該是這樣的:

for (int row = 0; row < r; ++row) 
    { 
     for (int col = 0; col < 2; ++col) 
     { 
      int index = (row * 2) + col; 
      if(index < [tempArr count]) 
      { 
       CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100); 
       UIView *fr = [self. scrollDisplay viewWithTag:index]; 
       CGRect imgFrame = CGRectMake(0, 0, 145, 100); 
       UIImageView *imgView = [fr viewWithTag:999]; 
    //imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:@"add"]]; //<--You've already set the image before so you don't need this 
       UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; 
       fr.layer.borderWidth = 0.0f; 
       fr.layer.borderColor = [UIColor greenColor].CGColor; 
       if(selctedFrame == index)//Here i put border 
       { 
        fr.layer.borderWidth = 2.0f; 
        fr.layer.borderColor = [UIColor greenColor].CGColor; 
       } 
       else //here i remove them 
       { 
        fr.layer.borderWidth = 0.0f; 
        fr.layer.borderColor = [UIColor clearColor].CGColor; 
       } 
       tapGesture.numberOfTapsRequired = 1; 
       tapGesture.numberOfTouchesRequired = 1; 
    //[fr addGestureRecognizer:tapGesture]; //<-- Not needed if you've already done this 
    //[fr addSubview:imgView];//<-- Not needed 
       fr.tag = index; 
    //[self.scrollDisplay addSubview:fr];//<-- Not needed 
       [self.scrollDisplay bringSubviewToFront:fr];//<-- probably not needed 
      } 
     } 
    } 
    //[self.view addSubview:self.scrollDisplay];//<-- Not needed 
+0

好吧我現在就試試這個 – 2012-04-11 05:12:54

+0

nop沒有改變只是看到我的應用程序屏幕截圖我剛剛添加 – 2012-04-11 05:22:15

+0

實際上viewWillAppear:這個滾動視圖是用圖像創建的,當圖像是全尺寸的時候,即左上角的那個,邊框將被重置,但是較小尺寸的圖像保留邊框,所以我必須每次都將圖像視圖添加到滾動視圖沒有東西顯示在滾動視圖 – 2012-04-16 07:30:01

相關問題