ios 在Swift中以编程方式模拟滑动手势

mf98qq94  于 2022-11-19  发布在  iOS
关注(0)|答案(5)|浏览(298)

我在Swift中实现了一个手势识别器,我希望能够模拟刷卡的过程(通过编程来实现刷卡)。
我以为会有一个内置的功能,但我发现的是一个点击手势,而不是滑动手势。
这就是我实现滑动手势的方式:

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
    cardView.addGestureRecognizer(gesture)
    cardView.userInteractionEnabled = true
}

func wasDragged (gesture: UIPanGestureRecognizer) {        
    let translation = gesture.translationInView(self.view)
    let cardView = gesture.view!

    // Move the object depending on the drag position
    cardView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
                              y:  self.view.bounds.height / 2 + translation.y)
3duebb1j

3duebb1j1#

你不能模拟一个手势识别器的全部含义(我的意思是,你不能让iOS认为这是一个真实的的用户操作)。
不过,你可以欺骗你自己的代码,让它看起来像是一个真实的的滑动动作。为此,你需要首先创建一个手势识别器:

var gestureRecognizerSwipeRight = UISwipeGestureRecognizer(target: self, action: "activatedGestureRecognizer:")
gestureRecognizerSwipeRight.direction = UISwipeGestureRecognizerDirection.Right
yourView.addGestureRecognizer(gestureRecognizerSwipeRight)

然后将它直接传递给你的动作:

// Some other place in your code
self.activatedGestureRecognizer(gesture: gestureRecognizerSwipeRight)

您的activatedGestureRecognizer(gesture:)方法应该类似于:

func activatedGestureRecognizer(gesture: UIGestureRecognizer) {
    if let gestureRecognizer = gesture as? UIGestureRecognizer {

        // Here you can compare using if gestureRecognizer == gestureRecognizerSwipeRight
        // ...or you could compare the direction of the gesture recognizer.
        // It all depends on your implementation really.

        if gestureRecognizer == gestureRecognizerSwipeRight {
            // Swipe right detected
        }
    }
}

公平地说,我看不出这样做有什么真实的的好处。简单地做与滑动相关的动作,而不是实际模拟手势识别器,应该要好得多。
例如,如果您需要在刷卡时制作动画,为什么不简单地禁用卡片视图上的用户交互,并通过编程制作动画呢?

quhf5bfb

quhf5bfb2#

适用于SWIFT 3.0

let swipeRightOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = .Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = .Left;

@IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blue
}
@IBAction func slideToRightWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGray
}
vwoqyblh

vwoqyblh3#

您可以自己创建UIPanGestureRecognizer并将其传递给wasDragged方法。不过,您应该检查转换的不同值:

let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: self.view)
wasDragged(gesture)

SWIFT 4.2版

let gesture = UIPanGestureRecognizer()
 gesture.setTranslation(CGPoint(x: 0, y: 100), in: self.view)
 wasDragged(gesture)

虽然我猜你还需要别的东西。你为什么要一开始就模仿这个姿势呢?

vsikbqxv

vsikbqxv4#

以下代码可用于编程滑动单元格。执行后,单元格将以动画方式移动到滑动位置。

@IBAction
func swipeFirstCell() {
    if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
        tableView.perform(Selector("_endSwipeToDeleteRowDidDelete:"), with: nil) // cancel any existing swipes
        tableView.perform(Selector("_swipeToDeleteCell:"), with: cell)
    }
}

请注意,您还需要为canEditRowAtIndexPath中的单元格返回true。
这可能会被Apple的私有API检测标记出来,但至少可以将其用于开发,如果真的需要,还可以对字符串进行模糊处理。

p8h8hvxi

p8h8hvxi5#

您需要实现UISwipeGestureRecognizer

override func viewDidLoad() {
super.viewDidLoad()

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)

var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)

}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

    switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.Left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.Up:
            print("Swiped up")
        default:
            break
    }
 }
}

相关问题