如何通过滑动在标签屏幕之间切换

时间:2015-05-26 14:43:42

标签: rubymotion

我有2个标签页,

我想向右滑动到第二个标签页

并向左滑动到第一个标签页

但我不知道如何在 handle_swipe 方法中调用打开的第二个标签页

有什么想法吗?谢谢

的AppDelegate

ruby class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.makeKeyAndVisible first_tab = FirstTabController.alloc.init second_tab = SecondTabController.alloc.init @tabbar = UITabBarController.alloc.init @tabbar.viewControllers = [first_tab, second_tab] @tabbar.wantsFullScreenLayout = true @window.addSubview @tabbar.view

FirstTabController

```红宝石

class FirstTabController < UIViewController
  def init
    if super 
      self.title = "First Tab"
      self.tabBarItem.image = UIImage.imageNamed('FirstTab.png')
    end
    self
  end 

  def viewDidLoad
    view.backgroundColor = UIColor.whiteColor
    @label = UILabel.new
    @label.text = 'First Tab'
    @label.frame = [[50,50],[250,50]]
    view.addSubview(@label)

    # right
    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
    self.view.addGestureRecognizer(recognizer)
    # left
    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
    recognizer.direction = UISwipeGestureRecognizerDirectionLeft
    self.view.addGestureRecognizer(recognizer)    
  end

  def handle_swipe(sender)

    if(sender.direction == UISwipeGestureRecognizerDirectionRight)
      p "Swiped right"
      # @secondary_controller = SecondaryController.alloc.init
      @secondary_controller = SecondTabController.alloc.init
      # self.navigationController.pushViewController(@secondary_controller, animated: true)
    end

  end

end

```

2 个答案:

答案 0 :(得分:1)

您可以尝试将手势识别器添加到UITabBarController视图中,在这种情况下,您需要将其子类化并将handle_swipe方法移动到该控制器。

如果您尝试此路线,我还会移动创建的代码并将手势识别器添加到viewDidLoad子类的UITabBarController方法中。

答案 1 :(得分:0)

最后,我通过将 UITabBarController 设置为全局变量来完成它,然后在滑动事件发生时更改其索引。

有没有更好的做法来实现这个目标?

我认为使用全局变量可能不是一个好主意。

但我不知道另一种方式。

```DIFF

--- a/ch_2/12_tabbars/app/first_tab_controller.rb
+++ b/ch_2/12_tabbars/app/first_tab_controller.rb
@@ -13,5 +13,22 @@ class FirstTabController < UIViewController
     @label.text = 'First Tab'
     @label.frame = [[50,50],[250,50]]
     view.addSubview(@label)
+
+    # right
+    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
+    self.view.addGestureRecognizer(recognizer)
+    # left
+    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
+    recognizer.direction = UISwipeGestureRecognizerDirectionLeft
+    self.view.addGestureRecognizer(recognizer)
+  end
+
+
+  def handle_swipe(sender)
+
+    if(sender.direction == UISwipeGestureRecognizerDirectionRight)
+      p "Swiped right"
+      $tabbar.selectedIndex =1
+    end
   end
 end
diff --git a/ch_2/12_tabbars/app/second_tab_controller.rb b/ch_2/12_tabbars/app/second_tab_controller.rb
index 18ce656..8e718b0 100644
--- a/ch_2/12_tabbars/app/second_tab_controller.rb
+++ b/ch_2/12_tabbars/app/second_tab_controller.rb
@@ -13,5 +13,24 @@ class SecondTabController < UIViewController
     @label.text = 'Second Tab Controller'
     @label.frame = [[50,50],[250,50]]
     view.addSubview(@label)
+
+    # right
+    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
+    self.view.addGestureRecognizer(recognizer)
+    # left
+    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:')
+    recognizer.direction = UISwipeGestureRecognizerDirectionLeft
+    self.view.addGestureRecognizer(recognizer)
+
+
   end
+
+
+  def handle_swipe(sender)
+
+    if(sender.direction == UISwipeGestureRecognizerDirectionLeft)
+      p "Swiped left"
+      $tabbar.selectedIndex = 0
+    end
+  end
 end

```