如何在旋转中使用TapGestureRecognizer处理UIImage

时间:2017-11-01 09:55:46

标签: ios swift uiview uitapgesturerecognizer

我有一个标签和UIImage布局如下。

LabelMonth ImgArrowDown

此LabelMonth的TapGestureRecognizer如下所示。

当用户点击LabelMonth时,ImageArrowDown将旋转360,使其看起来向上。

问题:

  • 当用户再次点击LabelMonth时,如何将ImageArrowDown旋转回其原点位置,这意味着箭头指向下方?

viewDidLoad中(){

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:
        #selector(HandleImgRotation))

    LabelMonth.isUserInteractionEnabled = true LabelMonth.addGestureRecognizer(tapGestureRecognizer)

    }




    func HandleImgRotation(_sender:AnyObject){

    UIView.animate(withDuration:1.0, animations: ({self.imgArrowDown.transform = CGAffineTransform(rotationAngle: self.radian(degree:360))
                          }))

    }



    func radian(degrees:Double) -> CGFloat{

    return CGFloat(degrees * .pi/degrees) 

    }

由于

1 个答案:

答案 0 :(得分:1)

以下方法应解决您的问题...

var rotated = false

func HandleImgRotation() {
        UIView.animate(withDuration: 2.0) {
            if !self.rotated {
                UIView.animate(withDuration:1.0, animations: ({
                    ImgArrowDown.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 0.999) )
                    self.rotated = true
                }))
            } else {
                UIView.animate(withDuration:1.0, animations: ({
                    ImgArrowDown.transform = CGAffineTransform.identity
                    self.rotated = false
                }))
            }
        }
    }