【问题标题】:Interaction of UIImageView and UITapGestureRecognizer in swiftUIImageView和UITapGestureRecognizer在swift中的交互
【发布时间】:2016-08-22 09:11:39
【问题描述】:

我的屏幕上有几个 UIImageViews,如果其中一个被点击,它必须改变他的颜色。由于我在 UIView 上遇到了同样的问题,因此解决方案是:

    func viewTapped(recognizer:UITapGestureRecognizer) {

    viewTouched = recognizer.view as UIView!
    thisCard.backgroundColor = UIColor.orangeColor()

}

但我还没有为图像视图找到类似的操作。你能帮帮我吗?

【问题讨论】:

    标签: ios swift uiview uiimageview uitapgesturerecognizer


    【解决方案1】:

    您可以更改tintColorUIImageViews :

    func imageViewTapped(recognizer:UITapGestureRecognizer) {
    
        guard let imageView = recognizer.view as? UIImageView
          else { return }
        imageView.tintColor = UIColor.orangeColor()
    
    }
    

    【讨论】:

      【解决方案2】:

      创建图像视图的扩展

      extension UIImage {
          func tintWithColor(color:UIColor)->UIImage {
      
              UIGraphicsBeginImageContext(self.size)
              let context = UIGraphicsGetCurrentContext()
      
              // flip the image
              CGContextScaleCTM(context, 1.0, -1.0)
              CGContextTranslateCTM(context, 0.0, -self.size.height)
      
              // multiply blend mode
              CGContextSetBlendMode(context, .Overlay)
      
              let rect = CGRectMake(0, 0, self.size.width, self.size.height)
              CGContextClipToMask(context, rect, self.CGImage)
              color.setFill()
              CGContextFillRect(context, rect)
      
              // create uiimage
              let newImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
              return newImage
      
          }
      }
      

      现在这样使用

      yourImageView.image = yourImageView.image!.tintWithColor(UIColor.orangeColor())
      

      【讨论】:

        【解决方案3】:

        UIImageView 不工作吗? 如果没有,请检查userInteractionEnabled

        userInteractionEnabled 设置为真。

        UIImageView

         public var userInteractionEnabled: Bool // default is NO
        

        【讨论】:

        • 是的,现在可以使用了。第一个答案已修复它。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多