Reputation: 839
I have several UIImageViews on my screen and if one of them is tapped, it has to change his color. As I had the same problem with UIView, the solution was:
func viewTapped(recognizer:UITapGestureRecognizer) {
viewTouched = recognizer.view as UIView!
thisCard.backgroundColor = UIColor.orangeColor()
}
But I haven't found the similar action for the image view. Could you help me?
Upvotes: 1
Views: 369
Reputation: 1
Is UIImageView
not working?
If not, check userInteractionEnabled
.
Set userInteractionEnabled
to true.
UIImageView
public var userInteractionEnabled: Bool // default is NO
Upvotes: 0
Reputation: 1371
Create an extension of image view
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
}
}
Now use like this
yourImageView.image = yourImageView.image!.tintWithColor(UIColor.orangeColor())
Upvotes: 0
Reputation: 4535
You can change the tintColor
of your UIImageView
s :
func imageViewTapped(recognizer:UITapGestureRecognizer) {
guard let imageView = recognizer.view as? UIImageView
else { return }
imageView.tintColor = UIColor.orangeColor()
}
Upvotes: 1