代码之家  ›  专栏  ›  技术社区  ›  John Jackson

如何以动画结束UILongPressGestureRecognizer?

  •  1
  • John Jackson  · 技术社区  · 9 年前

    我已使用 UILongPressGestureRecognizer 像这样:

    override func viewDidAppear(animated: Bool) {
    
            var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress"))
            imageView.addGestureRecognizer(longPress)
            imageView.userInteractionEnabled = true
        }
    
        func longPress()
        {
            let bounds = self.imageView.bounds
            UIView.animateWithDuration(0.5, animations: {
                self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
            })
    
            println("user pressed on image")
        }
    

    动画只会导致UIImageview的高度和宽度在longPress上扩展,但是当释放压力机时,边界会继续增长。释放压力机时如何将UIImageview边界返回到原始宽度和高度?

    1 回复  |  直到 9 年前
        1
  •  4
  •   Parth Bhadaja    9 年前

    试试看:

    var oldbounds:CGRect!
    @IBOutlet var image: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(animated: Bool) {
    
        var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
        longPress.minimumPressDuration = 0.01
        image.addGestureRecognizer(longPress)
        image.userInteractionEnabled = true
    }
    
    func longPress(gesture:UILongPressGestureRecognizer)
    {
        if gesture.state == UIGestureRecognizerState.Began
        {
            oldbounds = self.image.bounds
        }
        else if gesture.state == UIGestureRecognizerState.Changed
        {
            let bounds = self.image.bounds
            UIView.animateWithDuration(0.5, animations: {
                self.image.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
            })
    
            println("user pressed on image")
        }
        else
        {
            let bounds = self.image.bounds
            UIView.animateWithDuration(0.5, animations: {
                self.image.bounds = self.oldbounds
            })
    
            println("user release on image")
        }
    }