2012-05-05 31 views
2

我正在用「CGContextAddEllipseInRect」方法繪製一個圓圈。之後,我想點擊圈子來處理一些事件。可以點擊的iOS畫圈

沒有得到任何方法。

請幫幫我。

+0

必須要愛這個人 –

回答

0

假設您在UIView中繪製該圓,您可以創建自定義按鈕並將其添加到您的視圖。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(100, 100, 100, 30); 
[yourView addSubView:button]; 
[button addTarget:self action:@selector(yourEventTobeFired) forControlEvents:UIControlEventTouchUpInside]; 

那就是它。

+0

我只在一個視圖上繪製圓。但是該圓是基於矩形框中的半徑繪製的。我怎樣才能把一個按鈕放在一個圓上? – user1278228

+0

你可以請添加你的代碼嗎? –

+0

以下是用於繪製圓的代碼。圓形繪製。但希望可以點擊。 CGContextSetLineWidth(context,5.0); if(needColor)CGContextSetStrokeColorWithColor(context,[UIColor blueColor] .CGColor); else CGContextSetStrokeColorWithColor(context,[UIColor clearColor] .CGColor); CGContextAddEllipseInRect(context,rect); CGContextSetFillColorWithColor(context,[UIColor redColor] .CGColor); CGContextFillEllipseInRect(context,rect); CGContextStrokePath(context); – user1278228

5

我知道這個問題很老,但我想把我的兩毛錢扔在那裏。我認爲使用按鈕可以讓您更容易完成任務,如Jennis所示。但是,他的按鈕不是很圓。

需要更改以製作按鈕的屬性爲.cornerRadius。以下是這可以使用Jennis'的代碼示例來完成:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor; 
    button.frame = CGRectMake(x, y, width, height); 
    button.layer.cornerRadius = 20; //key to rounded button 
    [[self view] addSubview:button]; 
    [button addTarget:self action:@selector(yourEventTobeFired) forControlEvents:UIControlEventTouchUpInside]; 

而導致這段代碼的輸出:

enter image description here

此按鈕可點擊,並在點擊它會調用該方法yourEventTobeFired

如果您拒絕使用按鈕,那麼我建議您在自定義繪圖中添加gesture recognizer,因爲我不相信您可以像添加按鈕那樣添加action