触摸和触摸UIImageView的动作

时间:2015-12-06 18:40:49

标签: ios swift2

我想要实现的是当用户触摸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"
    }

}

3 个答案:

答案 0 :(得分:5)

UITapGestureRecognizer并未将其状态更改为.Began,但UILongPressGestureRecognizer会将其状态更改为minimumPressDuration。如果由于某种原因你想直接覆盖触摸回调,你可以使用UILongPressGestureRecognizer,其中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() } } } 非常短,只有0.1,以达到效果。

@ Daij-Djan的例子:

DataDictionary ={} //out of the data collection loop

while collectingData==True:
    DataDictionary['%s'%token] = data[0][1:]

答案 1 :(得分:2)

以下是解决方案:

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

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

答案 2 :(得分:1)

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