简体   繁体   English

向右和向左滑动后转换集合视图单元格容器

[英]Transform collection view cell container after Swipe to right and left

I'm trying to add Swipe to right and left to collection view cell to transform the container to the right and left with a certain angle我正在尝试将向右和向左滑动添加到集合视图单元格以将容器以一定角度向右和向左变换

Here is my initial setup这是我的初始设置

   private func setupGestures() {
        let swipeToRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeRight))
        swipeToRight.direction = .right
        container.addGestureRecognizer(swipeToRight)
        
        let swipeToLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeLeft))
        swipeToLeft.direction = .left
        container.addGestureRecognizer(swipeToLeft)
        
    }
    
    @objc func respondToSwipeRight(gesture: UIGestureRecognizer) {
        
        let angle = CGFloat(Double.pi/2)
        
        UIView.animate(withDuration: 0.5) {
            self.container.transform = CGAffineTransform(rotationAngle: angle)
        }
    }
    
    
    @objc func respondToSwipeLeft(gesture: UIGestureRecognizer) {
        
        let angle = -CGFloat(Double.pi/2)
        
        UIView.animate(withDuration: 0.5) {
            self.container.transform = CGAffineTransform(rotationAngle: angle)
        }
    }



But it completely rotate the container, which is I don't want, I want to make it something like it, and turn back to it's initial position after a sec or two但它完全旋转了容器,这是我不想要的,我想让它变成类似的东西,并在一两秒后返回到它的初始 position

[![how it should transform][1]][1] [![它应该如何转变][1]][1]

And it would be so awesome that move based on Swiping position, I mean not automatically goes to that level of position, move with finger tip and when it reach there, just stop moving.如果基于滑动 position 移动,我的意思是不会自动进入 position 的水平,用指尖移动,当它到达那里时,停止移动。

Could anyone help me to implement it, I have no idea how I can do it谁能帮我实现它,我不知道该怎么做

Many thanks非常感谢

Adding completion in UIView.animate in order to turn back to initial state. After a sec or two is depends on your duration in UIView.animateUIView.animate中添加completion以返回初始 state。一两秒后取决于您在UIView.animate中的duration

Code will be like this代码会是这样的

UIView.animate(withDuration: 0.5, animations: {
    self.container.transform = CGAffineTransform(rotationAngle: 0.5)
}, completion: {
    _ in
    // turn back to the previous before transform
    self.container.transform = .identity
})

And for your second question, you can implement with touchesBegan and touchesMoved or add PanGesture to handle changing position container view frame like you want.对于你的第二个问题,你可以使用touchesBegantouchesMoved来实现,或者添加PanGesture来处理你想要的改变 position 容器视图框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM