Uigesturerecognizer on Uiimageview

UIGestureRecognizer on UIImageView

Check that userInteractionEnabled is YES on the UIImageView. Then you can add a gesture recognizer.

imageView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinch:)];
pgr.delegate = self;
[imageView addGestureRecognizer:pgr];
[pgr release];
:
:
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
//handle pinch...
}

How to assign an action for UIImageView object in Swift

You'll need a UITapGestureRecognizer.
To set up use this:

override func viewDidLoad()
{
super.viewDidLoad()

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView

// Your action
}

(You could also use a UIButton and assign an image to it, without text and than simply connect an IBAction)

how to add gesture recognizer to UIImage and UIButton of UITableViewCell?

Your question title is misleading -- You are adding gesture recognizer to the UIElements, not the whole cell.

Buttons don't need a gesture recognizer since they subclass from UIControl.

private func initialize() {
let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileTapped))
profileImageView.addGestureRecognizer(imageTapGesture)
// imageviews by default aren't interactable
profileImageView.isUserInteractionEnabled = true

fullNameButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}

@objc private func profileTapped() {
delegate?.didClickProfileImageOf(cell: self)
}

@objc private func buttonTapped() {
delegate?.didClickProfileNameOf(cell: self)
}

-- Updated per comment --

protocol MediaTableViewCellDelegate: class {
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileImage: Bool)
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileName: Bool)
}

adding gesture recognizer to uiimage

There are two problems here:

  1. isUserInteractionEnabled is false by default
  2. you have to assign each image view a separate gesture recognizer

Solution

Instead of setting isUserInteractionEnabled to true for each image view individually, I suggest placing that code in the Banner class itself. Since it seems your initializing it from the storyboard, using the awakeFromNib function should work just fine:

class Banner: UIView {

@IBOutlet weak var img: UIImageView!

override func awakeFromNib() {
super.awakeFromNib()
img.isUserInteractionEnabled = true
}
}

In your createSlides() function, add a separate tap gesture to each view:

let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide1.img.image = UIImage(named: "bannerex")
slide1.img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))

or even better for scaling & no-code-redundancies:

function tap() -> UITapGestureRecognizer {
return UITapGestureRecognizer(target: self, action: #selector(tapped))
}

let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide1.img.image = UIImage(named: "bannerex")
slide1.img.addGestureRecognizer(tap())

one tap gesture on multiple UIImageView

A UIGestureRecognizer must be used with a single view only. You are using same object for both views. Try this.

override func viewDidLoad() {
// Do any additional setup after loading the view.

super.viewDidLoad()

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

cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)


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

plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer2)
}

UIImageView UITapGestureRecognizer not working

Okey, so I got it working somehow. All I need to do was to add view controllers inside my LoginViewController like so:

self.addChildViewController(myVC1)

Instead of using view.addSubview(container)

Notice: I'm not sure it is the correct way of doing this, and please comment if it's not. Meanwhile it solves my problem.

UITapGestureRecognizer working on UIImageView but not on UILabel

You need different gesture for all control

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
avatarRoundImageView.userInteractionEnabled = true
avatarRoundImageView.addGestureRecognizer(tapGesture)

let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
nameLabel.userInteractionEnabled = true
nameLabel.addGestureRecognizer(tapGesture2)


Related Topics



Leave a reply



Submit