在XCode 5中,我如何獲得它,以便當我點擊UIImage時會發生什麼。就像一個按鈕,但我不想使用按鈕。我需要它是一個圖像。我正在製作一款遊戲,並且我無法在其他地方找到如何做到這一點,請大家幫忙。XCode 5 - 使圖像像按鈕一樣可點擊
回答
沒有這樣的東西作爲一個鬆散的圖像,所以圖像必須在東西顯示它。確保某些內容(可能是圖像視圖)啓用了用戶交互。然後附加一個UITapGestureRecognizer。
啓用用戶交互: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
點擊手勢識別器:https:/ /developer.apple.com/library/ios/documentation/uikit/reference/uitapgesturerecognizer_class/index.html – matt 2014-10-08 18:27:40
爲了顯示您的UIImage
,它需要附加到UIView
或CALayer
。你可以這樣做:
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];
有沒有辦法將圖像設置到Xcode本身的按鈕上? – Praxiteles 2017-05-07 04:37:48
- 1. 如何使圖像按鈕可點擊?
- 2. 按鈕VS可點擊圖像,看起來像按鈕
- 3. 可點擊隱藏按鈕或圖像
- 4. CSS圖像按鈕不可點擊
- 5. 像旋鈕一樣的圖像按鈕
- 6. 點擊一個按鈕加載圖像
- 7. 圖像視圖在iPhone中可按鈕點擊按鈕
- 8. 按鈕與圖像,圖像消失點擊按鈕時iOS 10
- 9. 點擊按鈕後移動圖像
- 10. 圖像作爲一個按鈕 - 點擊時改變圖像
- 11. 當點擊按鈕時,圖像第一次沒有變化。第一次點擊時要改變按鈕圖像
- 12. 按鈕被點擊5次後如何顯示圖像
- 13. 我想點擊切換按鈕圖像
- 14. 根據按鈕點擊移動圖像
- 15. Javascript隨機圖像點擊按鈕
- 16. 編程添加圖像按鈕+點擊
- 17. 按鈕點擊更改圖像
- 18. Android圖像移動按鈕點擊
- 19. 圖像保存按鈕點擊
- 20. 點擊按鈕改變圖像
- 21. 獲取點擊圖像按鈕的ID
- 22. 旋轉按鈕上的圖像點擊
- 23. 點擊後更改按鈕圖像
- 24. 點擊圖像按鈕效果
- 25. javascript:點擊按鈕時移動圖像
- 26. 如何更改點擊按鈕圖像?
- 27. 點擊動畫圖像/按鈕
- 28. 按鈕/圖像打開彈出點擊
- 29. 更改圖像在按鈕點擊ios
- 30. 點擊按鈕查看圖像。 qt
你有什麼試過的?你是否需要它是UIImage的一個實例,因爲你希望它是一個圖像按鈕,或者因爲你特別不想使用UIButton的實例? – Erik 2014-10-08 18:30:16