2014-10-08 62 views
0

在XCode 5中,我如何獲得它,以便當我點擊UIImage時會發生什麼。就像一個按鈕,但我不想使用按鈕。我需要它是一個圖像。我正在製作一款遊戲,並且我無法在其他地方找到如何做到這一點,請大家幫忙。XCode 5 - 使圖像像按鈕一樣可點擊

+0

你有什麼試過的?你是否需要它是UIImage的一個實例,因爲你希望它是一個圖像按鈕,或者因爲你特別不想使用UIButton的實例? – Erik 2014-10-08 18:30:16

回答

2

沒有這樣的東西作爲一個鬆散的圖像,所以圖像必須在東西顯示它。確保某些內容(可能是圖像視圖)啓用了用戶交互。然後附加一個UITapGestureRecognizer。

+0

啓用用戶交互:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/userInteractionEnabled – matt 2014-10-08 18:27:18

+0

點擊手勢識別器:https:/ /developer.apple.com/library/ios/documentation/uikit/reference/uitapgesturerecognizer_class/index.html – matt 2014-10-08 18:27:40

2

爲了顯示您的UIImage,它需要附加到UIViewCALayer。你可以這樣做:

UIImageView *notAButton = [[UIImageView alloc] initWithImage:myImage]; 
[notAButton setUserInteractionEnabled:YES]; 
[notAButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedButton:)]]; 
[self.view addSubview notAButton]; 

你對UIButton你有什麼,順便說一下?這是迄今爲止最簡單和最廣泛接受的方式。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[aButton setImage:myImage forState:UIControlStateNormal]; 
[aButton addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:aButton]; 
+0

有沒有辦法將圖像設置到Xcode本身的按鈕上? – Praxiteles 2017-05-07 04:37:48