UILongPressureRecognizer with Image view

59 views Asked by At

Good morning everyone,

I am a newbie Swift developer and I am facing the following problem implementing an exercise I am dealing with.

I have a collection view with collection cells displaying images I have imported in XCODE; when I tap with the finger on the screen I would like to replace the image currently being display with another one that i have also imported, and animate this replacement.

I am implementing the UITapLongPressureRecognizer method but i am getting confused on which state to implement for the recognizer, just to replace the first image view with the one I want to be shown when I tap the screen to scroll up-down.

As you can see from the code below the two recognizer I think should be more appropriate to be implemented are the "Begin" and "Ended". My problem is that when the state .Begin starts I don't know how to animate the replacement of one image view with another and so when the state is .Ended I don't know how to replace the second image with the first one and animate the replacement (I want it to be like for example a Modal segue with "Cross Dissolve" transition).

Thank you in advance for your kindness and patience.

class MainViewController: UICollectionViewController {

var fashionArray = [Fashion]()
private var selectedIndexPath: NSIndexPath?

override func viewDidLoad() {
    super.viewDidLoad()

    selectedIndexPath = NSIndexPath()

    //Register the cell
    let collectionViewCellNIB = UINib(nibName: "CollectionViewCell", bundle: nil)
    self.collectionView!.registerNib(collectionViewCellNIB, forCellWithReuseIdentifier: reuseIdentifier)

    //Configure the size of the image according to the device size
    let layout = collectionViewLayout as! UICollectionViewFlowLayout
    let bounds = UIScreen.mainScreen().bounds
    let width = bounds.size.width
    let height = bounds.size.height
    layout.itemSize = CGSize(width: width, height: height)

    let longPressRecogn = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    collectionView!.addGestureRecognizer(longPressRecogn)
}

func handleLongPress(recognizer: UILongPressGestureRecognizer){

    var cell: CollectionViewCell!
    let location = recognizer.locationInView(collectionView)
    let indexPath = collectionView!.indexPathForItemAtPoint(location)
    if let indexPath = indexPath {
        cell = collectionView!.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
    }

    switch recognizer.state {
    case .Began:
        cell.screenTapped = false
    case .Ended:
        cell.screenTapped = false
    default:
        println("")
    }
}
1

There are 1 answers

0
enes On

First of all, I suggest you to use UITapGestureRecognizer instead of long press. Because, as far as I understand, you only tap once instead of pressing for a time.

let tapRecognizer = UITapGestureRecognizer(target: self, action:Selector("tapped:"))
collectionView.addGestureRecognizer(tapRecognizer)

And when the user tapped, you can use UIView animations to change the image. You can check the Example II from the following link to get insight about animations. http://www.appcoda.com/view-animation-in-swift/