【问题标题】:How to handle UIImage with TapGestureRecognizer in rotating如何在旋转中使用 TapGestureRecognizer 处理 UIImage
【发布时间】:2017-11-01 09:55:46
【问题描述】:

我有一个标签和 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) 

    }

谢谢

【问题讨论】:

    标签: ios swift uiview uitapgesturerecognizer


    【解决方案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
                    }))
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多