2013-02-04 55 views
6

साथ खींचने योग्य बनाने मैं mainViewController है, लेकिन मैं एक छोटा सा UIView कि अंदर सक्रिय है जब आप MKMapKit में एक MKAnnotationView नल है, तो मैं की जरूरत है कि UIView स्क्रीन के किसी भी भाग में खींचने योग्य होने के लिए।एक UIView उंगली

मेरे एप्लिकेशन का एक उदाहरण स्क्रीनशॉट:

enter image description here

चक्र बिंदु का एक उदाहरण है, मुझे लगता है मैं "छोटे" UIView के किसी भी बिंदु के लिए खींच सकते हैं लगता है।

मैंने UITapGestureRecognizer का उपयोग करने का प्रयास किया लेकिन यह काम नहीं किया क्योंकि मेरा कोड पर्याप्त नहीं था और मैं इसे खींचने योग्य नहीं बना सका क्योंकि यह केवल टैप है, न टैप करें और स्थानांतरित करें।

मुझे आशा है कि आप मेरी मदद कर सकते हैं।

उत्तर

11
  1. उपयोग UIPanGestureRecognizer बजाय UITapGestureRecognizer
  2. सेट userInteractionEnabled = YES आपके विचार
  3. समीक्षा इशारा recognizers के बारे में यह अच्छा ट्यूटोरियल के लिए: Touches। विचार खींचने का अच्छा उदाहरण है।
+0

धन्यवाद उत्तर के लिए, मुझे लगता है कि परियोजना मैं परिणाम का परीक्षण अध्ययन करेगा और मुझे लगता है कि यह मेरे लिए एक बहुत मदद करता है सकते हैं, धन्यवाद। – rokimoki

2

@borrrden द्वारा एक सराहना के बाद संपादित

UIPanGestureRecognizer उपयुक्त है। अपने हैंडलर फ़ंक्शन में यह state चर की जांच करें।

typedef enum { 
    UIGestureRecognizerStatePossible, 

    UIGestureRecognizerStateBegan,  // this will be the value on touch 
    UIGestureRecognizerStateChanged, // ~ on drag 
    UIGestureRecognizerStateEnded,  // ~ on end of touch event 
    UIGestureRecognizerStateCancelled, 

    UIGestureRecognizerStateFailed, 

    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded 
} UIGestureRecognizerState; 
+0

धन्यवाद, मुझे नहीं पता था, मैं अभी भी एपीआई का अध्ययन कर रहा हूं। – rokimoki

+0

"बदले गए" को टैप इशारा पर नहीं बुलाया जाएगा, क्योंकि यह समझ में नहीं आता है। – borrrden

+0

सच है, मैं 'UIPanGestureRecognizer' को इंगित करने के लिए पोस्ट संपादित करूंगा – Alexander

1

मैं स्केलिंग और मेरे एप्लिकेशन में एक UIView खींच लिए इसका उपयोग से संबंधित है पढ़ें।

1) सबसे पहले अपने कार्यान्वयन के लिए निम्न जोड़ सकते हैं और में अपने विचार करने के लिए दुकानों कनेक्ट करने के लिए सुनिश्चित करें कि आपके स्टोरीबोर्ड

#import <QuartzCore/QuartzCore.h> 

@interface yourclass() { 
    BOOL isDragging; 
} 
@property (weak, nonatomic) IBOutlet UIImageView *outletMainView; // View that contains the view we want to drag 
@property (weak, nonatomic) IBOutlet UIImageView *outletRedDot; // The view we want to drag in the main view 

स्पर्श शुरू होता है जब मैं दृश्य थोड़ा पैमाने उपयोगकर्ता विशेष दृश्य को छू लेती है जब यहां उसे लाल डॉट

// ANIMATE RED DOT WHEN START DRAGING IT 
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.outletMainView]; 

    CGRect redDotRect = [self.outletRedDot frame]; 
    if (CGRectContainsPoint(redDotRect, touchLocation)) { 
     isDragging = YES; 
     NSLog(@"Red Dot tapped!"); 
     [UIView animateWithDuration:0.2 
           delay:0.0 
          options:UIViewAnimationOptionCurveEaseOut 
         animations:^{ 
          self.outletRedDot.transform = CGAffineTransformMakeScale(1.75, 1.75); 
         } 
         completion:^(BOOL finished) { 
         }]; 
    } else { 
     return; 
    } 
} 

2) है तो मैं उंगली बिंदु के बिंदु का पालन करने के दृश्य सेट

// ANIMATE AND MOVE RED DOT WHEN WE DRAG IT 
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.outletMainView]; 

    if (isDragging) { 
     [UIView animateWithDuration:0.0f 
           delay:0.0f 
          options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut) 
         animations:^{ 
          self.outletRedDot.center = touchLocation; 
          NSLog(@"X: %0.2f Y: %0.2f",touchLocation.x-redDotStartCenter.x, redDotStartCenter.y-touchLocation.y); 
         } 
         completion:NULL]; 
    } 
} 

3) अंत में जब खींचने दृश्य समाप्त होता है अपने मूल पैमाने पर रीसेट है

// RESET RED DOT WHEN WE STOP DRAGGING 
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.outletMainView]; 

    CGRect redDotRect = [self.outletRedDot frame]; 
    if (CGRectContainsPoint(redDotRect, touchLocation)) { 
     [UIView animateWithDuration:0.1 
           delay:0.0 
          options:0 
         animations:^{ 
          self.outletRedDot.transform = CGAffineTransformMakeScale(1.0, 1.0); 
         } 
         completion:^(BOOL finished) { 
         }]; 
    } 
    isDragging = NO; 
}