Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch up and Touch down action for UIImageView

Tags:

ios

swift2

what i want to achieve is when user touch on UIImageView set Image1, when user lifts his finger set Image2.

i can only get UIGestureRecognizerState.Ended with this code

var tap = UITapGestureRecognizer(target: self, action: Selector("tappedMe:"))     
imageView.addGestureRecognizer(tap)
imageView.userInteractionEnabled = true
   
func tappedMe(gesture: UITapGestureRecognizer)
{
    
    if gesture.state == UIGestureRecognizerState.Began{
        imageView.image=originalImage
        dbgView.text="Began"
    }else if  gesture.state == UIGestureRecognizerState.Ended{
        imageView.image=filteredImage
        dbgView.text="Ended"
    }else if  gesture.state == UIGestureRecognizerState.Cancelled{
        imageView.image=filteredImage
        dbgView.text="Cancelled"
    }else if  gesture.state == UIGestureRecognizerState.Changed{
        imageView.image=filteredImage
        dbgView.text="Changed"
    }

}
like image 705
H4SN Avatar asked Jan 08 '23 01:01

H4SN


1 Answers

The UITapGestureRecognizer doesn't change it's state to .Began, but the UILongPressGestureRecognizer does. If you for some reason font want to override the touch callbacks directly you could use a UILongPressGestureRecognizer with a very short minimumPressDuration of like 0.1 to achieve the effect.

Example by @Daij-Djan:

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    var tap = UILongPressGestureRecognizer(target: self, action: Selector("pressedMe:"))
    tap.minimumPressDuration = 0
    self.view.addGestureRecognizer(tap)
    self.view.userInteractionEnabled = true
  }

  func pressedMe(gesture: UITapGestureRecognizer) {
    if gesture.state == .Began{
      self.view.backgroundColor = UIColor.blackColor()
    } else if  gesture.state == .Ended {
      self.view.backgroundColor = UIColor.whiteColor()
    }
  }
}
like image 129
Kie Avatar answered Jan 15 '23 21:01

Kie