简体   繁体   中英

tapGestureRecognizer.location brings nil in UICollectionView

In my swift app I created class that delegates UICollectionViewController . Additionally, I have other class responsible for handling 'UICollectionReusableView`.

So in the first class I have a method:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! UserProfileHeaderCollectionReusableView

and thanks to this - in this method - I have access to all buttons and labels stored in the header view, eg:

headerView.followButton.isHidden = false

headerView.followButton.addGestureRecognizer(
           UITapGestureRecognizer(target: self, action: #selector(followThisUser)))

later on, I have a method followThisUser :

@objc private func followThisUser(tapGestureRecognizer: UITapGestureRecognizer) {

    if (!doIFollow) {

        followUser(userId)
    } else {
        unfollowUser(userId)
    }
}

and based on the flag doIFollow I'm performing specific method.

I would like to give user a feedback when he presses a button and change it's color as soon as he presses it. I tried to access this button by adding:

    let tapLocation = tapGestureRecognizer.location(in: self.userProfileCollectionView)

    let indexPath : NSIndexPath = self.userProfileCollectionView.indexPathForItem(at: tapLocation)! as NSIndexPath

to the followThisUser method, but it throws error:

fatal error: unexpectedly found nil while unwrapping an Optional value

How can I access the followButton then?

What you can do, since you set a UITapGestureRecognizer on your button - is get the original UIView from the gesture recognizer in your handler method. Something like this to turn the button background color orange:

@objc private func buttonTap(tapGestureRecognizer: UITapGestureRecognizer) {

    // Get the view that the gesture is attached to
    let button = tapGestureRecognizer.view

    // Change the view's background color
    button?.backgroundColor = UIColor.orange

}

Now if your original button is a UIButton , and you need to use some of the special properties of the UIButton class, you can cast the view as a UIButton

@objc private func buttonTap(tapGestureRecognizer: UITapGestureRecognizer) {

    // Get the view that the gesture is attached to
    let button = tapGestureRecognizer.view as! UIButton

    // Change the UIButton's title label text color
    button.setTitleColor(UIColor.orange, for: .normal)

}

Try this one

var tapGesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "processTapGesture:")
        tapGesture.numberOfTapsRequired = 1
        collectionView.addGestureRecognizer(tapGesture)

Handle gesture

func processTapGesture (sender: UITapGestureRecognizer)
    {
        if sender.state == UIGestureRecognizerState.Ended
        {
            var point:CGPoint = sender.locationInView(collectionView)
            var indelPath:NSIndexPath =collectionView.indexPathForItemAtPoint(point)
            if indexPath
            {
                print("image taped")
            }
            else
            {
               //Do Some Other Stuff Here That Isnt Related;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM