2012-01-04 26 views
5

Bir UISplitViewController'da (Xcode şablonu projesine bağlı olarak) bir D & D üzerinde çalışıyorum, MasterViewController'dan DetailViewController'a. Temel olarak, yaptığım şey bir UILongPressGestureRecognizer'ı oluşturmak ve MasterViewController'ın self.tableview özelliğine yerleştirmektir.iOS (iPad) Sürükle ve Bırak bir UISplitViewController

Aşağıda benim jest tanıyıcı var.

- (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); 
    } 
} 

iPad dikey moddayken tüm işlemler çok güzel çalışır - sürükleme, bırakma - işler.

Sorunum, iPad döndürülürse başlar ... Böyle bir durumda, _draggedView'm "ters çevrilmiş" görünür - iPad'in dönüşünü - yansıtılana kadar "yansıtır".

Ben _dragFeedbackWindow bazı rotasyon uygulamak zorundadır Sanki - ama başarısız şeylerin bir dizi ...

herhangi bir fikir çalıştı?

Teşekkürler!

cevap

4

Tamam -. Ben "NEDEN" Bu olduğunda ve düzeltmek için (ve tüm aşağıda doğru bunu yapmak için işleyici kodu ekler "NASIL" İşte

kod ... "Sadece anladım (- benim gibi - sen detaylara ustadan sürüklemek istiyorum ... eğer) Şimdi

// 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 jest işleyicisi ...

- (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); 
    } 
} 

Bu "senin MAsterViewController eklemek ÇALIŞIYOR bir cazibe gibi - Bana nasıl çalıştığını bana bildirin (örnek bir projeye ihtiyacınız varsa, bana bildirin ...)

+0

Merhaba Reuven, Bende aynı gereksinim var. Lütfen bir örnek proje paylaşır mısınız lütfen. Teşekkürler. – applefreak

+0

emin. temizleme için bana bir gün sürecek. – Reuven

+1

İşte, keyfini çıkarın. http://www.box.com/s/p7exfvxcvzgneo97d1of – Reuven

3

Çözümünüz için teşekkürler! Yolun% 90'ını aldın. Ödemede size son 10% veririm :)

sorunu, UIWindow'un döndürülemeyeceği, sadece alt görünümlerinin yapılacağı gibi görünüyor! Yani çözüm bir alt görüş kullanmaktır.

Ayrıca hangi hücrenin seçildiğini gösteren bir kod ekledim (ana görünümünüzün bir UITableViewController olduğu varsayılarak).

- (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

RefuX, değeriniz manuel döndürme gereksinimini "temizler" iken, artık menüde "üstte" sürükleme görsel geri bildirimi sağlamaz ... Gönderdiğim örneğe bakın: http://www.box.com/ s/p7exfvxcvzgneo97d1of – Reuven

+0

Ahhh .. Portre modunda hiç test etmedim :) Evet, UIWindow üzerinde çizmeniz gereken açılır pencerenin üzerine çıkmayı düşünüyorum. Daha fazla araştırma yaptım ve şunu doğruladım: "Pencerenin kendisi koordinat sistemlerini değiştirmiyor, koordinat sistemini değiştiren alt görünümlerine bir dönüşüm uyguluyor." En azından şimdi anlamsız mıdır? :) – RefuX

+0

Elbette. Doğrulama için teşekkürler. – Reuven