如何以编程方式快速发送一个pangesture [英] How to programmatically send a pangesture in swift

查看:33
本文介绍了如何以编程方式快速发送一个pangesture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a view that has panGesture functionality, but I need to send a pan-gesture from one point to another programatically. Is there a way to do this in swift using an animation with a specific time interval? Here is my attempt at calling the pan gesture programmatically:

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

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

here is the code that recognizes the pan gesture:

 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)
    }
}

推荐答案

// 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
    }    
}

这篇关于如何以编程方式快速发送一个pangesture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆