swift2 UIImageView的向上触摸和向下触摸操作

euoag5mw  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(199)

我想要实现是,当用户触摸UIImageView时设置Image1,当用户抬起他的手指时设置Image2。
我只能获取UIGestureRecognizerState。以以下代码结束

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

}
fquxozlt

fquxozlt1#

UITapGestureRecognizer不会将其状态更改为.Began,但UILongPressGestureRecognizer会。如果您出于某种原因希望字体直接覆盖触摸回调,则可以使用带有非常短的minimumPressDuration(如0.1)的UILongPressGestureRecognizer来实现此效果。
@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()
    }
  }
}
8e2ybdfx

8e2ybdfx2#

解决方案如下:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if touch.view == self {
            //began
        }
    }
    super.touchesBegan(touches, with: event)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if touch.view == self {
            //end
        }
    }
    super.touchesEnded(touches, with: event)
}

**注意:**将其放入UIView子类中并添加:userInteractionEnabled = true在初始化块内

yfwxisqw

yfwxisqw3#

最好也是最实用的解决方案是将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
    }
}

相关问题