ios - 如何创建带有移动箭头的滑动开始按钮

标签 ios swift swipe

我想像这样创建完全相同的滑动按钮 https://github.com/shadowfaxtech/proSwipeButton . 我想知道如何更改用户触摸按钮的箭头

我这样做是为了获得滑动 Action 。

let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
rightSwipe.direction = .right
view.addGestureRecognizer(rightSwipe)

但问题是如何向按钮添加箭头,以便在用户触摸时改变位置。

最佳答案

这是我为在按钮上滑动而编写的代码。您将图像分配给 ImageView 。

func createSwipeButton() {
        let button = UIButton.init(type: .custom)
        button.backgroundColor = UIColor.brown
        button.setTitle("PLACE ORDER", for: .normal)
        button.frame = CGRect.init(x: 10, y: 200, width: self.view.frame.size.width-20, height: 100)
        button.addTarget(self, action: #selector(swiped(_:event:)), for: .touchDragInside)
        button.addTarget(self, action: #selector(swipeEnded(_:event:)), for: .touchUpInside)
        self.view.addSubview(button)

        let swipableView = UIImageView.init()
        swipableView.frame = CGRect.init(x: 0, y: 0, width: 20, height: button.frame.size.height)
        swipableView.tag = 20
        swipableView.backgroundColor = UIColor.white
        button.addSubview(swipableView)
    }

    @objc func swiped(_ sender : UIButton, event: UIEvent) {
        let swipableView = sender.viewWithTag(20)!
        let centerPosition = location(event: event, subView: swipableView, superView: sender,isSwiping: true)
        UIView.animate(withDuration: 0.2) {
            swipableView.center = centerPosition
        }
    }

    @objc func swipeEnded(_ sender : UIButton, event: UIEvent) {
        let swipableView = sender.viewWithTag(20)!
        let centerPosition = location(event: event, subView: swipableView, superView: sender, isSwiping: false)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .curveEaseInOut, animations: {
            swipableView.center = centerPosition
        }) { _ in}
    }

    func location(event: UIEvent, subView: UIView, superView: UIButton, isSwiping: Bool) -> CGPoint {
        if let touch = event.touches(for: superView)?.first{
            let previousLocation = touch.previousLocation(in: superView)
            let location = touch.location(in: superView)
            let delta_x = location.x - previousLocation.x;
            print(subView.center.x + delta_x)
            var centerPosition = CGPoint.init(x: subView.center.x + delta_x, y: subView.center.y)
            let minX = subView.frame.size.width/2
            let maxX = superView.frame.size.width - subView.frame.size.width/2
            centerPosition.x = centerPosition.x < minX ? minX : centerPosition.x
            centerPosition.x = centerPosition.x > maxX ? maxX : centerPosition.x
            if !isSwiping{
                let normalPosition = superView.frame.size.width * 0.5
                centerPosition.x = centerPosition.x > normalPosition ? maxX : minX
                centerPosition.x = centerPosition.x <= normalPosition ? minX : centerPosition.x
            }
            return centerPosition
        }
        return CGPoint.zero
    }

enter image description here

完整项目在 github 上:https://github.com/IamSaurav/SwipeButton

关于ios - 如何创建带有移动箭头的滑动开始按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53910360/

相关文章:

ios - 我的设备上的音量不影响音量

iOS swift : Using AWS SDK with xCode6 - error message

java - 对于 DrawerLayout,仅用两根手指滑动即可打开菜单抽屉

ios - 如何在iPad中全屏显示UIVideoEditorViewController

ios - NSBinaryStoreType 有什么优点?

ios - Swift - 限制 UITextview 行中的字符数

ios - Swift - 以编程方式创建 View 问题,无法找到存储的属性

Jquery Mobile 幻灯片列表

Android 下拉 fragment

ios - "tmp"'t be removed because you don' 没有权限访问它