2017-01-23 32 views
4

我有一個加載SKSpriteNode到場景中的斯威夫特+ SpriteKit應用,註冊一個UIPinchGestureRecognizer,以及縮放手勢用一個簡單的處理函數,像這樣的手柄:如何限制放大SpriteKit

func zoom(_ sender: UIPinchGestureRecognizer) { 

    // Don't let the map get too small or too big: 
    if map.frame.width >= 1408 && map.frame.width <= 3072 { 
     map.run(SKAction.scale(by: sender.scale, duration: 0)) 
    } 

    print(map.frame.width) 
} 

然而,捏仍然會使精靈節點的大小小於指定的限制,然後當我嘗試再次鬆開時,處理程序突然識別出我已經放置的限制,並且不會允許和取消捏手勢。

我試圖做與識別的規模屬性同樣的事情:

func zoom(_ sender: UIPinchGestureRecognizer) { 

    // Don't let the map get too small or too big: 
    if sender.scale >= 0.9 && sender.scale <= 2.1 { 
     map.run(SKAction.scale(by: sender.scale, duration: 0)) 
    } 
    print(map.frame.width) 
} 

但這是更加古怪:精靈節點將停止與捏越來越小,但隨後將增長與聯合國極其巨大的-捏。

什麼是正確的方式來限制捏手勢?

回答

1

這是我想出了一個解決方案,但有一件事我不喜歡它:

func zoom(_ sender: UIPinchGestureRecognizer) { 

    // If the height of the map is already <= the screen height, abort pinch 
    if (sender.scale < 1) { 
     if (true) { print("pinch rec scale = \(sender.scale)") } 
     if (map.frame.width <= 1408) { 
      if (true) { print("Pinch aborted due to map height minimum.") } 
      return 
     } 
    } 

    // If the height of the map is already >= 2000 the screen height, abort zoom 
    if (sender.scale > 1) { 
     if (true) { print("pinch rec scale = \(sender.scale)") } 
     if (map.frame.width >= 3072) { 
      if (true) { print("Pinch aborted due to map height Max.") } 
      return; 
     } 
    } 

    map.run(SKAction.scale(by: sender.scale, duration: 0)) 
    sender.scale = 1; 
} 

並沒有真正的工作,偉大的是如果執行該項目快速捏地圖節點可以減小到比if語句中的限制小得多的尺寸。如果你以正常的速度進行捏合(反正不是超快),那麼在if語句中所規定的限制是受到尊重的。

我不知道如何處理這種情況。