2017-08-12 45 views
0

以下是我的代码。我也发布了截图。Swift 3 - 无法执行功能

Button_CustomSegmentValueChanged是我自己制作的自定义分段控件。它是一个单独的按钮,工作正常,但我想从“SwipedRight”函数中运行“Button_CustomSegmentValueChanged”函数,但我无法传递正确的开关值以便这样做。需要帮助。

有人吗?

import UIKit 

class CredentialsViewController: UIViewController { 

    @IBOutlet weak var loginView: UIView! 
    @IBOutlet weak var signupView: UIView! 

    var currentSelectedView = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let swipeR = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedRight(swipe:))) 
     swipeR.direction = .right 
     view.addGestureRecognizer(swipeR) 

     let swipeL = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedLeft(swipe:))) 
     swipeL.direction = .left 
     view.addGestureRecognizer(swipeL) 

     //Hide view 
     signupView.transform = CGAffineTransform(translationX: signupView.frame.size.width, y: 0) 
    } 

    @IBAction func Button_CustomSegmentValueChanged(_ sender: CustomSegmentedControl) { 

     switch sender.selectedSegmentIndex { 
     case 0: 
      LoadLoginView() 
      break 

     case 1: 
      LoadSignupView() 
      break 

     default: 
      break 
     } 

    } 

    func SwipedRight(swipe : UISwipeGestureRecognizer){ 
     if currentSelectedView == 1 { 
      Button_CustomSegmentValueChanged(0) 
      //LoadLoginView() 
     } 
    } 

    func SwipedLeft(swipe : UISwipeGestureRecognizer){ 
     if currentSelectedView == 0 { 
      //LoadSignupView() 
     } 

    } 

    func LoadLoginView(){ 

     UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 
      self.signupView.transform = CGAffineTransform(translationX : self.loginView.frame.size.width, y : 0) 
      self.loginView.transform = .identity 
      self.currentSelectedView = 0 
     }) 
    } 

    func LoadSignupView(){ 

     UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 
      self.signupView.transform = .identity 
      self.loginView.transform = CGAffineTransform(translationX: self.loginView.frame.size.width, y: 0) 
      self.currentSelectedView = 1 
     }) 
    } 


} 

Screenshot of my code and error

+0

Button_CustomSegmentValueChanged函数接受CustomSegmentedControl作为参数,你要发送诠释它 – 3stud1ant3

+0

您好,我知道你在说什么,但你能告诉我它是如何做正确的代码。问题是我已经制作了一个具有两个选项的自定义分段控制器,并且我使用两个视图在两个视图之间切换。当我点击分段控件时,一切正常。当我滑动,它的工作正常,但分段控制器中的选择器没有改变。原因在于它需要用来了解开关。我想通过从另一个函数调用我的tapped函数来复制它,只是让它工作。看看视频http://youtu.be/Z5CHq4-MeVo – Biks

+0

好吧,你可以显示CustomSegmentedControl的代码? – 3stud1ant3

回答

0

我会建议使用在CredentialSegmentedController的按钮阵列,以模仿变化分段

  1. 的行为从您的自定义分段控制做一个出口
  2. 然后使用该段控制修改代码像下面

    func SwipedRight(swipe : UISwipeGestureRecognizer) { 
    
          if currentSelectedView == 1 { 
            //Button_CustomSegmentValueChanged(0) 
            //LoadLoginView() 
            let btn = customSegmentOutlet.buttons[0] 
            customSegmentOutlet.buttonTapped(btn) 
          } 
        } 
    
        func SwipedLeft(swipe : UISwipeGestureRecognizer){ 
          if currentSelectedView == 0 { 
            //LoadSignupView() 
            let btn = customSegmentOutlet.buttons[1] 
            customSegmentOutlet.buttonTapped(btn) 
          } 
    
        } 
    
+0

您的代码工作正常,但我的问题仍然存在。问题是我已经制作了一个具有两个选项的自定义分段控制器,并且我使用两个视图在两个视图之间切换。当我点击分段控件时,一切正常。当我滑动,它的工作正常,但分段控制器中的选择器没有改变。原因在于它需要用来了解开关。我想通过从另一个函数中调用我的tapped函数来复制该函数,以便切换选择器。 看一看视频https://youtu.be/Z5CHq4-MeVo – Biks

+0

@Biks我更新根据您的要求我的回答,看它是否有助于你 – 3stud1ant3

+0

真棒..它的工作..唷!非常感谢..它的工作现在完美... – Biks

1

你传入Int虽然实际需要的类型是CustomSegmentedControl。要简单地解决这个问题,只需为CustomSegmentedControl创建IBOutlet,并将其作为参数传递给Button_CustomSegmentValueChanged方法。

func SwipedRight(swipe : UISwipeGestureRecognizer){ 
    if currentSelectedView == 1 { 
     customSegmentOutlet.selectedSegmentIndex = 0 
     Button_CustomSegmentValueChanged(customSegmentOutlet) 
     //LoadLoginView() 
    } 
} 
+0

我做了一个插座,但我写什么代码来调用我的按钮功能?这并不正常工作...... FUNC SwipedRight(刷卡:UISwipeGestureRecognizer){ 如果currentSelectedView == 1 {// Button_CustomSegmentValueChanged(0) customSegmentOutlet.selectedSegmentIndex = 1个 customSegmentOutlet.sendActions(用于:.valueChanged) LoadLoginView( ) } } – Biks

+0

看到我编辑的答案 –

+0

nope ..它不工作..我已经加入我的出口括号内,但它不接受它,并给出错误“无法将类型的价值......” 'customSegmentOutlet.selectedSegmentIndex = 1 Button_CustomSegmentValueChanged(customSegmentOutlet) LoadLoginView()' – Biks

0

假设你CustomSegmentedControlUISegmentedControl一个子类,调用此

func swipedRight(swipe : UISwipeGestureRecognizer){ 
    if currentSelectedView == 1 { 
     let seg = UISegmentedControl() 
    seg.selectedSegmentIndex = 0 
    Button_CustomSegmentValueChanged(seg) 
    } 
} 

如果CustomSegmentedControl不是UISegmentedControl子类时我已经修改的代码

@IBAction func button_CustomSegmentValueChanged(_ sender: UISegmentedControl?) { 
    // guard sender for nil before use 
} 

几行,那么改变它。

+0

嗨。谢谢。我已经尝试过你的方法和它的工作。但我试图达到的目标还没有实现。我的观点正在改变,但分部控制器的选择没有改变。请检查视频,你会明白http://youtu.be/Z5CHq4-MeVo – Biks

+0

而不是创造新的segmentcontroller,为您现有的和使用方式,因此创建一个IBOutlet –