Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3中的滑动按钮_Ios_Swift3 - Fatal编程技术网

Ios Swift 3中的滑动按钮

Ios Swift 3中的滑动按钮,ios,swift3,Ios,Swift3,我想做一个滑动按钮。我想让你在我向右移动时更改按钮的图像。当我向左滑动时,我希望按钮图像恢复 我的代码: import UIKit class ViewController: UIViewController { @IBOutlet var swipeButton: UIButton! let swipe = UISwipeGestureRecognizer() override func viewDidLoad() { super.viewDidLoad() let

我想做一个滑动按钮。我想让你在我向右移动时更改按钮的图像。当我向左滑动时,我希望按钮图像恢复

我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var swipeButton: UIButton!
let swipe = UISwipeGestureRecognizer()


override func viewDidLoad()
{
    super.viewDidLoad()

    let swipeButtonRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("ButtonRight"))
        swipeButtonRight.direction = UISwipeGestureRecognizerDirection.right
        self.swipeButton.addGestureRecognizer(swipeButtonRight)

    let swipeButtonLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("ButtonLeft"))
        swipeButtonLeft.direction = UISwipeGestureRecognizerDirection.left
        self.swipeButton.addGestureRecognizer(swipeButtonLeft)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer)
{

    if let swipeGesture = gesture as? UISwipeGestureRecognizer
    {
        switch swipeGesture.direction
        {

        case UISwipeGestureRecognizerDirection.right:
         swipeButton.setImage = UIImage(named:"x.png")

        case UISwipeGestureRecognizerDirection.left:
        swipeButton.setImage = UIImage(named:"y.png")

        default:
           break
        }
    }
}
}

可能有什么问题?

在手势识别器的构造函数中的
动作
参数中,您告诉应用程序寻找一个名为“ButtonRight”(和“ButtonLeft”)的方法。您应该在此处传入要执行的实际方法(例如“respondtoswipesignature”)


在“RespondToSweep手势”中放置一个断点,您将看到它从未被调用。

第一个判断不是您的开关没有正确判断对象

#选择器(响应sweep手势:)

另一种设置图片的方法应该是这样写的


SwipeButton.setImage(UIImage(名为:“x”),forState:.normal)

您对功能的描述听起来好像真的应该查看UIScrollView而不是滑动。将这两个图像放入其中,添加代码使其“调整”以显示用户滑动到的图像,然后完成操作!对于
UIButton
s图像,您应该使用
swipeButton.setImage(UIImage(名为:“x”),forState:.normal)
--您缺少
forState:
如何修复它@ØyvindhaugetSWIFT的语法已经发生了很大的变化,但我相信这应该是可行的:
#选择器(respondtoswipe手势:)