我正在开发一个允许用户创建表单的iOS应用程序。 Form具有存储在堆栈中的UIScrollView中的UIView元素的数量。就编辑而言,UIViews应该改变订单。
我如何实现这一目标?我想拖动任何UIView组件并将其移动到表单中的任何位置。例如。持有第三个元素将激活自己移动。在第二个元素上移动它将取代它的位置。我正在使用平移手势来移动UIViews。现在我怎么知道在任何UIView之上是否达到了点击视图?
答案 0 :(得分:0)
据我了解您的要求,您希望用户能够在父视图中移动/重新定位某些UIViews,例如拖放。
我建议您使用collectionView
并启用其交互式动作。如果您想使用某个库,那么您可以尝试https://github.com/ra1028/RAReorderableLayout
或UIView
拖动&删除实现你可以检查这个
https://blog.apoorvmote.com/uipangesturerecognizer-to-make-draggable-uiview-in-ios-swift/
答案 1 :(得分:0)
可以通过canMoveItemAt委托方法轻松处理UICollectionViewController的简单选项 如果您有视图控制器,可以尝试以下方式:
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
}
}
答案 2 :(得分:0)
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;
}