2012-07-08 41 views
3

有沒有Touch Up Inside的意見? 我想在用戶點擊或點擊視圖時調用某個操作。 目前我在這個視圖上使用隱藏按鈕,但那不是一個聰明的方法。點擊/點擊查看動作Xcode

任何幫助表示讚賞:)

+1

添加手勢識別器。可能的水龍頭。 – Bazinga 2012-07-08 10:10:01

回答

8

那麼你也可以使用addGestureRecognizer方法上的UIView像這樣:

// In some View controller 
UITapGestureRecognizer *tapGR; 
tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)] autorelease]; 
tapGR.numberOfTapsRequired = 1; 
[myUIView addGestureRecognizer:tapGR]; 

// Add a delegate method to handle the tap and do something with it. 
-(void)handleTap:(UITapGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     // handling code 
    } 
} 
+0

謝謝!我會嘗試你的解決方案。 – ph3nx 2012-07-08 15:05:17

4

我不爲什麼使用隱藏的按鈕將不是一個聰明的辦法理解......它是由蘋果公司本身建議的方式,我認爲這是聰明,更好的這樣的時刻...

+1

是的,我同意:) – Bazinga 2012-07-08 10:20:55

0

使用- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event如果你正在尋找隨時隨地檢測沒有按鈕的觸摸屏上。您還可以找到觸摸的位置,像這樣:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint *tLocation = [touch locationInView:self.view]; 
} 

注:touchesEnded被稱爲每一次視圖被竊聽,因此上述tLocation將改變每個用戶觸摸屏幕的時間。