触摸UIImageView的触摸和触摸操作

我想要实现的是当用户触摸UIImageView设置Image1时,当用户抬起他的手指设置Image2时。

我只能使用此代码获取UIGestureRecognizerState.Ended

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" } } 

UITapGestureRecognizer不会将其状态更改为.Began,但UILongPressGestureRecognizer会执行此操作。 如果由于某种原因,字体想要直接覆盖触摸回调,则可以使用UILongPressGestureRecognizer,其minimumPressDuration非常短,如0.1,以达到效果。

例如@ 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() } } } 

这是解决方案:

 override func touchesBegan(touches: Set, withEvent event: UIEvent?) { if let touch = touches.first { if touch.view == imageView { //began } } super.touchesBegan(touches, withEvent:event) } override func touchesEnded(touches: Set, withEvent event: UIEvent?) { if let touch = touches.first{ if touch.view == imageView { //end } } super.touchesEnded(touches, withEvent: event) } 

注意:将它放在UIView SubClass中并在init块中添加: userInteractionEnabled = true

最好和最实用的解决方案是将UIImageView嵌入到UIControl子类中。

默认情况下, UIImageView启用了用户交互。 如果您创建一个简单的UIControl子类,您可以轻松地将图像视图添加到其中,然后使用以下方法来实现您想要的:

 let control = CustomImageControl() control.addTarget(self, action: "imageTouchedDown:", forControlEvents: .TouchDown) control.addTarget(self, action: "imageTouchedUp:", forControlEvents: [ .TouchUpInside, .TouchUpOutside ]) 

这样做的好处是,您可以访问所有不同的触摸事件,从而节省您自己检测它们的时间。

根据您的想法,您还可以覆盖var highlighted: Boolvar selected: Bool用于检测用户何时与图像交互。
最好这样做,这样您的用户就可以在其应用中使用所有控件获得一致的用户体验。

一个简单的实现看起来像这样:

 final class CustomImageControl: UIControl { let imageView: UIImageView = UIImageView() override init(frame: CGRect) { super.init(frame: frame) // setup our image view imageView.translatesAutoresizingMaskIntoConstraints = false addSubview(imageView) imageView.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true imageView.trailingAnchor.constraintEqualToAnchor(trailingAnchor).active = true imageView.topAnchor.constraintEqualToAnchor(topAnchor).active = true imageView.bottomAnchor.constraintEqualToAnchor(bottomAnchor).active = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } final class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let control = CustomImageControl() control.translatesAutoresizingMaskIntoConstraints = false control.imageView.image = ... // set your image here. control.addTarget(self, action: "imageTouchedDown:", forControlEvents: .TouchDown) control.addTarget(self, action: "imageTouchedUp:", forControlEvents: [ .TouchUpInside, .TouchUpOutside ]) view.addSubview(control) control.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true control.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true } @objc func imageTouchedDown(control: CustomImageControl) { // pressed down on the image } @objc func imageTouchedUp(control: CustomImageControl) { // released their finger off the image } }