触摸UIImageView的触摸和触摸操作

触摸UIImageView的触摸和触摸操作

本文介绍了触摸UIImageView的触摸和触摸操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

}


推荐答案

以下是解决方案:

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

Note: Put this inside a UIView SubClass and add: userInteractionEnabled = true inside the init block

这篇关于触摸UIImageView的触摸和触摸操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:08