2013-07-10 69 views
2

我在圖像視圖中使用圓形漸變作爲我的顏色拾取器的色輪。我想將平移手勢識別器的shouldReceiveTouch:限制爲漸變的位置,以便其餘視圖不受平移手勢識別器的影響。將平移手勢識別器限制到特定圓圈

我該如何指定與漸變半徑匹配的圓形邊界?

編輯:我曾嘗試下面的代碼不影響:

if (sender.numberOfTouches) 
    { 
     CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height/3); 

     float radius = MIN(size.width, size.height)/2; 

     [alphaSlider addTarget:self action:@selector(changeOpacity:) forControlEvents:UIControlEventValueChanged]; 
     [hellSlider addTarget:self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged]; 
     [saturationSlider addTarget:self action:@selector(saturate:) forControlEvents:UIControlEventValueChanged]; 
     [rSlider addTarget:self action:@selector(redSlider:) forControlEvents:UIControlEventValueChanged]; 
     [gSlider addTarget:self action:@selector(greenSlider:) forControlEvents:UIControlEventValueChanged]; 
     [bSlider addTarget:self action:@selector(blueSlider:) forControlEvents:UIControlEventValueChanged]; 




     CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: gradientView]; 
     CGPoint center = CGPointMake((size.width/2), (size.height /2)); 
     CGPoint delta = CGPointMake(lastPoint.x - center.x, lastPoint.y - center.y); 
     CGFloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : M_PI : atan2(delta.y, delta.x)); 
     angle = fmod(angle, M_PI * 2.0); 
     angle += angle >= 0 ? 0 : M_PI * 2.0; 
     if((lastPoint.x - center.x) + (lastPoint.y - center.y)/2 < radius) 
     { 
      UIColor *color = [UIColor colorWithHue: angle/(M_PI * 2.0) saturation:saturationSlider.value brightness:hellSlider.value alpha:alphaSlider.value]; 
      if ([color getRed: &r green: &g blue:&b alpha: &a]) 
      { 
       NSLog(@"Color value - R : %g G : %g : B %g", r*255, g*255, b*255); 
      } 
      float red = r; 
      float green = g; 
      float blue = b; 
      rText.text = [NSString stringWithFormat:@"%.0f",rSlider.value]; 
      gText.text = [NSString stringWithFormat:@"%.0f",green*255]; 
      bText.text = [NSString stringWithFormat:@"%.0f",blue*255]; 


      colorView.backgroundColor = [UIColor colorWithRed:(rText.text.floatValue/255) green:(gText.text.floatValue/255) blue:(bText.text.floatValue/255) alpha:alphaSlider.value]; 
      rSlider.value = red*255; 
      gSlider.value = green*255; 
      bSlider.value = blue*255; 
      alphaText.text = [NSString stringWithFormat:@"%.2f",alphaSlider.value]; 
      brightnessText.text = [NSString stringWithFormat:@"%.2f",hellSlider.value]; 
      saturationText.text = [NSString stringWithFormat:@"%.2f",saturationSlider.value]; 

     } 





    } 

}

它似乎在漸變的右側工作,並退出接收色彩更新,左側和下側仍更新。我需要適當限制panGesture,以便它不再幹擾我的UISliders

任何想法?

回答

1

你只需要對觸摸事件進行點擊測試,看看它是否在圓圈區域內。要做到這一點,你需要知道你的圓圈的中心點及其半徑。

這樣做的一般方程覆蓋在此存在的問題:

Equation for testing if a point is inside a circle

+0

這裏的問題如下。我使用imageView作爲漸變,即使我觸摸白色斑點,它仍然給我一些顏色的漸變... 我可以不創建一個容器視圖,在那裏拋出漸變,並檢查我的觸摸是在那個具體的觀點? – Exothug

+0

你當然可以,但意見有矩形邊界,而不是圓形邊界。因此,如果您絕對需要知道觸摸是否在圓形區域內,則必須使用我鏈接的公式。好消息是它非常簡單。 – lxt

+0

這意味着我不得不封裝函數,詢問觸摸是否在圓圈中,然後再調用另一個方法來請求邊界? – Exothug

相關問題