2011-07-05 18 views
8

एक्सकोड में कोई छवि छूने पर आप कैसे पता लगा सकते हैं? मैं एप्पल प्रलेखन पर ध्यान दिया है और यह वास्तव में भ्रमित कर रहा है ... मैंने देखा:छवि को छूने पर कैसे पता लगाया जाए

if (CGRectContainsPoint([imageView frame], location)) 

लेकिन मेरी छवि अभी भी नहीं ले जाएगा। मैं touchesBegan + touchesMoved उपयोग करने की कोशिश, और हाँ करने के लिए छवि के userInteractionIsEnabled निर्धारित करते हैं, लेकिन यह अभी भी यह पता नहीं लगा होगा :(


संपादित करें: आप अपने सभी महान सुझाव के लिए बहुत बहुत शुक्रिया अंत में, मैं वास्तव में इसे जितना संभव हो सके उतना सरल बनाना चाहता था, और मुझे पता था कि मेरा कोड काम करना चाहिए, इसलिए मैंने इसके साथ झुकाव रखा, और अच्छी रात की नींद के बाद, मुझे एहसास हुआ कि यह काफी सरल समाधान था:

इन मेरे स्पर्श हटाए गए:

UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    CGRect shapeRect = [imageView frame]; 
    CGRect dropSquareRect = [dropSquare frame]; 

    CGPoint touchLocation = CGPointMake(location.x, location.y); 

    if (CGRectContainsPoint(shapeRect, touchLocation)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [imageView setCenter:touchLocation]; 
     [UIView commitAnimations]; 
    } 

    if (CGRectIntersectsRect(shapeRect, dropSquareRect)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     self.imageView.alpha = 0; 
     self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y); 
     self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8); 
     [UIView commitAnimations]; 

उत्तर

3

आप UIImageViewognizer को UIImageView में जोड़ सकते हैं जो UIImage को पकड़ रहा है। यह आपको यह पता लगाने की अनुमति देगा कि उपयोगकर्ता छवि पर पैनिंग कर रहा है और छवियों के अनुसार अनुवाद को स्थानांतरित कर सकता है। दस्तावेज़ीकरण का संदर्भ यहां है।

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html

यह इशारा recognizers का उपयोग कर के बाद से यह जहाँ तक पैनिंग चला जाता है के रूप में ओएस के बाकी के साथ संगति रखता है अच्छा है।

उम्मीद है कि इससे मदद मिलती है।

+0

यह किया जाना चाहिए ... भगवान का जवाब –

3

आपको स्पर्शों की आवश्यकता हैगेगन विधि उदा।

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
CGPoint myPoint = [touch locationInView:self]; 
    if (CGRectContainsPoint([imageView frame], location)){ 
    // code here    
    } 
} 

मैं सिर्फ पढ़ा है कि आप इसे करने की कोशिश है, हालांकि मुझे यकीन है कि क्यों यह काम नहीं करता नहीं हूँ। नीचे सुझाए गए टैप इशारे का उपयोग करने का प्रयास करें।

2

आप इस का उपयोग कर सकते हैं: ध्यान में रखते हुए की स्थापना के बाद

क्या किया लोड:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 

निम्न विधियों तब का उपयोग करें:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     //Do moving 
    } 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     // do moving 
    } 
+0

बहुत अप्रासंगिक उत्तर ... –

13

आप पता लगाने के लिए UIImageView साथ UITapGestureRecognizer उपयोग करने पर विचार कर सकता है स्पर्श

और userInteractionIsEnabled से YES पर भी सेट करना न भूलें।

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(imageTapped:)]; 
myImageView.userInteractionIsEnabled = YES; 
[myImageView addGestureRecognizer:tap]; 
[tap release]; 

imageTapped: विधि लागू करें।

- (void)imageTapped:(UITapGestureRecognizer *) gestureRecognizer 
    { 

    } 
3

आप इस कोशिश कर सकते हैं मान लीजिए आप एक

IBOutlet UIImageView *touchImageVIew; 
Let touchImageVIew height and width are 50,25; 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 

    CGPoint pt = [[touches anyObject] locationInView:touchImageVIew]; 

    if (pt.x>=0 && pt.x<=50 && pt.y>=0 && pt.y<=25) { 
     NSLog(@"image touched"); 
    } 

    else 
{ 
NSLog(@"image not touched"); 
} 
} 

समायोजित ऊंचाई, चौड़ाई और नाम अपने आवश्यकता के अनुसार है।

+1

यह एक अच्छा समाधान है। – Siriss

2

यदि आप UIImageView का उपयोग करने में इतना विशेष नहीं हैं, तो इसमें अपनी छवि के साथ कस्टम बटन का उपयोग करने का प्रयास करें और छवि को स्थानांतरित करने के लिए एक्शन विधियों, टचडाउन और टच ड्रैगडऑट का उपयोग करें।

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