2013-02-09 126 views
1

我試圖開發具有一定的圖像的應用程序,所以我所做的一樣:如何檢測點擊/點擊UIImageView?

@property (nonatomic, retain) IBOutletCollection(UIImageView)NSArray* photos;

,我填補這些UIImageView陣列:

[self.photos[i] setImage:[UIImage imageWithData:data]];

現在,我需要執行segue,它會將用戶重定向到另一個視圖,並指出哪個圖像被點擊。

例子:

假設我有20幅圖像和查看,如果用戶點擊第一個圖像,然後他應該被重定向到查看,如果用戶點擊第二個則相同的觀點,等等...

回答

1

如果你正在使用UIImageView,然後處理tap,你必須創建UITapGestureRecognizer,給它們一個選擇器方法,並將它們添加到imageViews,並處理各個選擇器中的水龍頭。

更好的方法是使用自定義的UIButton而不是UIImageView,並直接從storyboard/xib中將它們與動作掛鉤,並在這些動作中處理按鈕按下事件。所以你可以防止在每個imageView中添加UITapGestures。

+0

順便說一句,如果你做前者,一定要設置'userInteractionEnabled'。 – Rob 2013-02-09 21:32:43

+0

許多手勢識別器有什麼問題?難道不是那些或許多按鈕? – danh 2013-02-09 23:58:24

+0

手勢識別器沒有任何問題,但由於他只對輕敲手勢感興趣,因此按鈕會截取輕擊事件並自動向目標對象發送操作消息。所以它會減少代碼,就是這樣。 – 2013-02-10 10:10:46

0

有三種方式
1)你可以使用TapGestures
2)您可以在UIControl使用UIControl &添加ImageView的&給予選擇以UIControl
3)你的圖像添加到自定義的UIButton

3

我喜歡在UIImageView上添加遮罩按鈕來響應用戶的觸摸,但請注意,UIImageView的userInteractionEnabled默認爲NO

下面是示例代碼,實際上我們總是將該按鈕設置爲透明。

CGRect imageRect = (CGRect){100, 100, 100, 100}; 
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:imageRect] autorelease]; 
imageView.backgroundColor = [UIColor yellowColor]; 
[self.view addSubview:imageView]; 
imageView.userInteractionEnabled = YES; 

UIButton *maskBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
maskBtn.frame = imageView.bounds; 
maskBtn.backgroundColor = [UIColor redColor]; 
[maskBtn addTarget:self action:@selector(maskBtnDidClick:) forControlEvents:UIControlEventTouchUpInside]; 
[imageView addSubview:maskBtn];