2012-01-04 13 views
5

मैं एक UISplitViewController (Xcode टेम्पलेट प्रोजेक्ट पर आधारित) में डी डी पर काम कर रहा हूं, MasterViewController से DetailViewController तक।आईओएस (आईपैड) एक UISplitViewController में खींचें और छोड़ें

असल में, मैं जो कर रहा हूं वह UILongPressGestureRecognizer बना रहा है और इसे MasterViewController की self.tableview प्रॉपर्टी पर रख रहा है।

नीचे मेरा इशारा पहचानकर्ता है। खींचें, ड्रॉप - - काम करता है

- (void)gestureHandler:(UIGestureRecognizer*)gesture 
{ 
    CGPoint location; 
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:[gesture locationInView:self.tableView]]; 
    if (gesture.state == UIGestureRecognizerStateBegan) { 
     // Create draggable view 
     NSString* imageCanvasName = [self imageNameByIndexPath:indexPath isDetail:YES]; 
     UIImage* imageCanvas = [UIImage imageNamed:imageCanvasName]; 
     _draggedView = [[UIImageView alloc] initWithImage:imageCanvas]; 

     // Create drag-feedback window, add the drag-view and make the drag-window visible 
     CGRect windowFrame = self.view.window.frame; 
     _dragFeedbackWindow = [[UIWindow alloc] initWithFrame:windowFrame]; 
     location = [gesture locationInView:gesture.view.window]; 
     [_draggedView setCenter:location]; 
     [_dragFeedbackWindow addSubview:_draggedView]; 
     [_dragFeedbackWindow setHidden:NO];  
    } 
    else if (gesture.state == UIGestureRecognizerStateChanged) { 
     // Update drag-view location 
     location = [gesture locationInView:gesture.view.window]; 
     [_draggedView setCenter:location]; 
    } 
    else if (gesture.state == UIGestureRecognizerStateEnded) 
    { 
     // Disconnect drag-view and hide drag-feedback window 
     [_draggedView removeFromSuperview];  
     [_dragFeedbackWindow setHidden:YES]; 

     // If drop is in a valid location... 
     if ([self.tableView pointInside:_draggedView.center withEvent:nil] == NO) 
     { 
      // Get final location in detailViewController coordinates 
      location = [gesture locationInView:self.detailViewController.view]; 
      [_draggedView setCenter:location]; 
      [self.detailViewController.view addSubview:_draggedView]; 
     } 
    } 
    else { 
     NSLog(@"%s unrecognized gesture %d", __FUNCTION__, gesture.state); 
    } 
} 

सभी बहुत अच्छी तरह से जब आईपैड पोर्ट्रेट मोड में होने काम करता है।

मेरी समस्या तब शुरू होती है जब आईपैड घुमाया जाता है ... ऐसे मामले में, मेरा _draggedView "काउंटर-रोटेटेड" दिखाई देता है - यह आईपैड के घूर्णन को "प्रतिबिंबित" करेगा - जब तक कि गिराया न जाए।

जैसे मैं _dragFeedbackWindow करने के लिए कुछ रोटेशन लागू करना चाहिए यह है - लेकिन मैं चीजों को, ऐसा न करने के एक नंबर ...

किसी भी विचार की कोशिश की?

धन्यवाद!

उत्तर

4

ठीक है - मैं पता लगा "क्यों" ऐसा होता है, और ("कैसे" ठीक करने के लिए और सभी नीचे सही ढंग से ऐसा करने के लिए, हैंडलर कोड जोड़ देती है

कोड यह रहा ... "बस। "अपने MAsterViewController में जोड़ने (यदि - मेरे जैसे - आप विस्तार करने के लिए गुरु से खींचना चाहते हैं ...)

// A simple UIView extension to rotate it to a given orientation 
@interface UIView(oriented) 
- (void)rotateToOrientation:(UIInterfaceOrientation)orientation; 
@end 

@implementation UIView(oriented) 
- (void)rotateToOrientation:(UIInterfaceOrientation)orientation { 
    CGFloat angle = 0.0;  
    switch (orientation) { 
     case UIInterfaceOrientationPortraitUpsideDown: 
      angle = M_PI; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      angle = - M_PI/2.0f; 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      angle = M_PI/2.0f; 
      break; 
     default: // as UIInterfaceOrientationPortrait 
      angle = 0.0; 
      break; 
    } 

    self.transform = CGAffineTransformMakeRotation(angle); 
} 

अब, LongPress इशारा हैंडलर ...

- (void)gestureHandler:(UIGestureRecognizer*)gesture 
{ 
    CGPoint location; 
    UIWindow* dragFeedback = [UIApplication sharedApplication].delegate.window; 
    if (gesture.state == UIGestureRecognizerStateBegan) { 
     // Create draggable view 
     _draggedView = [[UIImageView alloc] initWithImage:@"someImage.png"]; 

     // Required to adapt orientation... WORKS, BUT WHY NEEDED??? 
     [_draggedView rotateToOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; 

     // Create drag-feedback window, add the drag-view and make the drag-window visible 
     location = [gesture locationInView:dragFeedback]; 
     [_draggedView setCenter:location]; 
     [dragFeedback addSubview:_draggedView]; 
    } 
    else if (gesture.state == UIGestureRecognizerStateChanged) { 
     // Update drag-view location 
     location = [gesture locationInView:dragFeedback]; 
     [_draggedView setCenter:location]; 
    } 
    else if (gesture.state == UIGestureRecognizerStateEnded) 
    { 
     // Disconnect drag-view and hide drag-feedback window 
     [_draggedView removeFromSuperview];  

     // Get final location in detailViewController coordinates 
     location = [gesture locationInView:self.detailViewController.view]; 
     [_draggedView setCenter:location]; 
     // "Noramlize" orientation... WORKS, BUT WHY NEEDED??? 
     [_draggedView rotateToOrientation:UIInterfaceOrientationPortrait]; 
     [self.detailViewController.view addSubview:_draggedView]; 
    } 
    else { 
     NSLog(@"%s unrecognized gesture %d", __FUNCTION__, gesture.state); 
    } 
} 

यह एक जादू की तरह काम करता है - मुझे बताएं कि यह आपके लिए कैसे काम करता है (यदि आपको नमूना प्रोजेक्ट की आवश्यकता है, तो मुझे बताएं ...)

+0

हाय रेवेन, मेरे पास समान आवश्यकताएं हैं। क्या आप कृपया एक नमूना परियोजना साझा कर सकते हैं। धन्यवाद। – applefreak

+0

सुनिश्चित करें। यह मुझे सफाई के लिए एक दिन ले जाएगा। – Reuven

+1

यहां इसका आनंद लें। http://www.box.com/s/p7exfvxcvzgneo97d1of – Reuven

3

आपके समाधान के लिए धन्यवाद! आपको वहां से 9 0% रास्ता मिला। भुगतान में, मैं आपको अंतिम 10% दे दूंगा :)

समस्या लगता है कि UIWindow कभी घूमता नहीं है, केवल इसके सबव्यूज़ करते हैं! तो समाधान एक सबव्यू का उपयोग करना है।

मैंने कुछ कोड भी जोड़ा है जो दिखाता है कि कौन सा सेल चुना गया है (मान लें कि आपका मास्टर व्यू एक UITableViewController है)।

- (IBAction)handleGesture:(UIGestureRecognizer *)gesture 
    { 
     CGPoint location; 
     UIWindow *window = [UIApplication sharedApplication].delegate.window; 

     //The window doesn't seem to rotate, so we'll get the subview, which does! 
     UIView *dragFeedback = [window.subviews objectAtIndex:0]; 
     if (gesture.state == UIGestureRecognizerStateBegan) { 
      location = [gesture locationInView:dragFeedback]; 

      //which cell are we performing the long hold over? 
      NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[gesture locationInView:self.tableView]]; 
      UITableViewCell *selecteCell = [self.tableView cellForRowAtIndexPath:indexPath]; 
      NSLog(@"Cell %@", selecteCell); 

      // Create draggable view 
      _draggedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]]; 

      // Create drag-feedback window, add the drag-view and make the drag-window visible 
      [_draggedView setCenter:location]; 
      [dragFeedback addSubview:_draggedView]; 
     } 
     else if (gesture.state == UIGestureRecognizerStateChanged) { 
      // Update drag-view location 
      location = [gesture locationInView:dragFeedback]; 
      [_draggedView setCenter:location]; 
     } 
     else if (gesture.state == UIGestureRecognizerStateEnded) 
     { 
      // Disconnect drag-view and hide drag-feedback window 
      [_draggedView removeFromSuperview];  

      // Get final location in detailViewController coordinates 
      location = [gesture locationInView:self.detailViewController.view]; 
      [_draggedView setCenter:location]; 
      [self.detailViewController.view addSubview:_draggedView]; 
     } 
     else { 
      NSLog(@"%s unrecognized gesture %d", __FUNCTION__, gesture.state); 
     } 
    } 
+1

रेफ्यूएक्स, जबकि आपका परिवर्तन मैन्युअल रोटेशन की आवश्यकता को "साफ" करता है, यह अब मेनू के "शीर्ष पर" खींचने की दृश्य प्रतिक्रिया प्रदान नहीं करता है ... मैंने जो उदाहरण पोस्ट किया है उसे देखें: http://www.box.com/ s/p7exfvxcvzgneo97d1of – Reuven

+0

अहह .. मैंने पोर्ट्रेट मोड में कभी परीक्षण नहीं किया :) हां, मुझे लगता है कि पॉप-ओवर विंडो से ऊपर उठना आपको UIWindow पर आकर्षित करना होगा। मैंने कुछ और शोध किया और पुष्टि की कि: "खिड़की स्वयं वास्तव में समन्वय प्रणाली नहीं बदलती है, यह इसके उपखंडों में एक परिवर्तन लागू करती है जो उनके समन्वय प्रणाली को बदलती है।" तो कम से कम यह सब अब समझ में आता है? :) – RefuX

+0

निश्चित रूप से करता है। पुष्टि करने के लिए धन्यवाद। – Reuven

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