gpt4 book ai didi

ios - UITapGestureRecognizer 只调用一次

转载 作者:行者123 更新时间:2023-11-28 08:43:14 34 4
gpt4 key购买 nike

我有这个代码:

func initPlaceHolder(width: CGFloat, height: CGFloat){
var firstPlaceHolderPosition: CGFloat = 0;

UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)

let context = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0, y: 0, width: width, height: height)

CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
CGContextSetLineWidth(context, 1)

CGContextAddRect(context, rectangle)
CGContextDrawPath(context, .FillStroke)

let img = UIGraphicsGetImageFromCurrentImageContext()

for i in 1...4 {
let imageView = StompUIImageView(frame: CGRect(x: firstPlaceHolderPosition, y: 0, width: width, height: height))
let tap = UITapGestureRecognizer(target: self, action: "doubleTapped:")

tap.numberOfTapsRequired = 2

imageView.image = img
imageView.addGestureRecognizer(tap)
imageView.userInteractionEnabled = true
imageView.stompID = String(i)
imageView.stompSlot = i

addSubview(imageView)

firstPlaceHolderPosition = firstPlaceHolderPosition + width + 10

UIGraphicsEndImageContext()
}
}

func doubleTapped(sender: UITapGestureRecognizer) {
let view = sender.view as! StompUIImageView
print(view.stompID)
}

基本上,doubleTapped 处理程序只为第一个 UIImageView 调用,而不是为所有 4 个调用。抱歉,我是 ios 开发的新手,我很难理解。

感谢您的帮助

最佳答案

试试这个……

用这个替换你的代码....

希望对您有所帮助。

func initPlaceHolder(width: CGFloat, height: CGFloat){
var firstPlaceHolderPosition: CGFloat = 0;

UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0)

let context = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0, y: 0, width: width, height: height)

CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
CGContextSetLineWidth(context, 1)

CGContextAddRect(context, rectangle)
CGContextDrawPath(context, .FillStroke)

let img = UIGraphicsGetImageFromCurrentImageContext()

let tap = UITapGestureRecognizer(target: self, action: "doubleTapped:")

tap.numberOfTapsRequired = 2


for i in 1...4 {
let imageView = StompUIImageView(frame: CGRect(x: firstPlaceHolderPosition, y: 0, width: width, height: height))

imageView.image = img
imageView.addGestureRecognizer(tap)
imageView.userInteractionEnabled = true
imageView.stompID = String(i)
imageView.stompSlot = i

addSubview(imageView)

firstPlaceHolderPosition = firstPlaceHolderPosition + width + 10

UIGraphicsEndImageContext()
}
}

func doubleTapped(sender: UITapGestureRecognizer) {
let view = sender.view as! StompUIImageView
print(view.stompID)
}

关于ios - UITapGestureRecognizer 只调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941231/

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