2011-12-05 18 views
6

मैं जो तब देखने की सीमाओं के भीतर भी खींचा जा सकता NSView के एक कस्टम उपवर्ग पर और आयत बनाकर कर रहा हूँ में एक आयत खींच:कोको

enter image description here

ऐसा करने के लिए कोड है:

// Get the starting location of the mouse down event. 
NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil]; 

// Break out if this is not within the bounds of the rect. 
if (!NSPointInRect(location, [self boundsOfAllControlPoints])) { 
    return; 
} 

while (YES) { 

    // Begin modal mouse tracking, looking for mouse dragged and mouse up events 
    NSEvent *trackingEvent = [[self window] nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)]; 

    // Get tracking location and convert it to point in the view. 
    NSPoint trackingLocation = [self convertPoint:[trackingEvent locationInWindow] fromView:nil]; 

    // Calculate the delta's of x and y compared to the previous point. 
    long dX = location.x - trackingLocation.x; 
    long dY = location.y - trackingLocation.y; 

    // Update all points in the rect 
    for (int i = 0; i < 4; i++) { 
     NSPoint newPoint = NSMakePoint(points[i].x - dX, points[i].y - dY); 
     points[i] = newPoint; 
    } 

    NSLog(@"Tracking location x: %f y: %f", trackingLocation.x, trackingLocation.y); 

    // Set current location as previous location. 
    location = trackingLocation; 

    // Ask for a redraw. 
    [self setNeedsDisplay:YES]; 

    // Stop mouse tracking if a mouse up is received. 
    if ([trackingEvent type] == NSLeftMouseUp) { 
     break; 
    } 

} 

मैं मूल रूप से एक माउस डाउन इवेंट पकड़ता हूं और जांचता हूं कि उसका स्थान ड्रैगगेबल रेक्ट के अंदर है या नहीं। यदि ऐसा है, तो मैं trackEvent में माउस के आंदोलन को ट्रैक करना शुरू कर देता हूं। मैं एक्स और वाई निर्देशांक के लिए डेल्टा की गणना करता हूं, ड्रैगगेबल रेक्ट के लिए नए अंक बनाते हैं, और दृश्यों के प्रदर्शन को रीफ्रेश करने का अनुरोध करता हूं।

हालांकि यह काम करता है, यह ड्रैग के दौरान थोड़ा "शौकिया" दिखता है, माउस पॉइंटर खींचने वाले आकार के साथ पकड़ लेगा और अंततः इसकी सीमा पार करेगा। अन्य ड्रैग ऑपरेशंस में ड्रैग ऑपरेशन की शुरूआत से लेकर ऑब्जेक्ट की स्थिति के लिए माउस पॉइंटर को खींचा जा सकता है।

इस प्रभाव का कारण क्या है?

संपादित करें:

मैं अपने दृष्टिकोण रोब के जवाब निम्नलिखित बदल गया है और तीन विधि दृष्टिकोण अपनाया गया है:

- (void) mouseDown: (NSEvent*) event { 

    // There was a mouse down event which might be in the thumbnail rect. 

    [self setDragStartPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; 

    // Indicate we have a valid start of a drag. 
    if (NSPointInRect([self dragStartPoint], [self boundsOfAllControlPoints])) { 
     [self setValidDrag: YES]; 
    } 

} 

- (void) mouseDragged: (NSEvent *) anEvent { 

    // Return if a valid drag was not detected during a mouse down event. 
    if (![self validDrag]) { 
     return; 
    } 

    NSLog(@"Tracking a drag."); 

    // Get tracking location and convert it to point in the view. 
    NSPoint trackingLocation = [self convertPoint: [anEvent locationInWindow] fromView: nil]; 

    // Calculate the delta's of x and y compared to the previous point. 
    long dX = [self dragStartPoint].x - trackingLocation.x; 
    long dY = [self dragStartPoint].y - trackingLocation.y; 

    // Update all points in the rect 
    for (int i = 0; i < 4; i++) { 
     NSPoint newPoint = NSMakePoint(points[i].x - dX, points[i].y - dY); 
     points[i] = newPoint; 
    } 

    // Ask for a redraw. 
    [self setNeedsDisplay:YES]; 

    NSLog(@"Tracking location x: %f y: %f", trackingLocation.x, trackingLocation.y); 

    // Set current location as previous location. 
    [self setDragStartPoint: trackingLocation]; 

    NSLog(@"Completed mouseDragged method. Allow for repaint."); 

} 

- (void) mouseUp: (NSEvent *) anEvent { 

    // End the drag. 
    [self setValidDrag: NO]; 
    [self setNeedsDisplay: YES]; 

} 

हालांकि प्रभाव थोड़ा बेहतर है, वहां अभी भी रेक्ट के साथ एक ध्यान देने योग्य विलंब होता है अंत में माउस सूचक के आंदोलन की दिशा के पीछे खींच रहा है। जब मैं ड्रैग के दौरान धीरे-धीरे माउस को ले जाता हूं तो यह विशेष रूप से ध्यान देने योग्य होता है।

संपादित 2:

समझ गया। समस्या डेल्टा की गणना के साथ थी। मैंने इसके लिए लंबे समय तक उपयोग किया, जबकि मुझे फ्लोट का उपयोग करना चाहिए। अब महान काम करता है।

+1

माउस ड्रैगिंग चीज़ प्राप्त करने के लिए अपने कोड साझा करने के लिए धन्यवाद। – Tommy

उत्तर

9

आप ड्रॉ के दौरान इवेंट लूप पर हैं, जिसका मतलब है कि वर्ग कभी भी फिर से नहीं निकलता है। setNeedsDisplay: पर आपका कॉल कुछ भी नहीं खींचता है। यह सिर्फ दृश्य को फिर से खींचा जा सकता है। यह इस नियमित रिटर्न तक फिर से नहीं जा सकता है, जो माउस बटन जारी होने तक आप नहीं करते हैं। कोको में खींचने को कार्यान्वित करने के तरीके पर पूरी चर्चा के लिए

Handling Mouse Dragging Operations पढ़ें। आपको या तो mouseDown: से वापस लौटना होगा और mouseDragged: और mouseUp: ओवरराइड करना होगा, या आपको इवेंट लूप को पंप करने की आवश्यकता है ताकि ड्राइंग चक्र संसाधित हो सके।

मैं पहले दृष्टिकोण की सिफारिश करता हूं, भले ही इसे कई तरीकों की आवश्यकता हो। इवेंट लूप पम्पिंग बहुत आश्चर्यजनक बग बना सकता है और सावधानी के साथ इस्तेमाल किया जाना चाहिए। मेरे अनुभव में सबसे आम बग विलंबित चयनकर्ताओं के कारण होते हैं जब आप इवेंट लूप पंप करते हैं, जिससे आपके ड्रैगिंग रूटीन के बीच में "अतिरिक्त" कोड चलाना पड़ता है। कुछ मामलों में, यह पुनर्वित्त और डेडलॉक का कारण बन सकता है। (मैंने यह किया है ....)

+0

+1 - निश्चित रूप से उल्लेख किए गए पहले दृष्टिकोण पर सहमत हैं। – bryanmac