ios - 如何使用移动箭头创建滑动开始按钮
问题描述
我想创建与 https://github.com/shadowfaxtech/proSwipeButton完全相同的滑动按钮。我想知道如何更改用户触摸按钮的箭头
我这样做是为了获得滑动动作。
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
rightSwipe.direction = .right
view.addGestureRecognizer(rightSwipe)
但问题是如何向按钮添加箭头,以改变用户触摸的位置。
解决方案
这是我为在按钮上滑动而编写的代码。您将图像分配给图像视图。
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
}
完整的项目在 github:https ://github.com/IamSaurav/SwipeButton
推荐阅读
- c - STM32F107 通过代码 HAL 进行读出保护
- postman - 邮递员没有与本地主机连接
- javascript - 单击后退按钮 Instantclick.io 后,Javascript 执行两次
- r - 将几个文件的内容与文件名的共享部分结合起来
- python - 在父进程和子进程中使用 select.select 时有时会发生死锁
- sql - laravel 查询返回未定义
- node.js - 无法使用 nodejs 从 sqlite3 连接 docker 的命名卷数据库
- java - 生成要打印的随机值序列。重新排序和打印。爪哇
- uml - 如何在 UML 图中概括参与者
- python - numpy.fromfunction with specified non-arange-like array