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];
टचडाउन द्वारा, क्या आपका मतलब 'UIControlEventTouchDown' है? – Anurag
हाँ मेरा मतलब UIControlEventTouchDown –