如何以编程方式快速发送 pangesture

Posted

技术标签:

【中文标题】如何以编程方式快速发送 pangesture【英文标题】:How to programmatically send a pangesture in swift 【发布时间】:2015-09-03 06:57:51 【问题描述】:

我有一个具有 panGesture 功能的视图,但我需要以编程方式将平移手势从一个点发送到另一个点。有没有办法使用具有特定时间间隔的动画快速做到这一点?这是我尝试以编程方式调用平移手势:

    var upPanPoint = CGPoint(x: contentView.center.x, y: contentView.center.y + 500)
    var upPan = panGestureRecognizer.setTranslation(upPanPoint, inView: self)
    
    onSwipe(upPan)

这是识别平移手势的代码:

 func onSwipe(panGestureRecognizer : UIPanGestureRecognizer!) 
    let view = panGestureRecognizer.view!
    print(view)
    
    switch (panGestureRecognizer.state) 
    case UIGestureRecognizerState.Began:
        if (panGestureRecognizer.locationInView(view).y < view.center.y) 
            self.viewState.rotationDirection = .RotationAwayFromCenter
         else 
            self.viewState.rotationDirection = .RotationTowardsCenter
        
    case UIGestureRecognizerState.Ended:
        self.finalizePosition()
    default:
        let translation : CGPoint = panGestureRecognizer.translationInView(view)
        view.center = self.viewState.originalCenter + translation
        self.rotateForTranslation(translation, withRotationDirection: self.viewState.rotationDirection)
        self.executeOnPanForTranslation(translation)
    

【问题讨论】:

也许尝试以正确的顺序调用一些NSView触摸方法。 也许你必须将upPan.state 设置为.Changed(或一些不同于.Possible 的值,以便你识别它)。 UITapGestureRecognizer Programmatically trigger a tap in my view的可能重复 【参考方案1】:
// The Pan Gesture
func createPanGestureRecognizer(targetView: UIImageView) 
    var panGesture = UIPanGestureRecognizer(target: self, action:("handlePanGesture:"))
    targetView.addGestureRecognizer(panGesture)


func handlePanGesture(panGesture: UIPanGestureRecognizer) 
    // get translation
    var translation = panGesture.translationInView(view)
    panGesture.setTranslation(CGPointZero, inView: view)
    println(translation)

    // create a new Label and give it the parameters of the old one
    var label = panGesture.view as UIImageView
    label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y)
    label.multipleTouchEnabled = true
    label.userInteractionEnabled = true

    if panGesture.state == UIGestureRecognizer.State.began  
        // add something you want to happen when the Label Panning has started
    

    if panGesture.state == UIGestureRecognizer.State.ended 
        // add something you want to happen when the Label Panning has ended
    

    if panGesture.state == UIGestureRecognizer.State.changed            
        // add something you want to happen when the Label Panning has been change ( during the moving/panning ) 
     else   
        // or something when its not moving
        

【讨论】:

【参考方案2】:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGesture))
    self.imageView.addGestureRecognizer(panGesture)
@objc func panGesture(sender: UIPanGestureRecognizer)
    let point = sender.location(in: view)
    let panGesture = sender.view
    panGesture?.center = point
    print(point)

【讨论】:

【参考方案3】:

使用 Swift 4.2 版,您可以使用以下代码以编程方式设置平移手势:

let panGesture = UIPanGestureRecognizer(target: self, action:(#selector(self.handleGesture(_:))))
self.view.addGestureRecognizer(panGesture)

@objc func handleGesture(_ sender: UIPanGestureRecognizer) 

    switch sender.state 
    case .began:
    case .changed:
    case .cancelled:
    case .ended:
    default:
        break
    

【讨论】:

以上是关于如何以编程方式快速发送 pangesture的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式发送 ONLongPressGesture?

如何以编程方式快速创建 uicollectionview(没有情节提要)

如何以编程方式快速切换到暗模式

快速以编程方式创建后如何在情节提要中链接 UIButton

如何以与在 Windows 资源管理器中“发送给邮件收件人”相同的方式以编程方式发送电子邮件?

如何以编程方式快速定位图层?