2011-02-08 8 views
8

के लिए UITouchDown लागू के लिए UIImageViewमैं जानता हूँ कि UIImageView

touchesBegan लागू करने के लिए कैसे यह संभव UIImageView के लिए UITouchDown लागू करने के लिए? है (मुझे पता है कि मैं UITouchDown के बजाय touchesBegan उपयोग कर सकते हैं, लेकिन मैं UITouchDown लागू करना चाहते हैं)

+0

टचडाउन द्वारा, क्या आपका मतलब 'UIControlEventTouchDown' है? – Anurag

+0

हाँ मेरा मतलब UIControlEventTouchDown –

उत्तर

13

UIButton का उपयोग करें।

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[aButton setImage:someUIImage forState:UIControlStateNormal]; 
[aButton addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside]; 

[self.view addSubview:aButton]; 

UIButton (अब विधि) के लिए वैकल्पिक

एक UIControl पहले से ही सटीक उपयोगकर्ता बातचीत के लिए उपयोगकर्ता बातचीत और समर्थन लागू करता है। UIView के उप-वर्ग हैं क्योंकि आप इसे प्राप्त करने के लिए UIImageView और UIControl की कार्यक्षमता को जोड़ सकते हैं।

इस व्यवहार को प्राप्त करने के लिए, UIControl पर एक सबव्यूव के रूप में UIImageView का एक ऑब्जेक्ट जोड़ें, जैसे कि छवि पूरी तरह से कवर हो। फिर addTarget:action:forControlEvents: का उपयोग करके इस नियंत्रण में एक ईवेंट हैंडलर जोड़ें।

// assuming the image view object exists 
UIImageView *anImageView = ..; 

// create a mask that the covers the image exactly 
UIControl *mask = [[UIControl alloc] initWithFrame:anImageView.frame]; 

// add the image as a subview of this mask 
CGSize imageSize = anImageView.frame.size; 
anImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height); 
[mask addSubview:anImageView]; 

// add a target-action for the desired control events to this mask 
[mask addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside]; 

// add the mask as a subview instead of the image 
[self.view addSubview:mask]; 
+0

धन्यवाद मैं अब विधि का उपयोग –

+0

उपयोग GestureRecognizer: http://stackoverflow.com/a/3907961/860488 –

1

आप नहीं कर सकते

ऐसे UIControlEventTouchDown आदि के रूप में

नियंत्रण घटनाओं UIButton तरह UIControl's बच्चे वस्तुओं के लिए उपलब्ध हैं, UITextField आदि UIImageView एक UIView है और इसलिए नियंत्रण सूचनाएं नहीं बना सकते।

आपको touchesBegan और सह का उपयोग करना होगा।

2

आप बजाय कस्टम UIButton बनाने की UITapGestureRecognizer उपयोग कर सकते हैं,: यहाँ एक उदाहरण है।

0

यूआईसींट्रोल इवेंट टचडाउन इत्यादि जैसी नियंत्रण घटनाएं यूआईकंट्रोल की बाल वस्तुओं जैसे UIButton, UITextField आदि के लिए उपलब्ध हैं। UIImageView एक UIView है और इसलिए नियंत्रण अधिसूचनाएं उत्पन्न नहीं कर सकती हैं।

संबंधित मुद्दे