[英]How do I drag UIView elements and change position?
我正在开发一个iOS应用程序,允许用户创建表格。 窗体具有堆叠在UIScrollView中的许多UIView元素。 就编辑而言,UIView应该可以更改顺序。
我该如何实现? 我想拖动任何UIView组件并将其移动到窗体中的任何位置。 例如。 持有第三个元素将激活自己移动。 将其移动到第二个元素将替换其位置。 我正在使用平移手势来移动UIViews。 现在我如何知道是否在任何UIView顶部都到达了轻击视图?
据我了解您的要求,您希望用户能够移动/重新定位某些UIView,例如在父视图中拖放。
我建议您使用启用了交互式运动的collectionView
。 如果要使用某些库,则可以尝试https://github.com/ra1028/RAReorderableLayout
或者对于UIView
拖放实现,您可以查看以下https://blog.apoorvmote.com/uipangesturerecognizer-to-make-draggable-uiview-in-ios-swift/
UICollectionViewController的几个简单选项可以通过canMoveItemAt委托方法轻松处理,如果您有视图控制器,则可以通过以下方式尝试:
import UIKit
class LoveProfileEditViewViewController: BaseViewController {
fileprivate var longPressGesture:UILongPressGestureRecognizer!
@IBOutlet var theCollectionView:UICollectionView!
public var items:[String]!
override func viewDidLoad() {
super.viewDidLoad()
let horizontalLayout = UICollectionViewFlowLayout()
horizontalLayout.scrollDirection = .horizontal
self.theCollectionView.collectionViewLayout = horizontalLayout
self.theCollectionView.register(UINib(nibName: "SomeNibName", bundle: nil), forCellWithReuseIdentifier: "Some Identifier")
self.longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
self.theCollectionView.addGestureRecognizer(longPressGesture)
}
fileprivate func moveDataItem(_ sIndex: Int, _ dIndex: Int) {
let item = self.items.remove(at: sIndex)
self.items.insert(item, at:dIndex)
self.theCollectionView.reloadData()
}
@objc private func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.began:
guard let selectedIndexPath = self.theCollectionView.indexPathForItem(at: gesture.location(in: self.theCollectionView)) else {
break
}
self.theCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case UIGestureRecognizerState.changed:
guard let selectedIndexPath = self.theCollectionView.indexPathForItem(at: gesture.location(in: self.theCollectionView)) else {
break
}
self.theCollectionView?.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case UIGestureRecognizerState.ended:
self.theCollectionView.endInteractiveMovement()
default:
self.theCollectionView.cancelInteractiveMovement()
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension LoveProfileEditViewViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 88.0, height: collectionView.bounds.size.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0.0, 5.0, 0.0, 5.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return .zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return .zero
}
}
// MARK: - UICollectionViewDataSource
extension LoveProfileEditViewViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
//set boolean as per your need
return true
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.row > 0 && destinationIndexPath.row > 0 {
self.moveDataItem(sourceIndexPath.row, destinationIndexPath.row)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Some Identifier", for: indexPath) as! SomeNibName
cell.textLabel.text = self.items[indexPath.row]
return cell
}
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(imageTouch:withEvent:)
forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(imageMoved:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
[button setImage:[UIImage imageNamed:@"vehicle.png"]
forState:UIControlStateNormal];
[self.view addSubview:button];
然后,您可以通过响应UIControlEventTouchDragInside事件,将车辆移动到所需的位置,例如:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.