gpt4 book ai didi

ios - Touch up 和 Touch down 操作 UIImageView

转载 作者:可可西里 更新时间:2023-11-01 06:24:12 25 4
gpt4 key购买 nike

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

我只能得到 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"
}

}

最佳答案

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()
}
}
}

关于ios - Touch up 和 Touch down 操作 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com